Hdoj 1789 Doing Homework again 题解
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
思路
这是一道典型的贪心题。显然先完成惩罚分数大的会让答案更小,如果惩罚分数相同的时候,我们就要尽量将截止日期先的排在前面。实现的方式是按照惩罚分数来枚举截止日期,就可以完成我们的目的。详见注释
#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 题解的更多相关文章
- HDOJ.1789 Doing Homework again (贪心)
Doing Homework again 点我挑战题目 题意分析 给出n组数据,每组数据中有每份作业的deadline和score,如果不能按期完成,则要扣相应score,求每组数据最少扣除的scor ...
- hdoj 1789 Doing Homework again
Doing Homework again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- hdu 1789 Doing HomeWork Again (贪心算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1789 /*Doing Homework again Time Limit: 1000/1000 MS ...
- 题解报告:hdu 1789 Doing Homework again(贪心)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1789 Problem Description Ignatius has just come back ...
- HDU 1789 Doing Homework again(非常经典的贪心)
Doing Homework again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- HDU 1789 Doing Homework again(贪心)
Doing Homework again 这只是一道简单的贪心,但想不到的话,真的好难,我就想不到,最后还是看的题解 [题目链接]Doing Homework again [题目类型]贪心 & ...
- HDU 1789 Doing Homework again (贪心)
Doing Homework again http://acm.hdu.edu.cn/showproblem.php?pid=1789 Problem Description Ignatius has ...
- HDU 1789 - Doing Homework again - [贪心+优先队列]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1789 Time Limit: 1000/1000 MS (Java/Others) Memory Li ...
- HDU 1789 Doing Homework again(贪心)
在我上一篇说到的,就是这个,贪心的做法,对比一下就能发现,另一个的扣分会累加而且最后一定是把所有的作业都做了,而这个扣分是一次性的,所以应该是舍弃扣分小的,所以结构体排序后,往前选择一个损失最小的方案 ...
随机推荐
- Summer sell-off CodeForces - 810B (排序后贪心)
Summer holidays! Someone is going on trips, someone is visiting grandparents, but someone is trying ...
- linux中根据名称kill进程
shell函数如下: # kill processes by name kbn() { line=`ps -a | grep $1` arr=($line) for((i=0;i<${#arr[ ...
- 自己实现数据结构系列四---Queue
一.代码部分 1.定义接口: public interface Queue<E> { void enqueue(E e); E dequeue(); E getFront(); int g ...
- Mysql连接数、线程数、数据包
https://blog.csdn.net/qq_26545305/article/details/79675507
- MySQL之优化
当 MySQL 单表记录数过大时,增删改查性能都会急剧下降,本文会提供一些优化参考,大家可以参考以下步骤来优化. 一. 单表优化 除非单表数据未来会一直不断上涨,否则不要一开始就考虑拆分,拆分会带来逻 ...
- http1.0 1.1 与2.0
长连接 HTTP 1.0需要使用keep-alive参数来告知服务器端要建立一个长连接,而HTTP1.1默认支持长连接. HTTP是基于TCP/IP协议的,创建一个TCP连接是需要经过三次握手的,有一 ...
- 如何确定 Hadoop map和reduce的个数--map和reduce数量之间的关系是什么?
1.map和reduce的数量过多会导致什么情况?2.Reduce可以通过什么设置来增加任务个数?3.一个task的map数量由谁来决定?4.一个task的reduce数量由谁来决定? 一般情况下,在 ...
- linux redis服务安装
redis下载 官网地址:https://redis.io/download 在Linux下安装Redis非常简单,具体步骤如下(官网有说明): 1.下载源码,解压缩后编译源码. $ wget htt ...
- 老男孩python学习自修第七天【包与模块】
1.如何导入 from package import module module.function() 常用魔术方法 __init__.py 如果某个文件夹下面有该文件,则该文件夹是一个包,否则只是一 ...
- 怎样利用ADO中的adoquery进行缓存更新?????(100分)
我用BDE时,用query与updatesql相结合进行缓存更新,但是在ADO中没有updatesql,只有用adoquery,在DBGRID中,用CANCELUPADTE,只能取消一条记录,烦恼不已 ...