uva 11729 Commando War
阿新 • • 發佈:2018-12-31
#include <stdio.h> #include <algorithm> #define MAX_NUM 11000 struct node { int b; int j; }; struct node arr[MAX_NUM]; int case_count; int cmp(const void *a, const void *b) { struct node *pa = (struct node*)a; struct node *pb = (struct node*)b; return pb->j - pa->j; //降序排序 } void func(int N) { int start_time, end_time, max; int i; qsort(arr, N, sizeof(struct node), cmp); start_time = 0; max = 0; for(i=0; i<N; i++) { start_time += arr[i].b; end_time = start_time + arr[i].j; if(max < end_time) max = end_time; } printf("Case %d: %d\n", ++case_count, max); } int main(void) { int N, i; while(scanf("%d", &N)) { if(!N) break; for(i=0; i<N; i++) scanf("%d %d", &(arr[i].b), &(arr[i].j)); func(N); } return 0; }
這道題也是貪心演算法,優先安排工作時間長的工作,最後需要的總時間就是最少的。