您的位置:首页 > 其它

sgu 259

2013-04-05 17:38 501 查看
/*
greedy method
sort the leaflets by Li by descending order
let sumTk = Ti + T2 + ..... Tk
answer = max ( sumTk + Lk),  k = 1...n

proofproof by contradiction :
假设最优序列为 a = { 1, 2, 3 ....i, j ... n } (j = i + 1, Li < Lj )
构造序列 b = { 1,2,3 .....j, i ....n }

ans(a) = max {sumT(i-1)+Ti+Li, sumT(i-1)+Ti+Tj+Lj, sumTk+Lk }, k != i, j
ans(b) = max {sumT(i-1)+Tj+Lj, sumT(i-1)+Tj+Ti+Li, sumTk+Lk }, k != i, j

because Li < Lj, so   Ti+Tj+Lj > Tj+Ti+Li
and it is obvious that Ti+Tj+Lj > Tj+Lj

we come to conclusion: ans(a) >= ans(b) , this means a is not the best ans.

*/

#include <stdio.h>
#include <stdlib.h>

#define SIZE 100

typedef struct {
int T;
int L;
}TL;

int cmp(const void* a, const void* b) {
TL* pa = (TL*)a;
TL* pb = (TL*)b;
return (pb->L) -  (pa->L);
}

int main() {
int n, i;
TL tl[SIZE];
scanf("%d", &n);
for (i=0; i<n; ++i) {
scanf("%d", &tl[i].T);
}
for (i=0; i<n; ++i) {
scanf("%d", &tl[i].L);
}

qsort(tl, n, sizeof(TL), cmp);

int s = 0;
int e;
int ans = 0;
for (i=0; i<n; ++i) {
s += tl[i].T;
e = s + tl[i].L;
if (e > ans) {
ans = e;
}
}

printf("%d\n", ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  sgu 259 oj c