UVa 11729 Commando War

来源:互联网 发布:魔法师雾化器做芯数据 编辑:程序博客网 时间:2024/05/17 22:54

链接:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2829


/*   刘汝佳《训练指南》第一章 例题2   UVa  11729  Commando War    贪心 , 用“相邻交换法”证明正确性                      ------ by shuangde */#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#define REP(i,n) for(int i=0; i<(n); ++i)using namespace std;const int MAXN = 10000;struct node{    int b, j;    friend bool operator<(const node&a,const node&b){        return a.j > b.j;    }}arr[MAXN];int main(){    int n,cas=0;;    while(~scanf("%d",&n) && n){        printf("Case %d: ", ++cas);        REP(i, n) scanf("%d%d",&arr[i].b, &arr[i].j);        sort(arr, arr+n);        int maxx=0, tt=0;        REP(i, n){            tt += arr[i].b;            if(arr[i].j + tt > maxx)                 maxx = arr[i].j + tt;        }        printf("%d\n", maxx);    }    return 0;}


原创粉丝点击