1. 程式人生 > >Doing Homework Again貪心------[NWPU][2018寒假作業][通用版]二、stl ,模擬,貪心等 [Cloned]P題

Doing Homework Again貪心------[NWPU][2018寒假作業][通用版]二、stl ,模擬,貪心等 [Cloned]P題

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

解題思路:
這道題要用到貪心演算法。貪心演算法沒有什麼固定的模板,需要具體問題具體分析。
http://blog.csdn.net/u011787119/article/details/39827039
http://blog.csdn.net/wishchin/article/details/40081049
這兩篇部落格是一個參考。
這道題要考慮兩個因素,一個是截止日期,一個是要扣除的分數。最先考慮的一定是扣除分數多且馬上就要截止的作業,最後考慮的一定是扣除分數少且很久之後才截止的作業。
所以按扣除分數降序和截止日期升序排序。從序列中的第一個作業開始做,每做一個作業就要將date[1000]陣列中做作業的日期標記,如果一個作業找不到可以標記日期,將該作業扣除的分數加到結果中。
一開始,我是按第一天到deadline的順序找可以標記的日期的,但我、後來發現這樣做不是最優的。最優的順序是儘量先標記deadline,如果不行,再從後往前找,這樣可以儘量把前面的日期空出來,做其他作業。

#include<stdio.h>
#include<algorithm>
using namespace std;

struct node{
    int ddl;
    int sco;
} stu[1010]; 

int date[1010];

bool cmp(node x,node y)
{
    if(x.sco!=y.sco) return x.sco>y.sco;
    else return x.ddl<y.ddl;
}

int main()
{
    int n,t,flag,res;
    scanf("%d",&t);
    for
(int i=1;i<=t;i++) { memset(date,0,sizeof(date)); scanf("%d",&n); for(int j=1;j<=n;j++) { scanf("%d",&stu[j].ddl); } for(int j=1;j<=n;j++) { scanf("%d",&stu[j].sco); } sort(stu+1,stu+n+1,cmp); res=0; for(int j=1;j<=n;j++) { flag=0; for(int k=stu[j].ddl;k>=1;k--) { if(date[k]==0) { date[k]=1; flag=1; break; } } if(flag==0) { res+=stu[j].sco; } } printf("%d\n",res); } }