HDU 1789

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

大致题意:

  不交上作业就扣分,每天只能做一个作业,给你每个作业的deadline和对应分数,问你怎样扣分最少;

解题思路:

  贪心,先做分数多的,再做分数少的,若发现deadline之前的日期全被占满了,那就扣分呗;

  以下有两个代码:(可能第二个看起来短一点) 

  代码一:

    按照时间处理,从最后一天开始往前分配,依次分配给满足deadline的分数最大的作业;

    最后扫一遍,没被分配的都扣分;

  代码二:

    按照作业处理,按 分数由大到小,同分数deadline由小到大 排序;

    然后从 分数最大的作业 开始分配,分配给离 deadline 最近的没有被占的日期;

    如果日期全被占了,就扣分咯;

 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 struct P
 5 {
 6     int e;//deadline 
 7     int s;//score
 8 }s[1005];
 9 bool cmp(P x,P y){return x.e<y.e;}
10 int main()
11 {
12     int t,n;
13     cin>>t;
14     while(t--)
15     {
16         int flag[1005]={0},last=0,ans=0;
17         cin>>n;
18         for(int i=0;i<n;i++)
19         {
20             cin>>s[i].e;
21             if(s[i].e>last) last=s[i].e;
22         }
23         for(int i=0;i<n;i++)
24             cin>>s[i].s;
25 
26         for(int i=last;i>0;i--)//时间从后往前 
27         {
28             int max=0,temp;
29             for(int j=0;j<n;j++)
30             {
31                 if(s[j].e>=i && flag[j]==0 && max<s[j].s)//满足条件的最大 
32                 {
33                     temp=j;
34                     max=s[j].s;
35                 }
36             }
37             flag[temp]=1;
38         }
39         for(int i=0;i<n;i++)
40             if(flag[i]==0)
41                 ans+=s[i].s;
42         cout<<ans<<endl;
43     } return 0;
44 }
 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <cstring>
 5 using namespace std;
 6 struct data{int time,score;}a[100001];
 7 int day[100001];
 8 int cmp(data a,data b){return (a.score>b.score||a.score==b.score&&a.time<b.time);}
 9 int main(){
10     int t,n,i;
11     cin>>t;
12     while(t--){
13         cin>>n;
14         memset(day,0,sizeof(day));
15         for(i=1;i<=n;i++) cin>>a[i].time;
16         for(i=1;i<=n;i++) cin>>a[i].score;
17         sort(a+1,a+n+1,cmp);
18         int ans=0;
19         for(i=1;i<=n;i++){//分配作业 
20             int j; 
21             for(j=a[i].time;j>=1;j--)
22                 if(!day[j]) {day[j]=1;break;}
23             if(j==0) ans+=a[i].score;
24         } cout<<ans<<endl; 
25     } return 0;
26 }
我自倾杯,君且随意
原文地址:https://www.cnblogs.com/nicetomeetu/p/5158843.html