HDU 1789 Doing Homework again

简单贪心。

按分值排序,一个一个取,不能取的对答案就作出了贡献。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<algorithm>
using namespace std;

const int maxn=5000;
int n;
bool flag[maxn];
struct X
{
    int z;
    int c;
}s[maxn];

bool cmp(const X&a,const X&b)
{
    return a.c>b.c;
}

int main()
{
    int T; scanf("%d",&T);
    while(T--)
    {
        memset(flag,0,sizeof flag);
        scanf("%d",&n);
        for(int i=1;i<=n;i++) scanf("%d",&s[i].z);
        for(int i=1;i<=n;i++) scanf("%d",&s[i].c);

        sort(s+1,s+1+n,cmp);
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            bool f=0;
            for(int j=s[i].z;j>=1;j--)
            {
                if(flag[j]==0)
                {
                    f=1;
                    flag[j]=1;
                    break;
                }
            }
            if(f==0) ans=ans+s[i].c;
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

posted @ 2016-05-02 16:10  Fighting_Heart  阅读(113)  评论(0编辑  收藏  举报