hdu1789 Doing Homework Again

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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章