zoukankan      html  css  js  c++  java
  • Valera and Fruits

    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.

    Sample test(s)
    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.



    题目比較简单,但有两个easy忽略的地方,代码中已经标注了。

    #include <iostream>
    #include <cstdio>
    using namespace std;
    int t[3333];
    int main()
    {
        int n,v,m,i,j,a,b;
        cin>>n>>v;
        int minn=9999,maxx=-1;
        for(i=0;i<n;i++)
        {
            scanf("%d %d",&a,&b);
            t[a]+=b;//可能多棵树在同一天成熟
            maxx=max(maxx,a);
            minn=min(minn,a);
        }
        int sum=0,tmp=0;
    
        for(i=minn;i<=maxx+1;i++)
        {
            if(tmp>=v)
            {
                tmp=t[i];
                sum+=v;
            }
            else
            {
                if(tmp+t[i]<=v)
                {
    
    
                    sum+=tmp+t[i];
                    tmp=0;//这里tmp位置非常重要,之前放在sum前面,就悲剧了
                }
                else
                {
                    tmp=t[i]-(v-tmp);
                    sum+=v;
                }
            }
          //  cout<<tmp<<" "<<sum<<endl;
        }
        cout<<sum<<endl;
        return 0;
    }
    


  • 相关阅读:
    IDL读取TXT文件并写入二维数组中【转】
    远程连接ArcSDE
    Silverlight项目启动出现System.NullReferenceException未将对象引用到对象实例
    ENVI扩展工具:HDF5 Browser
    READF: End of file encountered. Unit: 100
    ENVI4.8下从两幅分类结果的栅格图中计算土地利用类型转换矩阵
    IDL中去掉数组中相同的元素方法
    利用IDL程序自动添加ENVI菜单【转】
    WIN7远程桌面连接知识
    对COM组件的调用返回了错误"HRESULT E_FAIL”的错误分析(c#与IDL混合编程)转
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4007284.html
Copyright © 2011-2022 走看看