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

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

解题思路:典型的贪心策略。因为要使得扣分最少,所以要先排个序,规则是:如果期限相同,对扣分多的从大到小排列,如果扣分相同,则将期限从小到大排列。最优策略:每门功课最好在给定的deadline当天就完成,如不能完成,只能往前找哪一天还没使用,尽量使得做这门功课的日期越大越好,即从其截止日期到第一天,如果一路遍历都已经被标记过了,到最后j==0说明已经没有足够的一天时间给他做这门功课,那么就将这门功课扣的分数加到ans中,最终的ans即为最小扣分值。

AC代码:

 #include<bits/stdc++.h>
using namespace std;
struct NODE
{
int deadline,reduce;
}node[];
bool cmp(NODE a,NODE b)
{
if(a.reduce!=b.reduce)return a.reduce>b.reduce;//reduce越多的越靠前,先解决扣分多的
return a.deadline<b.deadline;//reduce相同时,deadline越早越靠前,从小到大
}
bool vis[];//如果当天没用过,值为false;否则为true
int main()
{
int T,N,ans,j;//ans是保存减少的reduce
cin>>T;
while(T--){
memset(vis,false,sizeof(vis));
cin>>N;
for(int i=;i<N;i++)cin>>node[i].deadline;
for(int i=;i<N;i++)cin>>node[i].reduce;
sort(node,node+N,cmp);//按规则排序
ans=;
for(int i=;i<N;i++){
for(j=node[i].deadline;j>;j--)//从截止时间开始往前推,如果有一天没用过,这一天就做这一门课,那么这门课不扣分
if(!vis[j]){vis[j]=true;break;}
if(j==)ans+=node[i].reduce;//如果j=0,表明从deadline往前的每一天都被占用了,这门课完不成
}
cout<<ans<<endl;
}
return ;
}

题解报告:hdu 1789 Doing Homework again(贪心)的更多相关文章

  1. hdu 1789 Doing HomeWork Again (贪心算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1789 /*Doing Homework again Time Limit: 1000/1000 MS ...

  2. HDU 1789 - Doing Homework again - [贪心+优先队列]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1789 Time Limit: 1000/1000 MS (Java/Others) Memory Li ...

  3. HDU 1789 Doing Homework again(贪心)

    Doing Homework again 这只是一道简单的贪心,但想不到的话,真的好难,我就想不到,最后还是看的题解 [题目链接]Doing Homework again [题目类型]贪心 & ...

  4. hdu 1789 Doing Homework again (Greedy)

    Problem - 1789 继续贪心.经典贪心算法,如果数据比较大就要用线段树来维护了. 思路很简单,只要按照代价由大到小排序,然后靠后插入即可.RE了一次,是没想到deadline可以很大.如果d ...

  5. HDU 1789 Doing Homework again(非常经典的贪心)

    Doing Homework again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  6. HDU 1789 Doing Homework again (贪心)

    Doing Homework again http://acm.hdu.edu.cn/showproblem.php?pid=1789 Problem Description Ignatius has ...

  7. HDU 1789 Doing Homework again(贪心)

    在我上一篇说到的,就是这个,贪心的做法,对比一下就能发现,另一个的扣分会累加而且最后一定是把所有的作业都做了,而这个扣分是一次性的,所以应该是舍弃扣分小的,所以结构体排序后,往前选择一个损失最小的方案 ...

  8. HDU 1789 Doing Homework again【贪心】

    题意:给出n个作业的截止时间,和该作业没有完成会被扣掉的分数.问最少会被扣掉多少分. 第一次做这一题是好久之前,当时不会(不会处理两个关键字关系@_@)---现在还是不会---看了题解---原来是这样 ...

  9. HDU - 1789 Doing Homework again(贪心) ~~~学了一波sort对结构体排序

    题目中因为天数和分数是对应的,所以我们使用一个结构体来存分数和截止如期. 一开始做这道题的时候,很自然的就想到对天数排序,然后天数一样的分数从大到小排序,最后WA了之后才发现没有做到"舍小取 ...

随机推荐

  1. Atitit.软件仪表盘(7)--温度监測子系统--电脑重要部件温度与监控and警报

    Atitit.软件仪表盘(7)--温度监測子系统--电脑重要部件温度与监控and警报 Cpu温度.风扇转速 主板温度 显卡温度 硬盘温度 电池温度 鲁大师  硬盘温度 Cpu温度  core temp ...

  2. freemarker 模板

    1 整体结构 模板(FTL 编程)是由例如以下部分混合而成的: Text 文本:文本会照着原样来输出. Interpolation 插值:这部分的输出会被计算的值来替换.插值由${和}所分隔(或者#{ ...

  3. soapUI系列之—-07 调用JIRA Rest API接口【例】

    一.调用JIRA接口------实现过滤器搜索问题 1. 在SoapUI中新建 REST Project, 在URI 中输入登录接口的 url (任意一个 Rest 接口的 url 都可以): 2. ...

  4. asp.net mvc3的静态化实现

    静态化处理,可以大大提高客户的访问浏览速度,提高用户体验,同时也降低了服务器本身的压力.在asp.net mvc3中,可以相对容易地处理静态化问题,不用过多考虑静态网页的同步,生成等等问题.我提供这个 ...

  5. Windows server 2003 + IIS6 搭建Asp.net MVC执行环境

    安装.Net Framework4.0. 下载地址: http://www.microsoft.com/zh-cn/download/details.aspx?id=17718  安装WindowsS ...

  6. Making User-Managed Backups-17.3、Making User-Managed Backups of Offline Tablespaces and Datafiles

    17.3.Making User-Managed Backups of Offline Tablespaces and Datafiles 备份离线的表空间时.须要注意下面指导原则: (1)不能离线s ...

  7. MD5加密解密帮助类

    using System; using System.Security.Cryptography; using System.Text; namespace Maticsoft.DBUtility { ...

  8. Supporting IPv6-only Networks

    Supporting IPv6-only Networks - Support - Apple Developer https://developer.apple.com/support/ipv6/ ...

  9. nextLine() 、nextInt()的跳过问题

    nextInt()  将输入的信息的下一个标记扫描为 int. nextLine() 此扫描器执行当前行,并返回跳过的输入信息. nextInt 会读取下面输入的 int类型的信息以回车作为结束,如果 ...

  10. [开源下载] 【开源项目】EasySL for Silverlight 4

    [开源下载] [开源项目]EasySL for Silverlight 4  [复制链接]       silverlight 452 主题 7 好友 1万 积分 管理员 贡献 879 原创 0 银元 ...