华中程序设计邀请赛1007

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

还是读题的问题,第一遍读题时,没太明白,以为是求最小的总得罚款数。但是第二组simple对不上,所以又重读一遍题,原来是求每任务让最大罚款

尽可能的小,这样的话就很明了了,通过依次排序,就得到结果了。对了,还要注意这题int型装不下,要用——int64.

代码如下:

#include"stdio.h"
#include"stdlib.h"


typedef struct course
{
    int c,d;
}node;


int cmp(const void *a,const void *b)
{
    node aa=*(node*)a,bb=*(node*)b;
    return aa.d-bb.d;
}

node cour[100005];
int main( )
{
    int t,n,i,count=1;
    __int64 sum,max;
    scanf("%d",&t);
    while(t--)
    {
        max=sum=0;
        scanf("%d",&n);
        for(i=0;i<n;i++)
            scanf("%d%d",&cour[i].c,&cour[i].d);
        qsort(cour,n,sizeof(node),cmp);
        for(i=0;i<n;i++)
        {
            sum+=cour[i].c;
            if(sum>cour[i].d&&sum-cour[i].d>max)
                max=sum-cour[i].d;
        }
        printf("Case %d: %I64d\n",count++,max);
    }
    return 0;
}
        
原文地址:https://www.cnblogs.com/chaosheng/p/2450933.html