UVA 11729 Commando War

链接: 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
", ++cas, res);
    }
    return 0;
}


原文地址:https://www.cnblogs.com/lytwajue/p/6740162.html