题目链接: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. BloomFilter学习

    看大数据面试题,看到BloomFilter,找了篇文章学习一下: http://www.cnblogs.com/heaad/archive/2011/01/02/1924195.html Bloom ...

  2. AngularJS - $index, $event, $log

    原文: https://thinkster.io/egghead/index-event-log --------------------------------------------------- ...

  3. 关于对FLASH开发,starling、starling feathers、starling MVC框架的理解

    说在前头:楼主之前没有不论什么flash开发经验,仅仅是从一次尝试中总结自己的理解和经验而已.假设有写的不正确的地方,欢迎大家指正. 前一段时间尝试想用flash(as3)又一次制作一下之前做的一个游 ...

  4. 剑指Offer面试题15(Java版):链表中倒数第K个结点

    题目: 输入一个链表.输出该链表中倒数第k哥结点.  为了符合大多数人的习惯,本题从1開始计数.即链表的尾结点是倒数第1个结点. 比如一个链表有6个结点.从头结点開始它们的值依次是1.2.3,4,5, ...

  5. 自动填充输入框 Asp .Net Mvc

    1 效果 当在一个文本框中输入时,可以自动查找相关选项,然后加载出来以供参考   2 前台代码   <link href="~/Content/themes/base/jquery-u ...

  6. java里int类型转byte类型

    今天在做书上的一个例子的时候, 要使用byte类型,首先我很直接的就写到了byte b = 0XAA, 结果报错, 说从int转换到byte可能会有损失. 我当时就很奇怪, 为什么会出现这种情况呢? ...

  7. HTML5_路径

    <!DOCTYPE html> <hmtl> <html  lang="zh-cn"> <head> <meta charse ...

  8. Entity Framework底层操作封装V2版本号(3)

    如今是附加的,组合查询须要的扩展类.大家知道lanmda表达式的组合条件比較麻烦,所以就加了一样一个类,方便进行组合查询: using System; using System.Collections ...

  9. HDU1257 最少拦截系统 —— 贪心

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1257 最少拦截系统 Time Limit: 2000/1000 MS (Java/Othe ...

  10. 测试SQL基础知识

    SQL DML 和 DDL 可以把 SQL 分为两个部分:数据操作语言 (DML) 和 数据定义语言 (DDL). SQL (结构化查询语言)是用于执行查询的语法.但是 SQL 语言也包含用于更新.插 ...