题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1789
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
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项作业需要完成,每项作业都有限期,如果不在限期内完成作业,期末考就会被扣相应的分数。
给出测试数据T表示测试数,每个测试以N开始(N为0时结束),接下来一行有N个数据,分别是作业的限期,再有一行也有N个数据,分别是若不完成该作业会被扣的分数。
求他最佳的作业顺序后被扣的最小的分数(每个作业费时一天)。
题解:
第一步按照reduced_score从大到小排序,如果reduced_score相等,则按照deadline从小到大排。
然后开始选择,让当前的课排在其deadline上面,如果这一天已经被占用了,
那么就往前循环,有位置了就安排,没了就sum+=reduced_score
1 #include<cstdio> 2 #include<algorithm> 3 using namespace std; 4 typedef struct{ 5 int deadline; 6 int reduced_score; 7 }type; 8 type homework[1003]; 9 int day[1100]; 10 bool cmp(type a,type b) 11 { 12 if(a.reduced_score == b.reduced_score) return a.deadline < b.deadline; 13 return a.reduced_score > b.reduced_score; 14 } 15 int main() 16 { 17 int t,n; 18 scanf("%d",&t); 19 while(t--){ 20 scanf("%d",&n); 21 int deadline_max=-1; 22 for(int i=1;i<=n;i++){ 23 scanf("%d",&homework[i].deadline); 24 if(homework[i].deadline > deadline_max) deadline_max=homework[i].deadline; 25 } 26 for(int i=1;i<=n;i++) scanf("%d",&homework[i].reduced_score); 27 sort(homework+1,homework+n+1,cmp); 28 29 int reduced_score_sum=0; 30 memset(day,0,sizeof(day)); 31 for(int i=1;i<=n;i++){ 32 int j; 33 for(j=homework[i].deadline;j>=1;j--){ 34 if(day[j]==0){ 35 day[j]=1; 36 break; 37 } 38 } 39 if(j==0) reduced_score_sum+=homework[i].reduced_score; //反正分也扣了,一百年之后再做也没事,就不用去管哪天做了 40 } 41 printf("%d ",reduced_score_sum); 42 } 43 }
我觉得这才是名副其实的贪心……所有的作业都想办法拖到最后一天做,没办法了就往前算算有没有哪天可以做的,实在不行就只能扣分了……