首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Hdu 1789 Doing Homework again

Hdu 1789 Doing Homework again

作者头像
若羽
发布2019-07-15 16:20:54
3880
发布2019-07-15 16:20:54
举报
文章被收录于专栏:Code思维奇妙屋Code思维奇妙屋

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

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         else
57         {
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 }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014-08-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Doing Homework again
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档