Doing Homework again
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10618 Accepted Submission(s): 6232
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 31
6 2 3
7
1 4 6 4 2 4 3
3 2 1 7 6 5 4
Sample Output
0
3
5
瞎搞的, 突然就A西了- -话说网上有大神用的贪心, 好像比我写的简单多了- -
说说我的小思路, 也有点儿贪心的影子.
把作业当做一个整体, 按照deadline排序(从小到大).
设置一个时间计数器, dayCanUse表明目前有多少天可用于写作业.
yesterday记录的是前一个作业的deadline.
if(grp[i].deadline>yesterday)说明当前作业的deadline和前一个作业的deadline不一样, 说明中间就空出几天可以用来写作业.
if(dayCanUse>0)有时间就写吧, 并把当前作业耗时加入dustbin中(后面有用)
没时间的话, 看看dustbin里有没有扣分比较低的作业(低于当前作业的扣分), 如果没有, 老老实实被扣分吧.
如果有, 那dustbin扣分最低的那个作业就不做了, 用那个时间来做当前的作业.
multiset是允许有重复元素的集合(数学上的集合不允许元素重复), 当数据插入时, 自动排序, 默认是从小到大.
#include<cstdio>
#include<cstring>
#include<iostream>
#include<set>
#include<algorithm>
using namespace std;
const int N=1000;
struct homework{
int deadline;
int score;
};
homework grp[N+5];
bool xmp(homework a, homework b){
if(a.deadline!=b.deadline)return a.deadline<b.deadline;
return a.score>b.score;
}
multiset<int> dustbin;
int main(){
//freopen("hdu1789.in", "r", stdin);
//freopen("hdu.out", "w", stdout);
int t,n,i,j;
cin>>t;
while(t--){
cin>>n;
memset(grp, 0, sizeof(grp));
for(i=0;i<n;i++)scanf("%d", &(grp[i].deadline));
for(i=0;i<n;i++)scanf("%d", &(grp[i].score));
sort(grp, grp+n, xmp);
int yesterday=0;
int dayCanUse=0;
int reduseSum=0;
dustbin.clear();
for(i=0;i<n;i++){
if(grp[i].deadline>yesterday){
dayCanUse+=grp[i].deadline-yesterday;
yesterday=grp[i].deadline;
}
if(dayCanUse>0){
dayCanUse--;
dustbin.insert(grp[i].score);
}else{
if(dustbin.size()==0 || *dustbin.begin()>grp[i].score){
reduseSum+=grp[i].score;
}else{
reduseSum+=*dustbin.begin();
dustbin.erase(dustbin.begin());
dustbin.insert(grp[i].score);
}
}
}
cout<<reduseSum<<endl;
}
return 0;
}