您的位置:首页 > 其它

UVA 11729 Commando War

2017-04-20 18:18 405 查看
链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2829
不摘抄题目了;

解析:

简单贪心思想。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#define MAXN 20005
#define RST(N)memset(N, 0, sizeof(N))
using namespace std;

typedef struct Node_ {
int b, j;
}Node;

Node N[MAXN];
int n;

int cmp(const void *a, const void *b)
{
Node *p1 = (Node *)a;
Node *p2 = (Node *)b;
if(p1->j != p2->j) return p2->j - p1->j;
else return p2->b - p1->b;
}

int max(int x, int y) { return x>y ? x:y; }

int main()
{
int cas = 0;
while(~scanf("%d", &n) && n) {
for(int i=0; i<n; i++) {
scanf("%d %d", &N[i].b, &N[i].j);
}
qsort(N, n, sizeof(Node), cmp);
int res = 0, cnt = 0;
for(int i=0; i<n; i++) {
cnt += N[i].b;
res = max(res, cnt+N[i].j);
}
printf("Case %d: %d\n", ++cas, res);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: