Codeforces Round #252(Div. 2) 441B. Valera and Fruits 贪心

本文介绍了Valera如何在他的花园中优化水果采摘,以最大限度地收集水果。Valera每天最多能摘v个水果,每棵树的水果在特定两天内会枯萎。通过贪心策略,按果实成熟日期升序排列并合并同一天成熟的果树,可以找到最大收集量。在给定的示例中,展示了如何实现这一策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B. Valera and Fruits
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 thanv 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棵果树,第i棵的产量为bi,收获时间为ai和ai+1两天,过期不能收获,问最多能收获多少果实。

题解:

当然先收获早成熟的,所以我们按a升序排序,以此往后收。

注意,如果两个果树a相等,就把两棵树合并,因为果实是没有区别的,所以合并处理就好了。

/****************
*PID:441b div2
*Auth:Jonariguez
*****************
*/
#define lson k*2,l,m
#define rson k*2+1,m+1,r
#define rep(i,s,e) for(i=(s);i<=(e);i++)
#define For(j,s,e) For(j=(s);j<(e);j++)
#define sc(x) scanf("%d",&x)
#define In(x) scanf("%I64d",&x)
#define pf(x) printf("%d",x)
#define pfn(x) printf("%d\n",(x))
#define Pf(x) printf("%I64d",(x))
#define Pfn(x) printf("%I64d\n",(x))
#define Pc printf(" ")
#define PY puts("YES")
#define PN puts("NO")
#include <stdio.h>
#include <string.h>
#include <string>
#include <math.h>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef int Ll;
Ll quick_pow(Ll a,Ll b,Ll MOD){a%=MOD;Ll res=1;while(b){if(b&1)res=(res*a)%MOD;b/=2;a=(a*a)%MOD;}return res;}

const int maxn=3000+10;
int vis[maxn];
struct pp{
    int a,b;
}t[maxn],s[maxn];

int cmp(const pp &x,const pp &y){
    if(x.a==y.a)
        return x.b>y.b;
    return x.a<y.a;
}

int main()
{
    int i,j,n,v;
    while(scanf("%d%d",&n,&v)!=EOF){
        for(i=1;i<=n;++i){
            sc(t[i].a);sc(t[i].b);
        }
        sort(t+1,t+n+1,cmp);
        s[1]=t[1];
        int last=1,tot=1;
        for(i=2;i<=n;i++){
            if(t[i].a==s[last].a){
                s[last].b+=t[i].b;
            }else {
                s[++tot]=t[i];
                last=tot;
            }
        }
        int res=0,now=1;
        last=0;
        j=1;s[tot+1].a=3333;
        for(now=1;now<=3001;now++){
            if(now==s[j].a){
                int temp=0;
                if(last){
                    if(v>=last){
                        res+=last;
                        temp=v-last;
                        if(temp<=s[j].b){
                            res+=temp;last=s[j].b-temp;
                        }else {
                            res+=s[j].b;last=0;
                        }
                    }else {
                        res+=v;last=s[j].b;
                    }
                }else {
                    res+=min(v,s[j].b);
                    last=max(0,s[j].b-v);
                }
                j++;
            }else {
                res+=min(v,last);last=0;
            }
        }
        pfn(res);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值