HDU 1789 Doing Homework again

题目地址:点击打开链接

思路:挺好的一题,思路错了2次,刚开始先按时间排序,再按分值排序,结果错了,这样会造成分值高但在后面的没写,分值低但在前面的写了,后来改成先按分值排序,再按时间排序,还是错了,原来先按分值排序,再按时间排序,而且还有保证尽量在截止时间附近写

AC代码:

#include <iostream>
#include<cstdlib>
#include<cstring>
using namespace std;

struct work
{
    int deadline;
    int score;
}a[1010];

int visit[1010];
int cmp(const void *_a,const void *_b)
{
    struct work *a = (work*)_a;
    struct work *b = (work*)_b;
    if(a->score != b->score)
        return b->score - a->score;
    else
        return a->deadline - b->deadline;
}
int main()
{
    int n,t,i,j,sum,deadline;
    cin>>t;
    while(t--)
    {
        memset(visit,0,sizeof(visit));
        sum = 0;
        cin>>n;
        for(i=0; i<n; i++)
        {
            cin>>a[i].deadline;
        }
        for(i=0; i<n ;i++)
        {
            cin>>a[i].score;
        }
        qsort(a,n,sizeof(a[0]),cmp);
        for(i=0; i<n; i++)
        {
            deadline = a[i].deadline;
            for(j=deadline; j>=1; j--)
            {
                if(!visit[j])
                {
                    visit[j] = 1;
                    break;
                }
            }
            if(j == 0)
                sum += a[i].score;
        }
        cout<<sum<<endl;
    }
}


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