hdu 1789 Doing Homework again

想了半天没想出来,后来参考了别人的思路。先按reduced从大到小排,对于每个homework,若它的deadline日期上无homework,就安排它,若已有homework,就往前推,直到找到一个日期安排。若实在找不到,就加到sum。最后输出sum即可。

AC代码:

#include<iostream>
using namespace std;
#define NUM 1005
 
struct hw{
    int deadLine;
    int reduced;
}homework[NUM];
int a[NUM];
 
int cmp(const void *x, const void *y)
{
    struct hw *a=(struct hw*)x;
    struct hw *b=(struct hw*)y;
    if(a->reduced==b->reduced)
        return a->deadLine - b->deadLine;
    return b->reduced - a->reduced;
}
 
int main()
{
    int cases,n,i,j,sum;
    cin>>cases;
    while(cases--){
        cin>>n;
        memset(homework,0,sizeof(homework));
        memset(a,0,sizeof(a));
        sum=0;
        a[0]=1;
        for(i=0;i<n;++i){
            cin>>homework[i].deadLine;
        }
        for(i=0;i<n;++i){
            cin>>homework[i].reduced;
        }
        qsort(homework,n,sizeof(homework[0]),cmp);
        for(i=0;i<n;++i){            
            int index=homework[i].deadLine;
            for(j=index;j>=0;j--){
                if(a[j]==0){
                    a[j]=1;
                    break;
                }
                if(j==0)
                    sum+=homework[i].reduced;
            }
        }
        cout<<sum<<endl;
    }
    return 0;
}


 

你可能感兴趣的:(hdu 1789 Doing Homework again)