您的位置:首页 > 大数据 > 人工智能

HDU 1789 Doing Homework again

2016-05-02 16:10 567 查看
简单贪心。

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

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: