Hdu 1789 Doing Homework again
2015-07-31 21:26
459 查看
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
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each test case, you should output the smallest total reduced score, one line per test case.
[align=left]Sample Input[/align]
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
[align=left]Sample Output[/align]
0
3
5
题意:
每个任务都有提交的截止日期,和不提交需要扣除的分数,每天只能完成一个任务,问这个人最少被扣掉多少分
题解:
贪心问题,首先任务比较急的优先,按扣分二级排序
使用队列维护当前选取的任务,进行动态选取,并记录扣的分数
#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;
}
相关文章推荐
- process_begin: CreateProcess(...) failed.
- hdu 5289 Assignment 二分+RMQ 2015 Multi-University Training Contest 1 02
- Aizu 1335 Eequal sum sets
- (easy)LeetCode 219.Contains Duplicate II
- HUAS Summer Trainning #3~M
- HDU doing homework again
- HUAS Summer Trainning #3~L
- HDU-4810 Wall Painting
- 矩阵快速幂 POJ 3735 Training little cats
- (easy)LeetCode 217.Contains Duplicate
- HUAS Summer Trainning #3~C
- HDU 5328 Problem Killer (2015 Multi-University Training Contest 4 2015多校联合)
- Doing Homework again
- HDU 5327 Olympiad (2015 Multi-University Training Contest 4 2015多校联合)
- hdu 1789 Doing Homework again
- waiting for xdebug session
- HUAS Summer Trainning #3~K
- Hdu 5309 JRY is Fighting 2015 Multi-University Training Contest 2
- 2015 Multi-University Training Contest 1(HDOJ5288、5289)
- sleep和wait有什么区别