标签:
Doing Homework again
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8963 Accepted Submission(s): 5292
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
这道题很容易想到是使用贪心的策略,先根据期限进行排序,然后对扣的分进行贪心即可,需要注意的是这道题和背包问题一样,单纯的贪心是不能得到最优解的,务必要进行判定
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int T;
int num;
int N,S1,S2;
struct day{
int D;
int S;
int k;
};
const int cmp(const day &a,const day &b){
if(a.D==b.D) return a.S>b.S;
return a.D<b.D;
}
int main()
{
while(scanf("%d",&T)!=EOF){
while(T--){
S1=0;
int s=0,n=1;
day st[1010]={};
scanf("%d",&num);
for(int i=0;i<num;i++){
scanf("%d",&st[i].D);
}
for(int i=0;i<num;i++){
scanf("%d",&st[i].S);
S1+=st[i].S;//记录总的扣分
st[i].k=0;
}
sort(st,st+num,cmp);//按期限排序
n=st[num-1].D;
while(n>0){
int Max=-1;
for(int i=num-1;i>=0;i--){
if(st[i].k==0){
if(st[i].D>=n&&Max==-1) Max=i;
else if(st[i].D>=n&&st[i].S>st[Max].S) Max=i;
}
}
if(Max!=-1){
st[Max].k=1;s+=st[Max].S;
}
n--;
}//反向贪心,注意标记防止重复
printf("%d\n",S1-s);
}
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
Hdu 1789
标签:
原文地址:http://blog.csdn.net/jaker233333/article/details/47664627