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
0
3
5
Author
lcy
Source
Recommend
lcy
贪心.
贪心策略:按扣分降序排序,对每个作业进行判断,是否能够在限定的最后一天完成,如果限定的最后一天已经有作业要完成,(如第一个案例:先判断扣10分的作业能不能在第三天完成,此时第三天没有安排作业. 那么就安排进去.接下来再判断第二个作业 也就是扣分为5的作业,第三天已经安排了扣分为10的作业,所以第三天不能再安排作业。往前找,第二天没有安排作业,所以安排到第二天。以此类推)那么继续往前寻找,若前面都不能安排此作业,那么就算这个作业不能完成.
代码如下:
1 #include2 #include 3 #include 4 #include 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.score;19 }20 void out()21 {22 for(int i=1;i<=n;i++)23 cout< <<" ";24 cout< =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 }