题目链接: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. UVALive3211- Now or later(二分+2-SAT)

    题目链接 题意:有n架飞机.每架飞机都能够选择早着陆和晚着陆两种方式之中的一个,且必须选择一种. 任务就是安排全部飞机着陆时.相邻两个着陆时间间隔的最小值尽量大. 思路:用二分处理最小值尽量大.该题目 ...

  2. [TypeScript] Transform Existing Types Using Mapped Types in TypeScript

    Mapped types are a powerful and unique feature of TypeScript's type system. They allow you to create ...

  3. MySQL基础笔记(一) SQL简介+数据类型

    MySQL是一个关系型数据库管理系统(RDBMS),它是当前最流行的 RDBMS 之一.MySQL分为社区版和企业版,由于其体积小.速度快.总体拥有成本低,尤其是开放源码这一特点,一般中小型网站的开发 ...

  4. router-link的a样式变成div样式元素属性

    <router-link tag="div" :to="itemChild.path"><span>{{itemChild.name}} ...

  5. C#调用国家气象局天气预报接口

    原文:C#调用国家气象局天气预报接口 一.需求 最近,刚好项目中有天气预报查询功能的需求,要求录入城市名称,获取该城市今日天气信息及相关气象生活辅助信息等. 例如:查询北京市天气 结果为: 今日北京天 ...

  6. 菜鸟Sublime日记

            一.进行系统安装:www.sublimetext.com/3   选择相应的操作系统,你会发现安装速度惊人的快. 二.安装完成以后,先安装两个基本的插件package control ...

  7. MUI日期选择控件

    MUI的Demo里面有例子,实际做的时候发现日期老是乱码,调了半天,最后发现引用的CSS错误,例子中引用的是 <link href="Css/mui.picker.min.css&qu ...

  8. 内部消息 微软中国云计算 内測Azure免费账号 赶紧申请 错过不再有

    内部消息 微软中国云计算 顶级内測Azure免费账号 火热申请 过期不再有! 微软MSDN俱乐部  29754721, [一大波Azure免费账号来袭]Windows Azure再次开启示放免费试用账 ...

  9. bzoj1090 [SCOI2003]字符串折叠——区间DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1090 区间DP... 代码如下: #include<iostream> #inc ...

  10. mysql同步延迟导致的问题

    前几天又遇到一个mysql读写分离的坑, 在将数据写入master后,因为存在同步延迟,所以如果立马去从库查询刚刚插入的数据可能会出现查询不到数据的情况, 解决办法:强制从主库读取数据,将插入和查询放 ...