HDU 3090 (贪心)

题目链接http://acm.hdu.edu.cn/showproblem.php?pid=3090

题目大意:一共n段路。每段路每千米都会被抢劫一定数量,可以雇佣武士护卫m千米。问最少被抢劫数量。

解题思路

题目英文很啰嗦。一开始还以为是n段路可选的,其实是都要走完。

那么直接按照抢劫数量从大到小排序、护卫即可。雇佣兵跑了之后,直接扣。

特判n=0时,ans=0。

#include "cstdio"
#include "algorithm"
using namespace std;
#define LL long long
struct Road
{
    int d,p;
    bool operator < (const Road &a) const {return p>a.p;}
}r[10005];
int main()
{
    //freopen("in.txt","r",stdin);
    LL n,m;
    while(scanf("%I64d%I64d",&n,&m)!=EOF)
    {
        if(n==0&&m==0) break;
        if(n==0) printf("0\n");
        for(int i=0;i<n;i++) scanf("%d%d",&r[i].d,&r[i].p);
        sort(r,r+n);
        LL ans=0,ret=m;
        bool flag=true;
        for(int i=0;i<n;i++)
        {
            if(!flag) {ans+=(r[i].d*r[i].p);continue;}
            ret-=r[i].d;
            if(ret>=0) continue;
            else
            {
                ans+=((-ret)*r[i].p);
                flag=false;
            }
        }
        printf("%I64d\n",ans);
    }
}

 

posted @ 2015-05-15 11:24  Physcal  阅读(355)  评论(0编辑  收藏  举报