• Valera and Fruits


    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 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.

    Sample Input

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

    Hint

    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.

    大意:摘水果,每种水果的成熟期为两天,单纯暴力,就把i与时间相比,如果在这个时间内,就有两种情况,一种是摘得水果不够,那就摘下一天的,另一种是已经满了,不过在每次计算后,都要把值减去已经摘了的。原本一直看不出哪儿错了- -结果回寝室一眼看出了,还是得歇一会儿再看清楚!

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    struct edge{
        int d;
        int num;
    }a[5000];
    bool cmp(edge i, edge j){
        return i.d < j.d;
    }
    int main()
    {
     int n,m;
     while(~scanf("%d%d",&n,&m)){
            for(int i = 1; i <= n ; i++)
            scanf("%d%d", &a[i].d,&a[i].num);
          sort(a+1, a+n+1,cmp);
          int ans = 0;
          for(int i = 1; i <= 3100 ; i++){
                int m1 = m;
             for(int j = 1; j <= n ;j++){
                    if(i-a[j].d >= 0&&i - a[j].d <= 1){
                        if(a[j].num <= m1){
                            ans += a[j].num;
                            m1 -= a[j].num;
                            a[j].num = 0;
                        }
                       else {
                            ans += m1;
                            a[j].num -= m1;
                            m1 = 0;
                            break;
                       }
                    }
                 }
             }
          printf("%d
    ",ans);
          }
          return 0;
     }
    View Code
  • 相关阅读:
    利用jmSlip写一个移动端顶部日历选择组件
    JS写的排序算法演示
    jmSlip WEB前端滑屏组件
    如何:使用 Visual Basic 编写基于 Unity3D 的计算器
    验证 .NET 4.6 的 SIMD 硬件加速支持的重要性
    VB 2015 的 闭包(Closure)
    VS "15" 预览 5 中 VB 15 新增的功能
    演练:使用Xamarin.Forms开发产品介绍性质的应用(VB版)
    UWP游戏防内存修改器的方法
    优化win2d实现的萤火虫粒子效果
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4344150.html
Copyright © 2020-2023  润新知