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


思路

这是一道典型的贪心题。显然先完成惩罚分数大的会让答案更小,如果惩罚分数相同的时候,我们就要尽量将截止日期先的排在前面。实现的方式是按照惩罚分数来枚举截止日期,就可以完成我们的目的。详见注释

#include<bits/stdc++.h>
using namespace std;
struct node
{
int d; //截止日期
int r; //惩罚分数
}a[1010];
bool vis[1010]; bool cmp(node x,node y)
{
if(x.r==y.r)
return x.d < y.d;
else
return x.r > y.r;
} //排序函数,先按照惩罚分数排,如果惩罚分数一样就按照截止日期从小到大排 int main()
{
int T;
int ans;
while(cin>>T)
{
while(T--)
{
int N;
cin >> N;
for(int i=1;i<=N;i++) cin >> a[i].d;
for(int i=1;i<=N;i++) cin >> a[i].r;
sort(a+1,a+N+1,cmp);
memset(vis,false,sizeof(vis)); //读入并排序和初始化 ans = 0;
for(int i=1;i<=N;i++)
{
if(!vis[a[i].d])
vis[a[i].d] = true; //每个任务都是一天做完的,如果当天还没用就使用
else
{
int j;
for(j=a[i].d;j>=1;j--)
if(!vis[j]) break; //枚举截止日期,如果先前有一天没有用到就退出
if(j>0) vis[j] = true; //说明a]i].d之前的截止日期没有充分利用
else ans += a[i].r; //a[i].d之前每天都有任务,所以要进行惩罚分数的累加
}
}
cout << ans << endl;
}
}
return 0;
}

Hdoj 1789 Doing Homework again 题解的更多相关文章

  1. HDOJ.1789 Doing Homework again (贪心)

    Doing Homework again 点我挑战题目 题意分析 给出n组数据,每组数据中有每份作业的deadline和score,如果不能按期完成,则要扣相应score,求每组数据最少扣除的scor ...

  2. hdoj 1789 Doing Homework again

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

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

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

  4. 题解报告:hdu 1789 Doing Homework again(贪心)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1789 Problem Description Ignatius has just come back ...

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

  7. HDU 1789 Doing Homework again (贪心)

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

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

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

  9. HDU 1789 Doing Homework again(贪心)

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

随机推荐

  1. ibeacon和蓝牙有什么区别_它们的区别在哪里

    iBeacon概述 iBeacon是苹果公司2013年9月发布的移动设备用OS(iOS7)上配备的新功能.其工作方式是,配备有低功耗蓝牙(BLE)通信功能的设备使用BLE技术向周围发送自己特有的ID, ...

  2. 自签名证书 nginx tomcat

    给Nginx配置一个自签名的SSL证书 - 廖雪峰的官方网站 https://www.liaoxuefeng.com/article/0014189023237367e8d42829de24b6eaf ...

  3. toTree

    // js实现树级递归, // 通过js生成tree树形菜单(递归算法) const data = [ { id: 1, name: "办公管理", pid: 0 }, { id: ...

  4. java对象的四种引用:强引用、软引用、弱引用和虚引用

    在JDK1.2之前,创建的对象只有在处于可触及(reachable)的状态下,才能被程序使用.也就是说,若一个对象不被任何变量引用,那么程序就无法再使用这个对象.垃圾回收器一旦发现这些无用对象,就会对 ...

  5. Tomcat启用GZIP压缩,提升web性能

    一.前言 最近做了个项目,遇到这么一个问题:服务器返回给客户端的json数据量太大(大概65M),在客户端加载了1分多钟才渲染完毕,费时耗流量,用户体验极其不好.后来网上搜优化的方法,就是Http压缩 ...

  6. oracle建表流程

    --创建表空间test1 create tablespace test1 datafile 'd:\test1.dbf' size 100m autoextend on next 10m --创建用户 ...

  7. Granfana+PostgreSQL

    建表: create table pdm_log( id serial primary key, date_time timestamp with time zone DEFAULT CURRENT_ ...

  8. C# Note10: AutoComplete TextBox in WPF

    参考: 1.https://stackoverflow.com/questions/950770/autocomplete-textbox-in-wpf 2.AutoCompleteBox的使用(实现 ...

  9. 在浏览器上安装 Vue Devtools工具

    Vue.js devtools是基于google chrome浏览器的一款调试vue.js应用的开发者浏览器扩展,可以在浏览器开发者工具下调试代码. 1)首先在github下载devtools源码,地 ...

  10. python 编码格式

    1. 字符编码简介 1.1. ASCII ASCII(American Standard Code for Information Interchange),是一种单字节的编码.计算机世界里一开始只有 ...