【贪心】HDU-1789 Doing Homework again

来源:互联网 发布:淘宝网限时秒杀 编辑:程序博客网 时间:2024/06/06 03:39

Doing Homework again

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Problem Description
Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Every teacher gives him a deadline of handing in the homework. If Ignatius hands in the homework after the deadline, the teacher will reduce his score of the final test. And now we assume that doing everyone homework always takes one day. So Ignatius wants you to help him to arrange the order of doing homework to minimize the reduced score.
 

Input
The input contains several test cases. The first line of the input is a single integer T that is the number of test cases. T test cases follow.
Each test case start with a positive integer N(1<=N<=1000) which indicate the number of homework.. Then 2 lines follow. The first line contains N integers that indicate the deadlines of the subjects, and the next line contains N integers that indicate the reduced scores.
 

Output
For each test case, you should output the smallest total reduced score, one line per test case.
 

Sample Input
333 3 310 5 131 3 16 2 371 4 6 4 2 4 33 2 1 7 6 5 4
 

Sample Output
035
 
————————————————————————————————————————————————————————
前言:真是……连贪心都做不出来。我发现我到现在,都必须依赖题解做题目。不然一道题目都做不出来。我学的到底是什么。。。
思路:要注意,某作业要么能完成,要么就被扣一次分。就扣一次。(题目没有说清楚)
首先按照扣分高低排序。不论怎样,在有限的时间内,选择扣分更高的作业都是明智的。
正确的贪心姿势:对于扣分高的作业,将它安排到最优的位置——消耗掉可以利用的最远的deadline
代码如下:
/*ID: j.sure.1PROG:LANG: C++*//****************************************/#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <ctime>#include <cmath>#include <stack>#include <queue>#include <vector>#include <map>#include <set>#include <string>#include <climits>#include <iostream>#define For(i,x,y) for(int i = x; i < y; i++)#define Mem(f,x) memset(f, x, sizeof(f))#define Sca(x) scanf("%d", &x)#define LL long longusing namespace std;const int INF = 0x3f3f3f3f;/****************************************/const int N = 1111;struct Node {int dead, sco;}work[N];int n;bool vis[N];bool cmp(Node a, Node b){if(a.sco == b.sco) return a.dead < b.dead;return a.sco > b.sco;}int main(){#ifdef J_Sure//freopen("000.in", "r", stdin);//freopen("999.out", "w", stdout);#endifint T;Sca(T);int x;while(T--) {Mem(vis, 0);Sca(n);For(i, 0, n) {Sca(x);work[i].dead = x;}For(i, 0, n) {Sca(x);work[i].sco = x;}sort(work, work+n, cmp);int ans = 0;For(i, 0, n) {int j;for(j = work[i].dead; j > 0; j--) {//必须倒着安排才可以,否则会占用靠前的deadlineif(!vis[j]) {vis[j] = 1; break;}}if(!j) ans += work[i].sco;}printf("%d\n", ans);}return 0;}


0 0
原创粉丝点击