首页 > 代码库 > Hdu 1789 Doing Homework again

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): 6538    Accepted Submission(s): 3900


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
035
 

 

Author
lcy
 

 

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

 

Recommend
lcy

 

贪心.

贪心策略:按扣分降序排序,对每个作业进行判断,是否能够在限定的最后一天完成,如果限定的最后一天已经有作业要完成,(如第一个案例:先判断扣10分的作业能不能在第三天完成,此时第三天没有安排作业. 那么就安排进去.接下来再判断第二个作业 也就是扣分为5的作业,第三天已经安排了扣分为10的作业,所以第三天不能再安排作业。往前找,第二天没有安排作业,所以安排到第二天。以此类推)那么继续往前寻找,若前面都不能安排此作业,那么就算这个作业不能完成.

代码如下:

 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 using namespace std; 6 #define MAX 1001 7 struct node 8 { 9     int day;10     int score;11 };12 struct node num[MAX];13 bool flag[MAX];14 int n,sum;15 bool cmp(struct node x,struct node y)16 {17     if(x.score==y.score) return x.day<y.day;18     return x.score>y.score;19 }20 void out()21 {22     for(int i=1;i<=n;i++)23         cout<<flag[i]<<" ";24     cout<<endl;25 }26 void init()27 {28     memset(num,0,sizeof(num));29     memset(flag,true,sizeof(flag));30     sum=0;31 }32 void read()33 {34     int i;35     scanf("%d",&n);36     for(i=0;i<n;i++)37         scanf("%d",&num[i].day);38     for(i=0;i<n;i++)39     {40         scanf("%d",&num[i].score);41         sum+=num[i].score;42     }43     sort(num,num+n,cmp);44 }45 void cal()46 {47     int ans=sum;48     int i,j;49     for(i=0;i<n;++i)50     {51         if(flag[num[i].day])52         {53             ans-=num[i].score;54             flag[num[i].day]=!flag[num[i].day];55         }56         else57         {58             for(j=num[i].day-1;j>=1;j--)59                 if(flag[j])60                 {61                     flag[j]=!flag[j];62                     ans-=num[i].score;63                     break;64                 }65         }66     }67     printf("%d\n",ans);68 }69 void solve()70 {71     init();72     read();73     cal();74 }75 76 int main()77 {78     int t;79     scanf("%d",&t);80     while(t--)81     {82         solve();83     }84     return 0;85 }
View Code