UVA 11292 和 UVA 11729

来源:互联网 发布:you don t know js 编辑:程序博客网 时间:2024/05/16 06:37

最近开始刷刘汝佳的大白书,这两道妥妥的贪心题目,第一道很快就知道怎么做,第二道稍微分析一下也可以知道是用贪心来写,先把完成任务时间长的先安排。第一题的代码就不附上了,第二题代码如下:#include <iostream>#include <cstring>#include <string>#include <cstdio>#include <algorithm>using namespace std;const int maxn=10000+10;int t;struct job {    int j,b;} a[maxn];bool cmp(job k1,job k2) {    return k1.j>k2.j;}int main() {    int tcase=0;    while(scanf("%d",&t),t) {        for(int i=0; i<t; i++) {            scanf("%d%d",&a[i].b,&a[i].j);        }        sort(a,a+t,cmp);        cout<<"###########"<<endl;        for(int i=0; i<t; i++)        {            cout<<a[i].b<<" "<<a[i].j<<endl;        }        cout<<"###########"<<endl;        int s=0;        int ans=0;        for(int i=0; i<t; i++) {            s+=a[i].b;            ans=max(ans,s+a[i].j);        }        printf("Case %d: %d\n",++tcase,ans);    }    return 0;}


0 0