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. java问题

    Collection 和 Collections的区别? Collection是集合类的上级接口,继承与他的接口主要有Set 和List. Collections是针对集合类的一个帮助类,他提供一系列 ...

  2. Redis趣谈一则

    今天看代码,无意中发现Redis中的一行注释: ; /* The world is full of Solaris Fixes */ 这行代码在bio.c文件中. 看来程序员对操作系统多样性吐槽多多啊 ...

  3. java中的标记接口(标签接口)

    Java中的标记接口(Marker Interface),又称标签接口(Tag Interface),具体是不包含任何方法的接口.在Java中很容易找到标记接口的例子,比如JDK中的Serialzab ...

  4. Java-Spring-获取Request,Response对象

    转载自:https://www.cnblogs.com/bjlhx/p/6639542.html 第一种.参数 @RequestMapping("/test") @Response ...

  5. python设计模式第六天【原型模式】

    1.定义 使用原型模式复制的对象与原来对象具有一样的结构和数据,有浅克隆和深克隆 2.应用场景 (1)希望复制原来对象的结构和数据胆步影响原来对象 3.代码实现 #!/usr/bin/env pyth ...

  6. npm安裝、卸載、刪除、撤銷發佈包、更新版本信息

    利用npm安裝包: 全局安裝:npm install -g 模塊安裝 局部安裝(可以使用repuire(‘模塊名’)引用):npm install 模塊名稱 如果權限不夠,就是用管理員方式安裝. 本地 ...

  7. jaxp的dom方式操作(查找、添加、修改、删除、遍历节点)

    package cn.itcast.jaxptest; import java.io.IOException; import javax.xml.parsers.DocumentBuilder;imp ...

  8. SQL Server 数据库try catch 存储过程

    SQL Server 在生产环境中这样写存储过程的坑都避免了吗? 原文链接: http://www.cnblogs.com/chenmh/p/7856777.html 概述 最近因为业务的需求写了一段 ...

  9. 13.kubernetes之pv,pvc,configmap(带补充实例)

    管理存储是管理计算的一个明显问题.该PersistentVolume子系统为用户和管理员提供了一个API,用于抽象如何根据消费方式提供存储的详细信息.为此,我们引入了两个新的API资源:Persist ...

  10. darknet源码解析

    https://blog.csdn.net/u014540717/article/category/6513159