分析:
采用贪心策略,先将分数从高到低排序,每次都先保证分数最高的作业能在规定时间内做完。
代码:
#include<stdio.h> #include<stdlib.h> #include<string.h> int v[1010]; struct node { int t; int num; } g[1010],temp; int main() { int t,n,i,j; while(~scanf("%d",&t)) { while(t--) { int sum = 0; memset(v,0,sizeof(v)); scanf("%d",&n); for (i = 0; i < n; i ++) scanf("%d",&g[i].t); for (i = 0; i < n; i ++) { scanf("%d",&g[i].num); sum+=g[i].num; } for (i = 0; i < n-1; i ++) for (j = 0; j < n-1-i; j ++) if (g[j].num < g[j+1].num) { temp = g[j]; g[j] = g[j+1]; g[j+1] = temp; } for (i = 0; i < n; i ++) { if (!v[g[i].t]) { v[g[i].t] = 1; sum-=g[i].num; } else { int p = g[i].t-1; while(p >= 1) { if (!v[p]) { v[p] = 1; sum-=g[i].num; break; } else p--; } } } printf("%d\n",sum); } } return 0; }