Hdu 1789 Doing Homework again
来源:互联网 发布:怎样用淘宝客推广 编辑:程序博客网 时间:2024/06/06 09:33
Doing Homework again
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8837 Accepted Submission(s): 5203
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.
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
333 3 310 5 131 3 16 2 371 4 6 4 2 4 33 2 1 7 6 5 4
Sample Output
035
题意:
每个任务都有提交的截止日期,和不提交需要扣除的分数,每天只能完成一个任务,问这个人最少被扣掉多少分
题解:
贪心问题,首先任务比较急的优先,按扣分二级排序
使用队列维护当前选取的任务,进行动态选取,并记录扣的分数
#include<stdio.h>#include<string.h>#include<algorithm>#include<queue>using namespace std;struct ex{ int day,sc;}x[1005];int cmp(ex a,ex b){ if(a.day==b.day) { return a.sc<b.sc; } return a.day<b.day;}int main(){ int t,n,i,j,k,sum; scanf("%d",&t); while(t--) { priority_queue<int,vector<int>,greater<int> > y; scanf("%d",&n); for(i=0;i<n;++i) { scanf("%d",&x[i].day); } for(i=0;i<n;++i) { scanf("%d",&x[i].sc); } sort(x,x+n,cmp); sum=0; for(i=0;i<n;++i) { if(x[i].day>y.size()) { y.push(x[i].sc); } else { if(y.top()<x[i].sc) { sum+=y.top(); y.pop(); y.push(x[i].sc); } else { sum+=x[i].sc; } } } printf("%d\n",sum); } return 0;}
2016年4月19日8:58
距离上次做这道题已经七八个月了......
重新做一遍感觉思路清晰多了,程序控制也更为简练了,也在一点一点的进步吧!
加油!
题解:
队列中维护值最小的优先,依次放入任务,任务过多的话,就取出队列中分数最小的那个任务,抛弃,直到程序结束。
/*http://blog.csdn.net/liuke19950717*/#include<cstdio>#include<cstring>#include<queue>#include<algorithm>using namespace std;const int maxn=1e6;struct node{int time,val;bool friend operator < (node a,node b){return a.val>b.val;}}x[maxn];int cmp(node a,node b){if(a.time==b.time){return a.val>b.val;}return a.time<b.time;}int slove(int n){sort(x,x+n,cmp);priority_queue<node> q;int ans=0;for(int i=0;i<n;++i){q.push(x[i]);if(q.size()>x[i].time){node tp=q.top();q.pop();ans+=tp.val;}}return ans;}int main(){int t;//freopen("shuju.txt","r",stdin);scanf("%d",&t);while(t--){int n;scanf("%d",&n);for(int i=0;i<n;++i){scanf("%d",&x[i].time);}for(int i=0;i<n;++i){scanf("%d",&x[i].val);}printf("%d\n",slove(n));} return 0;}
0 0
- hdu 1789 doing homework again
- HDU 1789 Doing Homework again!
- hdu 1789 Doing Homework again
- hdu 1789 Doing Homework again
- hdu 1789 Doing Homework again
- hdu 1789 Doing Homework again
- Hdu 1789 - Doing Homework again
- HDU 1789 Doing Homework again
- hdu 1789 Doing Homework again
- HDU 1789 Doing Homework again
- hdu 1789 Doing Homework again
- hdu 1789 doing homework again
- HDU 1789 Doing Homework again
- HDU 1789 Doing Homework again
- HDU 1789 Doing Homework again
- HDU - 1789 Doing Homework again
- HDU 1789 Doing Homework again
- hdu 1789 Doing Homework again
- STL学习记录(十四):其它几种算法
- VS2010关于error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- ARM处理器体系架构详细说明
- uvalive 4973
- Scala学习第五天:数组的基本操作,数组的进阶操作和多维数组
- Hdu 1789 Doing Homework again
- pycharm快捷键、常用设置、包管理
- Selenium IDE简介
- linux驱动中宏__setup(str, fn)
- uva11235 Frequent values(线段树区间合并)
- 黑马程序员——String类
- dsp之BF531笔记
- Xcode运行时候报路径错误和编译错误时及莫名其妙系列错误时
- 将两个有序单链表合并