C - Valera and Fruits

Posted 霜雪千年

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C - Valera and Fruits相关的知识,希望对你有一定的参考价值。

Problem description

Valera loves his garden, where n fruit trees grow.

This year he will enjoy a great harvest! On the i-th tree bi fruit grow, they will ripen on a day number ai. Unfortunately, the fruit on the tree get withered, so they can only be collected on day ai and day ai?+?1 (all fruits that are not collected in these two days, become unfit to eat).

Valera is not very fast, but there are some positive points. Valera is ready to work every day. In one day, Valera can collect no more than v fruits. The fruits may be either from the same tree, or from different ones. What is the maximum amount of fruit Valera can collect for all time, if he operates optimally well?

Input

The first line contains two space-separated integers n and v (1?≤?n,?v?≤?3000) — the number of fruit trees in the garden and the number of fruits that Valera can collect in a day.

Next n lines contain the description of trees in the garden. The i-th line contains two space-separated integers ai and bi (1?≤?ai,?bi?≤?3000) — the day the fruits ripen on the i-th tree and the number of fruits on the i-th tree.

Output

Print a single integer — the maximum number of fruit that Valera can collect.

Examples

Input
2 3
1 5
2 3
Output
8
Input
5 10
3 20
2 20
1 20
4 20
5 20
Output
60

Note

In the first sample, in order to obtain the optimal answer, you should act as follows.

  • On the first day collect 3 fruits from the 1-st tree.
  • On the second day collect 1 fruit from the 2-nd tree and 2 fruits from the 1-st tree.
  • On the third day collect the remaining fruits from the 2-nd tree.

In the second sample, you can only collect 60 fruits, the remaining fruit will simply wither.

解题思路:题目的意思就是有n棵树,a,b分别表示一棵树第a天成熟和该树树上有b个果实,并且只能在第a天或第a+1天这两天摘其树上的果实,超过这两天就不能再摘该树树上的果实(已发霉)。求农夫可以摘的最大果实数目。按照提示给的规则,刚开始做法是用结构体数组,但发现如果成熟的天数是断续的,这时候就会覆盖当前果树本该要摘的果实数目,从而少摘了一些果实。以下代码中的结构体数组按果树的成熟天数从小到大排完序后其连续下标存放的是成熟天数(成熟天数可能有断续即不连续)。举个栗子:n=4,v=10;(这里不举例有相同成熟天数的一些果树)

3 20

1 20 

4 20 

5 20

此样例正确输出应为60,但以下代码的输出为50(错误),因此不能这样子做。过程比较简单,可以自己模拟一下。

贴一下错误代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 3005;
 4 struct NODE{
 5     int dayth,num;
 6 }node[maxn];
 7 bool cmp(NODE x,NODE y){return x.num<y.num;}
 8 int main(){
 9     int n,v,x,y,tmp,sum=0,k=0;bool flag;
10     cin>>n>>v;
11     for(int i=0;i<maxn;++i)node[i].num=0;
12     for(int i=1;i<=n;++i){
13         cin>>x>>y;flag=false;
14         for(int j=0;j<k;++j)
15             if(node[i].dayth==x){node[i].num+=y;flag=true;break;}
16         if(!flag){node[k].dayth=x;node[k++].num=y;}
17     }
18     sort(node,node+k,cmp);
19     if(node[0].num>=v){sum+=v;node[0].num-=v;}
20     else {sum+=node[0].num;node[0].num=0;}
21     for(int i=1;i<=k;++i){
22         if(node[i-1].num>=v)sum+=v;
23         else{
24             sum+=node[i-1].num;tmp=v-node[i-1].num;
25             if(node[i].num>=tmp){sum+=tmp;node[i].num-=tmp;}
26             else {sum+=node[i].num;node[i].num=0;}
27         }
28     }
29     cout<<sum<<endl;
30     return 0;
31 }

我们定义了一个数组r[i]=value,其下标i表示第i天成熟,value为果实数目。注意某些树成熟的天数可能是相同的,因而要将它们的果实累加。枚举每一天是否有成熟的果实可以摘,并且按规则摘取,这样就不会有覆盖,即发生少摘的情况。

AC代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 3005;
 4 int main(){
 5     int n,v,x,y,tmp,sum=0,r[maxn];//x,y分别表示某棵树成熟的天数和该树树上的果实数量
 6     cin>>n>>v;
 7     memset(r,0,sizeof(r));//注意清0
 8     for(int i=1;i<=n;++i){cin>>x>>y;r[x]+=y;}//可能有一些树成熟的时间相同,所以要将其果实相加
 9     for(int i=1;i<maxn;++i){//从1开始枚举
10         if(r[i-1]>=v)sum+=v;//如果上一棵树可以摘的果实数目不小于v,则直接摘掉v个,并且摘完后上一棵树已经过期了,此后不能再摘了
11         else{
12             sum+=r[i-1];tmp=v-r[i-1];//否则就直接摘掉上一棵树剩下的果实
13             if(r[i]>=tmp){sum+=tmp;r[i]-=tmp;}//如果当前这个树可以摘的果实还大于一天剩下可摘的果实tmp,就摘掉tmp个
14             else{sum+=r[i];r[i]=0;}//否则就直接摘掉当前这棵树上的果实
15         }
16     }
17     cout<<sum<<endl;
18     return 0;
19 }

 

以上是关于C - Valera and Fruits的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces 369 C Valera and Elections

[CF441E]Valera and Number

[CF369E]Valera and Queries_离线_树状数组

Codeforces441A_Valera and Antique Items(水题)

Codeforces 441C Valera and Tubes

A. Valera and X1000 / 模拟