HDU 1789 Doing Homework again (贪心)

本文介绍了一种算法,用于解决学生如何安排多项作业以最小化因延期提交而产生的总惩罚分数的问题。通过从最后一天往回推算,每日选择惩罚值最大的可完成作业进行提交,从而有效减少总的惩罚分数。
 Doing Homework again
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  HDU 1789

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
 

 

 
 
思路:从最后一天开始,找出当天能完成的作业里惩罚最大那个,将其完成,然后天数向前推,推到第一天为止。
   其实和从第一天开始,每天找出能完成且惩罚最大的这种思路有点像,但是这样的做法造成的浪费太大,比如可能第一天就去把第四天的完成掉,这样第一天就不能做别的。从后往前就是减少了浪费,将浪费压缩到最低。
 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<stdlib.h>
 4 #define    MAX    1005
 5 
 6 int    main(void)
 7 {
 8     int    t,n;
 9     int    s[MAX][2];
10     int    sum,box,max,max_loc;
11 
12     scanf("%d",&t);
13     while(t --)
14     {
15         sum = box = 0;
16 
17         scanf("%d",&n);
18         for(int i = 0;i < n;i ++)
19             scanf("%d",&s[i][0]);        //0号单元存天数
20         for(int i = 0;i < n;i ++)
21         {
22             scanf("%d",&s[i][1]);        //1号单元存惩罚值
23             sum += s[i][1];
24         }
25         for(int i = n;i >= 1;i --)
26         {
27             max = -1;
28             for(int j = 0;j < n;j ++)
29                 if(s[j][0] >= i && s[j][1] > max)
30                 {
31                     max = s[j][1];
32                     max_loc = j;
33                 }
34             if(max != -1)
35             {
36                 box += max;
37                 s[max_loc][1] = -1;
38             }
39         }
40         printf("%d\n",sum - box);
41     }
42 }

 

转载于:https://www.cnblogs.com/xz816111/p/4167251.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值