Codeforces Round #326 (Div. 2)
2015-10-26 12:53
302 查看
A. Duff and Meat
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Duff is addicted to meat! Malek wants to keep her happy for n days. In order to be happy in i-th
day, she needs to eat exactly ai kilograms
of meat.
![](http://espresso.codeforces.com/99fc691eaf57150b35c52b82b26f08446a71223c.png)
There is a big shop uptown and Malek wants to buy meat for her from there. In i-th day, they sell meat for pi dollars
per kilogram. Malek knows all numbers a1, ..., an and p1, ..., pn.
In each day, he can buy arbitrary amount of meat, also he can keep some meat he has for the future.
Malek is a little tired from cooking meat, so he asked for your help. Help him to minimize the total money he spends to keep Duff happy for n days.
Input
The first line of input contains integer n (1 ≤ n ≤ 105),
the number of days.
In the next n lines, i-th
line contains two integers ai and pi (1 ≤ ai, pi ≤ 100),
the amount of meat Duff needs and the cost of meat in that day.
Output
Print the minimum money needed to keep Duff happy for n days, in one line.
Sample test(s)
input
output
input
output
Note
In the first sample case: An optimal way would be to buy 1 kg on the first day, 2 kg on the second day and 3 kg on the third day.
In the second sample case: An optimal way would be to buy 1 kg on the first day and 5 kg (needed meat for the second and third day) on the second day.
题意:给定一个天数n,然后给出每天需要的肉量和当前肉的价格,(肉可以多买,多余的留到以后吃)问这n天买肉的价钱的最小值
思路:只要判断如果今天没肉了,那么今天肯定要买今天所需的肉,然后如果今天的肉价比明天低,那么在今天把明天的分量也
买了,如果今天的肉价比后天也便宜,继续买后天所需的肉,以此类推即可。
B. Duff in Love
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Duff is in love with lovely numbers! A positive integer x is called lovely if
and only if there is no such positive integera > 1 such that a2 is
a divisor of x.
![](http://codeforces.com/predownloaded/01/2d/012dd215c8c4fe26618c1deac16b705d6e91c093.png)
Malek has a number store! In his store, he has only divisors of positive integer n (and he has all of them). As a birthday present,
Malek wants to give her a lovely number from his store. He wants this number to be as big as possible.
Malek always had issues in math, so he asked for your help. Please tell him what is the biggest lovely number in his store.
Input
The first and only line of input contains one integer, n (1 ≤ n ≤ 1012).
Output
Print the answer in one line.
Sample test(s)
input
output
input
output
Note
In first sample case, there are numbers 1, 2, 5 and 10 in the shop. 10 isn't divisible by any perfect square, so 10 islovely.
In second sample case, there are numbers 1, 2, 3, 4, 6 and 12 in the shop. 12 is divisible by 4 = 22,
so 12 is notlovely, while 6 is indeed lovely.
题意:给定数n,求一个n的最大满足要求的因子。
思路:将n的素因子想乘即可
C. Duff and Weight Lifting
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Recently, Duff has been practicing weight lifting. As a hard practice, Malek gave her a task. He gave her a sequence of weights. Weight of i-th
of them is 2wi pounds.
In each step, Duff can lift some of the remaining weights and throw them away. She does this until there's no more weight left. Malek asked her to minimize the number of steps.
![](http://codeforces.com/predownloaded/05/1c/051c344e6b9eb78f1f88fc810efa56b75a0ee02a.png)
Duff is a competitive programming fan. That's why in each step, she can only lift and throw away a sequence of weights 2a1, ..., 2ak if
and only if there exists a non-negative integer x such that 2a1 + 2a2 + ... + 2ak = 2x,
i. e. the sum of those numbers is a power of two.
Duff is a competitive programming fan, but not a programmer. That's why she asked for your help. Help her minimize the number of steps.
Input
The first line of input contains integer n (1 ≤ n ≤ 106),
the number of weights.
The second line contains n integers w1, ..., wn separated
by spaces (0 ≤ wi ≤ 106 for
each 1 ≤ i ≤ n), the powers of two forming the weights values.
Output
Print the minimum number of steps in a single line.
Sample test(s)
input
output
input
output
Note
In the first sample case: One optimal way would be to throw away the first three in the first step and the rest in the second step. Also, it's not possible to do it in one step because their sum is not a power of two.
In the second sample case: The only optimal way is to throw away one weight in each step. It's not possible to do it in less than 4 steps because there's no subset of weights with more than one weight and sum equal to a power of two.
题意:给定n个哑铃,每个重量为2^n[i] 每次可以拿起若干个,但是这若干个要满足加起来的和是2的次方,问最少需要几次能举完这些哑铃
思路:题目的意思相当于两个重量为2^i的哑铃组合在一起变成2^i+1。只要一直组合,直到不能组成那么算一步,然后剩下的数继续次操作
用优先队列很好解决。
D. Duff in Beach
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
While Duff was resting in the beach, she accidentally found a strange array b0, b1, ..., bl - 1 consisting
of l positive integers. This array was strange because it was extremely long, but there was another (maybe shorter) array,a0, ..., an - 1 that b can
be build from a with formula: bi = ai mod n where a mod b denoted
the remainder of dividing aby b.
![](http://espresso.codeforces.com/3796f0b777ac9dbea8c163d08f34e94137182a19.png)
Duff is so curious, she wants to know the number of subsequences of b like bi1, bi2, ..., bix (0 ≤ i1 < i2 < ... < ix < l),
such that:
1 ≤ x ≤ k
For each 1 ≤ j ≤ x - 1,
![](http://codeforces.com/predownloaded/0d/7c/0d7c0d743694764e696d274aa6aaa2b8d2911497.png)
For each 1 ≤ j ≤ x - 1, bij ≤ bij + 1.
i.e this subsequence is non-decreasing.
Since this number can be very large, she want to know it modulo 109 + 7.
Duff is not a programmer, and Malek is unavailable at the moment. So she asked for your help. Please tell her this number.
Input
The first line of input contains three integers, n, l and k (1 ≤ n, k, n × k ≤ 106 and 1 ≤ l ≤ 1018).
The second line contains n space separated integers, a0, a1, ..., an - 1 (1 ≤ ai ≤ 109 for
each 0 ≤ i ≤ n - 1).
Output
Print the answer modulo 1 000 000 007 in one line.
Sample test(s)
input
output
input
output
Note
In the first sample case,
![](http://espresso.codeforces.com/d9d8fe92937aeef2bcddb9d213e5587f0f950087.png)
.
So all such sequences are:
![](http://codeforces.com/predownloaded/2f/1a/2f1a184d6d2c19fe08dc2ee379b6771f3dda6830.png)
,
![](http://codeforces.com/predownloaded/21/ec/21eca0ea1757903a6ca9da32f69bfd38381c691c.png)
,
![](http://codeforces.com/predownloaded/d7/bc/d7bc28124edccfad2726903c0602a42947790453.png)
,
![](http://codeforces.com/predownloaded/15/00/1500f524726eb4a3990d99eb229acf9680d8eaf0.png)
,
![](http://espresso.codeforces.com/c6db71d99107f752b5caa1ba7e44af1d2302e5db.png)
,
![](http://codeforces.com/predownloaded/4c/94/4c94cceb7f4ccdf9de262f8b435874a222b632cd.png)
,
![](http://codeforces.com/predownloaded/0b/db/0bdb3b2f17e237a6d27930411f1f03f57caaa3e0.png)
,
![](http://codeforces.com/predownloaded/92/55/9255e13e81f8cd8bedad1201420689a698771d33.png)
,
![](http://espresso.codeforces.com/8756896f0d00888771e69f1a9ccddeee19d4a9ab.png)
and
![](http://espresso.codeforces.com/e771f8119b1b8218c875759aa03e38312514c9a2.png)
.
DP
首先这个数在一个块类的相对位置没有关系,只看他的相对大小
然后最后一个块暴力,有点像分块一样
dp[i][j]表示第i个数,长度为j的方案一共有多少种,然后转移就好了
(没写orz,解题思路来自acm吧的讨论贴)
E. Duff in the Army
time limit per test
4 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output
Recently Duff has been a soldier in the army. Malek is her commander.
Their country, Andarz Gu has n cities (numbered from 1 to n)
and n - 1 bidirectional roads. Each road connects two different cities. There exist a unique path between any two cities.
There are also m people living in Andarz Gu (numbered from 1 to m).
Each person has and ID number. ID number ofi - th person is i and
he/she lives in city number ci.
Note that there may be more than one person in a city, also there may be no people living in the city.
![](http://codeforces.com/predownloaded/d3/0b/d30bf401a5e9f5b2c5e8db0f56b736a1ae8d5018.png)
Malek loves to order. That's why he asks Duff to answer to q queries. In each query, he gives her numbers v, u and a.
To answer a query:
Assume there are x people living in the cities lying on the path from city v to
city u. Assume these people's IDs arep1, p2, ..., px in
increasing order.
If k = min(x, a), then Duff should tell Malek numbers k, p1, p2, ..., pk in
this order. In the other words, Malek wants to know a minimums on that path (or less, if there are less than a people).
Duff is very busy at the moment, so she asked you to help her and answer the queries.
Input
The first line of input contains three integers, n, m and q (1 ≤ n, m, q ≤ 105).
The next n - 1 lines contain the roads. Each line contains two integers v and u,
endpoints of a road (1 ≤ v, u ≤ n,v ≠ u).
Next line contains m integers c1, c2, ..., cm separated
by spaces (1 ≤ ci ≤ n for
each 1 ≤ i ≤ m).
Next q lines contain the queries. Each of them contains three integers, v, u and a (1 ≤ v, u ≤ n and 1 ≤ a ≤ 10).
Output
For each query, print numbers k, p1, p2, ..., pk separated
by spaces in one line.
Sample test(s)
input
output
Note
Graph of Andarz Gu in the sample case is as follows (ID of people in each city are written next to them):
![](http://codeforces.com/predownloaded/59/da/59da2d98ad6ffcd148ebab415aeffced4f865b17.png)
E题,树链剖分套线段树,线段树的每个节点维护一个优先队列,队列中存区间中最小的10个值,查询直接维护链上最小a个元素即可
(没写orz,解题思路来自acm吧的讨论贴)
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Duff is addicted to meat! Malek wants to keep her happy for n days. In order to be happy in i-th
day, she needs to eat exactly ai kilograms
of meat.
![](http://espresso.codeforces.com/99fc691eaf57150b35c52b82b26f08446a71223c.png)
There is a big shop uptown and Malek wants to buy meat for her from there. In i-th day, they sell meat for pi dollars
per kilogram. Malek knows all numbers a1, ..., an and p1, ..., pn.
In each day, he can buy arbitrary amount of meat, also he can keep some meat he has for the future.
Malek is a little tired from cooking meat, so he asked for your help. Help him to minimize the total money he spends to keep Duff happy for n days.
Input
The first line of input contains integer n (1 ≤ n ≤ 105),
the number of days.
In the next n lines, i-th
line contains two integers ai and pi (1 ≤ ai, pi ≤ 100),
the amount of meat Duff needs and the cost of meat in that day.
Output
Print the minimum money needed to keep Duff happy for n days, in one line.
Sample test(s)
input
3 1 3 2 2 3 1
output
10
input
3 1 3 2 1 3 2
output
8
Note
In the first sample case: An optimal way would be to buy 1 kg on the first day, 2 kg on the second day and 3 kg on the third day.
In the second sample case: An optimal way would be to buy 1 kg on the first day and 5 kg (needed meat for the second and third day) on the second day.
题意:给定一个天数n,然后给出每天需要的肉量和当前肉的价格,(肉可以多买,多余的留到以后吃)问这n天买肉的价钱的最小值
思路:只要判断如果今天没肉了,那么今天肯定要买今天所需的肉,然后如果今天的肉价比明天低,那么在今天把明天的分量也
买了,如果今天的肉价比后天也便宜,继续买后天所需的肉,以此类推即可。
#include<iostream> #include<cstring> #include<string> #include<stdio.h> using namespace std; const int MAXN=100005; int a[MAXN],p[MAXN],vis[MAXN]; int main() { int n; long long int mincost=0; memset(vis,0,sizeof(vis)); scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%lld %lld",&a[i],&p[i]); } for(int i=1;i<=n;i++) { if(!vis[i]) { mincost+=(a[i]*p[i]); vis[i]=1; for(int j=i+1;j<=n;j++) { if(p[i]<p[j]) { mincost+=(a[j]*p[i]); vis[j]=1; } else { break; } } } } printf("%lld\n",mincost); return 0; }
B. Duff in Love
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Duff is in love with lovely numbers! A positive integer x is called lovely if
and only if there is no such positive integera > 1 such that a2 is
a divisor of x.
![](http://codeforces.com/predownloaded/01/2d/012dd215c8c4fe26618c1deac16b705d6e91c093.png)
Malek has a number store! In his store, he has only divisors of positive integer n (and he has all of them). As a birthday present,
Malek wants to give her a lovely number from his store. He wants this number to be as big as possible.
Malek always had issues in math, so he asked for your help. Please tell him what is the biggest lovely number in his store.
Input
The first and only line of input contains one integer, n (1 ≤ n ≤ 1012).
Output
Print the answer in one line.
Sample test(s)
input
10
output
10
input
12
output
6
Note
In first sample case, there are numbers 1, 2, 5 and 10 in the shop. 10 isn't divisible by any perfect square, so 10 islovely.
In second sample case, there are numbers 1, 2, 3, 4, 6 and 12 in the shop. 12 is divisible by 4 = 22,
so 12 is notlovely, while 6 is indeed lovely.
题意:给定数n,求一个n的最大满足要求的因子。
思路:将n的素因子想乘即可
#include<iostream> #include<cmath> using namespace std; int main() { long long int n,temp=1; cin >> n; for (int i = 2; i <= sqrt(double(n)); i++) { if (n%i == 0) { temp *= i; while (n%i == 0) { n /= i; } } } cout << temp*n << endl; //system("pause"); return 0; }
C. Duff and Weight Lifting
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Recently, Duff has been practicing weight lifting. As a hard practice, Malek gave her a task. He gave her a sequence of weights. Weight of i-th
of them is 2wi pounds.
In each step, Duff can lift some of the remaining weights and throw them away. She does this until there's no more weight left. Malek asked her to minimize the number of steps.
![](http://codeforces.com/predownloaded/05/1c/051c344e6b9eb78f1f88fc810efa56b75a0ee02a.png)
Duff is a competitive programming fan. That's why in each step, she can only lift and throw away a sequence of weights 2a1, ..., 2ak if
and only if there exists a non-negative integer x such that 2a1 + 2a2 + ... + 2ak = 2x,
i. e. the sum of those numbers is a power of two.
Duff is a competitive programming fan, but not a programmer. That's why she asked for your help. Help her minimize the number of steps.
Input
The first line of input contains integer n (1 ≤ n ≤ 106),
the number of weights.
The second line contains n integers w1, ..., wn separated
by spaces (0 ≤ wi ≤ 106 for
each 1 ≤ i ≤ n), the powers of two forming the weights values.
Output
Print the minimum number of steps in a single line.
Sample test(s)
input
5 1 1 2 3 3
output
2
input
4 0 1 2 3
output
4
Note
In the first sample case: One optimal way would be to throw away the first three in the first step and the rest in the second step. Also, it's not possible to do it in one step because their sum is not a power of two.
In the second sample case: The only optimal way is to throw away one weight in each step. It's not possible to do it in less than 4 steps because there's no subset of weights with more than one weight and sum equal to a power of two.
题意:给定n个哑铃,每个重量为2^n[i] 每次可以拿起若干个,但是这若干个要满足加起来的和是2的次方,问最少需要几次能举完这些哑铃
思路:题目的意思相当于两个重量为2^i的哑铃组合在一起变成2^i+1。只要一直组合,直到不能组成那么算一步,然后剩下的数继续次操作
用优先队列很好解决。
#include<iostream> #include<vector> #include<queue> #include<stdio.h> using namespace std; const int MAXN=1000005; struct cmp { bool operator ()(int &a,int &b) { return a>b; } }; int main() { int n,val; scanf("%d",&n); priority_queue<int,vector<int>,cmp> Q; for(int i=0;i<n;i++) { scanf("%d",&val); Q.push(val); } int step=0; while(!Q.empty()) { int head,next; head=Q.top(); Q.pop(); if(Q.empty()) { step++; break; } next=Q.top(); Q.pop(); if(head==next) { Q.push(head+1); } else { step++; Q.push(next); } } printf("%d\n",step); return 0; }
D. Duff in Beach
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
While Duff was resting in the beach, she accidentally found a strange array b0, b1, ..., bl - 1 consisting
of l positive integers. This array was strange because it was extremely long, but there was another (maybe shorter) array,a0, ..., an - 1 that b can
be build from a with formula: bi = ai mod n where a mod b denoted
the remainder of dividing aby b.
![](http://espresso.codeforces.com/3796f0b777ac9dbea8c163d08f34e94137182a19.png)
Duff is so curious, she wants to know the number of subsequences of b like bi1, bi2, ..., bix (0 ≤ i1 < i2 < ... < ix < l),
such that:
1 ≤ x ≤ k
For each 1 ≤ j ≤ x - 1,
![](http://codeforces.com/predownloaded/0d/7c/0d7c0d743694764e696d274aa6aaa2b8d2911497.png)
For each 1 ≤ j ≤ x - 1, bij ≤ bij + 1.
i.e this subsequence is non-decreasing.
Since this number can be very large, she want to know it modulo 109 + 7.
Duff is not a programmer, and Malek is unavailable at the moment. So she asked for your help. Please tell her this number.
Input
The first line of input contains three integers, n, l and k (1 ≤ n, k, n × k ≤ 106 and 1 ≤ l ≤ 1018).
The second line contains n space separated integers, a0, a1, ..., an - 1 (1 ≤ ai ≤ 109 for
each 0 ≤ i ≤ n - 1).
Output
Print the answer modulo 1 000 000 007 in one line.
Sample test(s)
input
3 5 3 5 9 1
output
10
input
5 10 3 1 2 3 4 5
output
25
Note
In the first sample case,
![](http://espresso.codeforces.com/d9d8fe92937aeef2bcddb9d213e5587f0f950087.png)
.
So all such sequences are:
![](http://codeforces.com/predownloaded/2f/1a/2f1a184d6d2c19fe08dc2ee379b6771f3dda6830.png)
,
![](http://codeforces.com/predownloaded/21/ec/21eca0ea1757903a6ca9da32f69bfd38381c691c.png)
,
![](http://codeforces.com/predownloaded/d7/bc/d7bc28124edccfad2726903c0602a42947790453.png)
,
![](http://codeforces.com/predownloaded/15/00/1500f524726eb4a3990d99eb229acf9680d8eaf0.png)
,
![](http://espresso.codeforces.com/c6db71d99107f752b5caa1ba7e44af1d2302e5db.png)
,
![](http://codeforces.com/predownloaded/4c/94/4c94cceb7f4ccdf9de262f8b435874a222b632cd.png)
,
![](http://codeforces.com/predownloaded/0b/db/0bdb3b2f17e237a6d27930411f1f03f57caaa3e0.png)
,
![](http://codeforces.com/predownloaded/92/55/9255e13e81f8cd8bedad1201420689a698771d33.png)
,
![](http://espresso.codeforces.com/8756896f0d00888771e69f1a9ccddeee19d4a9ab.png)
and
![](http://espresso.codeforces.com/e771f8119b1b8218c875759aa03e38312514c9a2.png)
.
DP
首先这个数在一个块类的相对位置没有关系,只看他的相对大小
然后最后一个块暴力,有点像分块一样
dp[i][j]表示第i个数,长度为j的方案一共有多少种,然后转移就好了
(没写orz,解题思路来自acm吧的讨论贴)
E. Duff in the Army
time limit per test
4 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output
Recently Duff has been a soldier in the army. Malek is her commander.
Their country, Andarz Gu has n cities (numbered from 1 to n)
and n - 1 bidirectional roads. Each road connects two different cities. There exist a unique path between any two cities.
There are also m people living in Andarz Gu (numbered from 1 to m).
Each person has and ID number. ID number ofi - th person is i and
he/she lives in city number ci.
Note that there may be more than one person in a city, also there may be no people living in the city.
![](http://codeforces.com/predownloaded/d3/0b/d30bf401a5e9f5b2c5e8db0f56b736a1ae8d5018.png)
Malek loves to order. That's why he asks Duff to answer to q queries. In each query, he gives her numbers v, u and a.
To answer a query:
Assume there are x people living in the cities lying on the path from city v to
city u. Assume these people's IDs arep1, p2, ..., px in
increasing order.
If k = min(x, a), then Duff should tell Malek numbers k, p1, p2, ..., pk in
this order. In the other words, Malek wants to know a minimums on that path (or less, if there are less than a people).
Duff is very busy at the moment, so she asked you to help her and answer the queries.
Input
The first line of input contains three integers, n, m and q (1 ≤ n, m, q ≤ 105).
The next n - 1 lines contain the roads. Each line contains two integers v and u,
endpoints of a road (1 ≤ v, u ≤ n,v ≠ u).
Next line contains m integers c1, c2, ..., cm separated
by spaces (1 ≤ ci ≤ n for
each 1 ≤ i ≤ m).
Next q lines contain the queries. Each of them contains three integers, v, u and a (1 ≤ v, u ≤ n and 1 ≤ a ≤ 10).
Output
For each query, print numbers k, p1, p2, ..., pk separated
by spaces in one line.
Sample test(s)
input
5 4 5
1 3
1 21 44 5
2 1 4 3
4 5 61 5 25 5 102 3 3
5 3 1
output
1 3
2 2 3
0
3 1 2 41 2
Note
Graph of Andarz Gu in the sample case is as follows (ID of people in each city are written next to them):
![](http://codeforces.com/predownloaded/59/da/59da2d98ad6ffcd148ebab415aeffced4f865b17.png)
E题,树链剖分套线段树,线段树的每个节点维护一个优先队列,队列中存区间中最小的10个值,查询直接维护链上最小a个元素即可
(没写orz,解题思路来自acm吧的讨论贴)
相关文章推荐
- DOM - nodeType 的取值
- 2015年浪潮面试题
- 编程之法:面试和算法
- iOS开发工具-网络封包分析工具Charles
- c3p0数据源项目启动卡死
- 最少拦截系统
- 实习之前及实习时的学习计划
- 关于url传参乱码的解决方法以及文件流下载遇见的问题
- Linux下UDP通讯开发
- 使用AJAX直接调用后台方法
- struts2 之配置拦截器使网站页面在session失效后退回到登录页面
- 【整理】强化学习与MDP
- Android Studio导入第三方类库的方法
- 测试驱动
- Java核心技术第12章(4)
- 【1.Qt之多线程小入门】
- JAVA抽象类与接口的比较
- AFNetwork 作用和用法详解
- 分词驱动的parser vs. meta character驱动
- LeetCode 093 Restore IP Addresses