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(贪心)
在我上一篇说到的,就是这个,贪心的做法,对比一下就能发现,另一个的扣分会累加而且最后一定是把所有的作业都做了,而这个扣分是一次性的,所以应该是舍弃扣分小的,所以结构体排序后,往前选择一个损失最小的方案 ...
随机推荐
- 通过设置线程池的最小线程数来提高task的效率,SetMinThreads。
http://www.cnblogs.com/Charltsing/p/taskpoolthread.html task默认对线程的调度是逐步增加的,连续多次运行并发线程,会提高占用的线程数,而等若干 ...
- iOS 传感器集锦
https://www.jianshu.com/p/5fc26af852b6 传感器集锦:指纹识别.运动传感器.加速计.环境光感.距离传感器.磁力计.陀螺仪 效果预览.gif 一.指纹识别 应用: ...
- debian下 Hadoop 1.0.4 集群配置及运行WordCount
说明:我用的是压缩包安装,不是安装包 官网安装说明:http://hadoop.apache.org/docs/r1.1.2/cluster_setup.html,繁冗,看的眼花...大部分人应该都不 ...
- 【学习总结】Git学习-参考廖雪峰老师教程九-使用码云
学习总结之Git学习-总 目录: 一.Git简介 二.安装Git 三.创建版本库 四.时光机穿梭 五.远程仓库 六.分支管理 七.标签管理 八.使用GitHub 九.使用码云 十.自定义Git 期末总 ...
- 【问题解决方案】Git bash进入多层子目录问题(通配符问题留坑)
cd进入指定路径下:cd 斜杠 斜杠 方法一: 1- 撇丿,不是"那",盘符前面要加上 / (d盘前面也加,不加也行) 2- 路径名不区分大小写 3- 不用空格 4- 如果目录名中 ...
- alibaba druid
FAQ · alibaba/druid Wikihttps://github.com/alibaba/druid/wiki/FAQ sql 连接数不释放 ,Druid异常:wait millis 40 ...
- mysql uuid() 相同 重复
mysql select UPPER(REPLACE(uuid(),'-','')) from xxxtable 得到相同的uuid的问题 - LWJdear的博客 - CSDN博客 https:// ...
- Jenkins Installing and migration
JAVA_Zookeeper_hadoop - CSDN博客https://blog.csdn.net/wangmuming Installing Jenkins on Red Hat distrib ...
- flutter-StatelessWidget与StatefulWidget
StatelessWidget和StatefulWidget是flutter的基础组件,日常开发中自定义Widget都是选择继承这两者之一. 两者的区别在于状态的改变,StatelessWidget面 ...
- 如何使用Action.Invoke()触发一个Storyboard
一般在我们的项目中,最好是将Storyboard放在前台,然后设置Storyboard的x:key值,通过我们的TryFindResource来查找到当前的Storyboard来启动Stroyboar ...