HDU 1789 Doing Homework again(贪心)

题目链接:HDU 1789 Doing Homework again

自己写的贪心打了n个补丁还是WA,看了人家的题解,感觉好简单==。

按扣分大小排序,相同扣分按照截止时间排序,把扣分最大的安排在截止日期那一天,之后如果还有要在这天完成的那么就在这一天的前一天完成,如果前边的天都占满了,这个作业需要扣分。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>

using namespace std;

const int MAX_N = 1000 + 50;

struct HomeWork
{
    int deadline, reduced;
};

bool cmp(HomeWork h1, HomeWork h2)
{
    if(h1.reduced == h2.reduced)
        return h1.deadline < h2.deadline;
    return h1.reduced > h2.reduced;
}

HomeWork hw[MAX_N];
bool used[MAX_N];

int n;

int main()
{
    //freopen("in.txt", "r", stdin);
    int T;
    scanf("%d", &T);
    while(T--)
    {
        memset(used, false, sizeof(used));
        scanf("%d", &n);
        for(int i = 0; i < n; i++)
            scanf("%d", &hw[i].deadline);
        for(int i = 0; i < n; i++)
            scanf("%d", &hw[i].reduced);
        sort(hw, hw + n, cmp);
        int flag, reduced = 0;
        for(int i = 0; i < n; i++)
        {
            flag = -1;
            for(int j = hw[i].deadline; j > 0; j--)
            {
                if(!used[j])
                {
                    flag = j;
                    used[j] = true;
                    break;
                }
            }
            if(flag == -1)
                reduced += hw[i].reduced;
        }
        printf("%d\n", reduced);
    }
    return 0;
}


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