【hdu 1789】 Doing Homework again

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1789

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

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

题目大意:有T组测试数据,每组测试数据首先输入N表示有N门功课,然后输入一行N个整数表示完成N门功课作业的时间限制,接着输入一行N个整数表示未在限制的时间内完成作业要扣除的分数。需要求最少被扣的分数。

思路:可以用贪心算法策略解决:优先安排分数较大的功课;同时,每一门功课的作业在其截止日期到达前尽量靠后安排,这样可以把靠前的日期节约出来,安排其他更紧急的功课。

具体做法:

对分数按从大到小排序,分数相等则按截止日从小到大排。然后按分数从大到小扫描所有工作,为各项功课排档期:扫描到第i项功课时,假设第i项功课截止日为x,那么,若是第x天没有被安排其他功课,那第x天就排第i项功课;若是第x天安排了其他功课,那么就从第x-1天往前到第1天寻找一个空闲日来安排第i项功课。若是找不到空闲日,就无法安排第i项功课。若是第i项功课能被安排就接着安排下一项功课,若无法安排第i项功课,那就扣除第i项功课的分数。

具体代码如下:

 1 #include <iostream>
 2 #include<algorithm>
 3 #include<stdio.h>
 4 #include<string.h>
 5 using namespace std;
 6
 7 #define maxN 1005
 8
 9 struct work
10 {
11     int time,score;
12 }works[maxN];
13 bool done[maxN];
14
15 int cmp(struct work a,struct work b)
16 {
17     if(a.score!=b.score)
18         return a.score > b.score;//按分数从大到小排序
19     else return a.time < b.time;//分数相等,截止时间小的排在前面
20 }
21
22 int main()
23 {
24     int T,N,i,ans;
25     int j;
26     scanf("%d",&T);
27     while(T--)
28     {
29         scanf("%d",&N);
30         for(i=0;i<N;i++) scanf("%d",&works[i].time);
31         for(i=0;i<N;i++) scanf("%d",&works[i].score);
32         sort(works,works+N,cmp);
33
34         /*for(i=0;i<N;i++) printf("%d ",works[i].score);
35         printf("\n");*/
36
37         ans=0;
38         memset(done,false,sizeof(done));
39
40         for(i=0;i<N;i++)//按照分数从小到大的顺序扫描所有作业
41         {
42             if(done[works[i].time]==true)//如果第i项作业截止日期当天已经被占用
43             {
44                 j=works[i].time;
45                 while(j>0&&done[j]==true)//注意:时间是从1开始,第0天不存在,这里j必须大于0
46                     j--;//从截止日当天往前寻找空闲的一天来安排该项作业
47                 if(j>0)//注意:时间是从1开始,第0天不存在,这里j必须大于0
48                     done[j]=true;   //如果找到空闲天,在这一天安排该项作业
49                 else ans=ans+works[i].score;//若找不到空闲天,该项作业无法完成,相应分数被扣除。
50             }
51             else done[works[i].time]=true;//第i项作业截止日期当天没被占用,那就在截止日期安排该项作业
52         }
53         printf("%d\n",ans);
54     }
55     return 0;
56 }

下面的代码是从网上摘抄的,可以参考一下:(来源戳这里

 1 #include<stdio.h>
 2 #include<iostream>
 3 #include<string.h>
 4 #include<algorithm>
 5 using namespace std;
 6 struct ss
 7 {
 8     int time,p;
 9 }t[100000];
10 int f[100000];
11 int cmp(const ss a,const ss b)
12 {
13     if(a.p>b.p)
14         return 1;
15     else if(a.p==b.p&&a.time<b.time)
16         return 1;
17     else
18         return 0;
19 }
20 int main()
21 {
22     int text,n;
23     scanf("%d",&text);
24     while(text--)
25     {
26         scanf("%d",&n);
27         memset(f,0,sizeof(f));
28         int i;
29         for(i=1;i<=n;i++)
30             scanf("%d",&t[i].time);
31         for(i=1;i<=n;i++)
32             scanf("%d",&t[i].p);
33         sort(t+1,t+1+n,cmp);
34         int sum=0;
35         for(i=1;i<=n;i++)
36         {
37             for(int j=t[i].time;j>=1;j--)
38                 if(!f[j])
39                 {
40                     f[j]=1;
41                     break;
42                 }
43             if(j==0)
44                 sum+=t[i].p;
45         }
46         printf("%d\n",sum);
47     }
48     return 0;
49 }

原文地址:https://www.cnblogs.com/huashanqingzhu/p/12686326.html

时间: 2024-08-02 19:37:47

【hdu 1789】 Doing Homework again的相关文章

【HDU 1009】FatMouse&#39; Trade

题 Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean. The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of c

【HDU 4940】Destroy Transportation system(数据水/无源无汇带上下界可行流)

Description Tom is a commander, his task is destroying his enemy’s transportation system. Let’s represent his enemy’s transportation system as a simple directed graph G with n nodes and m edges. Each node is a city and each directed edge is a directe

【HDU 5647】DZY Loves Connecting(树DP)

pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 332    Accepted Submission(s): 112 Problem Description DZY has an unroote

【2014 Multi-University Training Contest 3 1002】/【HDU 4888】 Redraw Beautiful Drawings

不容易啊,终于可以补第二个题了!! 顺便说一句:模版写残了就不要怪出题人啊 ~ (这残废模版研究了好长时间才找出错) 题目大意: 有一个n*m的矩阵,每一个格子里都将有一个数.给你每一行数字之和和每一列数字之和.求每一个位置能填0~k之间的哪个数.如果有多种可能输出"Not Unique",如果没有解输出"Impossible",如果一组解则将其输出. 解题思路: 最大流: 不可能的条件:是行之和和列之和不想等或者建图后的最大流与他们不想等. 多组的条件是:在最大流

【HDU 1839】 Delay Constrained Maximum Capacity Path(二分+最短路)

[HDU 1839] Delay Constrained Maximum Capacity Path(二分+最短路) Delay Constrained Maximum Capacity Path Time Limit: 10000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 1515    Accepted Submission(s): 481 Problem

【HDU 5828】Rikka with Sequence(线段树)

[HDU 5828]Rikka with Sequence(线段树) Rikka with Sequence Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2311    Accepted Submission(s): 391 Problem Description As we know, Rikka is poor at math.

【POJ 1789】Truck History

[POJ 1789]Truck History 对于弱这种英语渣来说 这明明就是个英语阅读题啊!!--虽然已经习惯了..不过这题寒假集训刚做过 在哪来一看还是没读懂--千刀万剐煮了我吧 一个字符串由7个小写字母组成 每个字符串表示一种状态 每个状态只能由另一种状态转换 转换耗时为两个字符串中不同字母的数量(即distance -> [0,7]) 读出来这些就好办了 赤果果的最小生成树,Prim Kruskal都可以 测了测这题Kruskal耗时少--但2000个点的话1999000条边(无向)

【HDU 4352】 XHXJ&#39;s LIS (数位DP+状态压缩+LIS)

XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2422    Accepted Submission(s): 990 Problem Description #define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then careful

【HDU 5811】Colosseo(拓扑+输入优化)

[HDU 5811]Colosseo(拓扑+输入优化) Colosseo Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 446    Accepted Submission(s): 98 Problem Description Mr. Chopsticks keeps N monsters, numbered from 1 to N.