HDU 2111 Saving HDU

http://acm.hdu.edu.cn/showproblem.php?pid=2111

直接贪心

View Code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
struct node{
    int p,m;
}kk[110];
int cmp(const void*a,const void*b)
{
    struct node*c=(struct node*)a;
    struct node*d=(struct node*)b;
    return d->p-c->p;
}
int main()
{
    int v,n,i,cnt,pp;
    while(scanf("%d",&v),v)
    {
        scanf("%d",&n);
        for(i=0;i<n;i++)
            scanf("%d%d",&kk[i].p,&kk[i].m);
        qsort(kk,n,sizeof(struct node),cmp);
        cnt=pp=0;
        while(1){
            pp+=kk[cnt].m*kk[cnt].p;
            v-=kk[cnt].m;
            if(v<0)
            {
                pp-=kk[cnt].m*kk[cnt].p;
                v+=kk[cnt].m;
                pp+=v*kk[cnt].p;
                v=0;
            }
            cnt++;
            if(cnt==n||v==0)break;
        }
        printf("%d\n",pp);
    }
    return 0;
} 
原文地址:https://www.cnblogs.com/xiaohongmao/p/2438652.html