1789 Doing Homework again 倒敘思想的貪心算法

Doing Homework again

Time Limit: 1000/1000 MS (Java/Others)    

Memory Limit: 32768/32768 K (Java/Others)

 

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
#include "iostream"
#include <algorithm>
#include <cstring>
using namespace std;

struct Node{
    int time,fen;
}node[1005];


int cmp(struct Node a,struct Node b){
    if(a.fen!=b.fen)
        return a.fen>b.fen;//分數越高排名越前
    return a.time<b.time;//分數相同是ddl越早排名越前
    // ddl=deadline,截止日期
}



int visit[2010];//visit表示當天是否寫過作業,如果當天沒寫過,值爲0,否則爲1
int main(){
    
    
    freopen("/Users/qigelaodadehongxiaodi/Desktop/data1.txt", "r", stdin);
    //這個不理,是用來方便輸入輸出的東西,利用文本輸入流來讀取數據
    //提交代碼的時候記得註銷這條語句
    
    int m;
    cin>>m;
    while(m--){
        
        int n,i,j,ans=0;
        memset(visit, 0, sizeof(visit));//visit表示當天是否寫過作業,
        cin>>n;
        for(i=0;i<n;i++){
            cin>>node[i].time;
        }
        for(i=0;i<n;i++){
            cin>>node[i].fen;
        }
        
        sort(node,node+n,cmp);
     
        for(i=0;i<n;i++){
            
            j=node[i].time;
            //從截止日期往前推,如果其中有一天沒有用過,則這一天做第i份作業
            //比如j=4,則從第四天開始往前推,找到四天中最晚的空的時間,拿來做這份作業,因此可以在完成作業的基礎上,在前面留下更多的位置給ddl早的作業
            //這種倒着的思想很重要!
            while(j){
                if(!visit[j]){//即最晚的空的時間第j天可以拿來完成第i天的作業
                    visit[j]=1;//表示用過了
                    break;
                }
                j--;
            }
            
            if(j==0)//如果j=0,表明從ddl往前的每一天都被佔用了
                //沒有時間給他使用,這門課完不成
                //完不成的原因是因爲其性價比太低,最後才選擇做,然後沒辦法爆炸了
                ans+=node[i].fen;
        }
        cout<<ans<<endl;
        
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章