hdu 1789 Doing Homework again

                                                  Doing Homework again

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


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
 


Author
lcy
 


Source
2007省赛集训队练习赛(10)_以此感谢DOOMIII



题目描述:
      给出课程数目,科目交作业的最后期限以及若到期为交上作业将要扣除相对应的科目分数,求怎样使扣除的分数最少。
思路:
       首先排一下序,然后从前往后遍历,若哪一科不能按时交上,则从开头到这一位置找到扣除最少的科目,用这一天做现在的科目。

#include<stdio.h>
#include<algorithm>
using namespace std;

struct A
{
	int time;
	int score;
	int flag; 
}a[1005];
bool cmp(A a,A b)
{
	if(a.time != b.time)   //只要最后时间不同,按时间从小到大排
		return a.time < b.time;
	else
		return a.score > b.score;  //否则按分数从大到小排 
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n,sum = 0;    //sum记录的是要减去的分数 
		scanf("%d",&n);
		for(int i = 1;i <= n;i++)
			scanf("%d",&a[i].time);
		for(int i = 1;i <= n;i++)
		{
			scanf("%d",&a[i].score);
			a[i].flag = 1;   //没被减的标为 1 
		}	
		sort(a + 1,a + n + 1,cmp);  //输入时是从一开始输的,所有这里排序时开始最后下标都要加一 
		int l = 1;
		for(int i = 1;i <= n;i++)
		{	
			if(l <= a[i].time) 
			{
				l++;
				continue; 
			}
			
			int ss = a[i].score,pos = i;  //不能在当前情况下完成下
			for(int j = 1;j <= i;j++)  // 从第一个到该位置遍历,找分数最小的,不能找已经减过的 
				if(a[j].score < ss && a[j].flag)    
				{				
					ss = a[j].score;      //ss记录最小的分数 
					pos = j;         //pos记录当前没有扣除分数的且分数最小下标 
				}
			sum += ss;
			a[pos].flag = 0;	//扣过分数的标为 0 
		}
		printf("%d\n",sum);
	}
	return 0;
} 

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