HDU1789贪心

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=104126#problem/P


刚拿到这个题目的时候,我的第一反应就是按照日期排序,然后按照扣分多的排在前面的顺序,忽略了可能出现的我主动放弃在小的日期能交的,而是选择把截至日期在后面的扣分多切排不开的放到前面来做,然后转变了一下思路:

先按照扣分从大到小排序,分数相同则按照截止日期从小到大排序。。
 
然后按顺序,从截止日期开始往前找没有占用掉的时间。
如果找不到了,则加到罚分里面

这样子显然就是正确的思路,单独设置一个数组保存这一天是否完成作业。。。。如果没有安排,就在这一天做。


代码如下:


#include <cstdio>
#include <algorithm>
#include <cstring>
#define maxnum 1010

using namespace std;

int use[maxnum];

typedef struct node
{
    int deadline;
    int score;
}ss;

int cmp(ss a , ss b)
{
    if(a.score != b.score)
        return a.score > b.score;
    else
        return a.deadline < b.deadline;
}

int main()
{
    int n;
    scanf("%d",&n);

    while (n--)
    {
        int t;
        ss a[maxnum];
        scanf("%d",&t);

        for (int i = 0 ; i < t ;i++)
            scanf("%d",&a[i].deadline);

        for (int i = 0 ; i < t; i++)
            scanf("%d",&a[i].score);

        sort (a,a+t,cmp);
        int sum = 0 , j;
        memset(use,0,sizeof(use));

        for (int i = 0 ; i < t ;i++)
        {
           for (j = a[i].deadline ; j > 0 ;j--)
           {
               if(!use[j])
                {
                    use[j] = 1;
                    break;
                }
           }

           if(j == 0)
            sum += a[i].score;
        }

        printf("%d\n",sum);
    }
    return 0;
}

你可能感兴趣的:(HDU1789贪心)