Doing Homework again(HDU 1789)

Doing Homework again

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7299    Accepted Submission(s): 4311


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
   
   
   
   
3 3 3 3 3 10 5 1 3 1 3 1 6 2 3 7 1 4 6 4 2 4 3 3 2 1 7 6 5 4
 

Sample Output
   
   
   
   
0 3 5
 
题意:
有n项作业,并告诉你每项作业的最后完成期限,如果在最后期限内没有完成,则会扣除你一些分数(在期限过后完成是没有意义的),每项作业都要做一天,问你该如何安排写作业的顺序,求最少会扣除的分数.


分析:
虽然觉得想出来后并不难,但还是花了一点时间的. 首先按照最后期限从小到大排序, 然后就有点像最长上升子序列一样操作了. 从前往后操作,看我将要安排的时间是否在这项作业的最后期限内, 如果在, 则直接将它的分数赋值到另外一个数组中, 如果不在, 则遍历那个数组有没有分数比它低的, 如果有, 则替换那个数组中的分数(而那个被替换的就是一定会被扣分的), 否则它就是一定会被扣分的. 把扣分的总和求出就是最少会被扣的分数. 证明可以看最长上升子序列的(有合适的元素则添加, 没有则看能否替换前面的元素而使结果可能更佳, 不会跟坏)
.

code:
/*************************************************************************
    > File Name: greedy.cpp
    > Author: tj
    > Mail: [email protected]
    > Created Time: 2015年01月05日 星期一 11时26分40秒
 ************************************************************************/

#include<stdio.h>
#include<algorithm>
using namespace std;
#define INF 1<<30
struct P
{
	int deadline, score;
}p[1010];

bool cmp(P c, P d)
{
	if(c.deadline != d.deadline) return c.deadline < d.deadline;
	return c.score > d.score;
}

int main()
{
	int T, n, a[1010];

	scanf("%d", &T);
	while(T--)
	{
		scanf("%d", &n);
		for(int i=1; i<=n; i++)
			scanf("%d", &p[i].deadline);
		for(int i=1; i<=n; i++)
			scanf("%d", &p[i].score);
		sort(p+1,p+n+1,cmp);
		int size = 0, ans = 0;
		for(int i=1; i<=n; i++)
		{
			if(p[i].deadline <= size)
			{
				sort(a+1,a+size+1);
				if(p[i].score < a[1]) ans += p[i].score;
				else ans += a[1], a[1] = p[i].score;
			}
			else a[++size] = p[i].score;
		}
		printf("%d\n", ans);
	}
	return 0;
}


你可能感兴趣的:(贪心)