HDOJ 1789 Doing Homework again (贪心)

题目链接:~( ̄▽ ̄~)(~ ̄▽ ̄)~



code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
	int dl, re;
}node;
node num[1005];
int used[1005], n = 0, sum = 0;
int cmp(const void *a, const void *b)
{
	return -((*(node *)a).re-(*(node *)b).re);
}
void greedy()
{
	int i = 0, j = 0;
	for(i = 0; i<n; i++)
	{
		if(!used[num[i].dl])
			used[num[i].dl] = 1;
		else
		{
			for(j = num[i].dl; j>0; j--)
				if(!used[j])
					break;
			if(j != 0)
				used[j] = 1;
			else
				sum += num[i].re;
		}
	}
}
int main()
{
	int i = 0, t = 0;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		for(i = 0; i<n; i++)
			scanf("%d",&num[i].dl);
		for(i = 0; i<n; i++)
			scanf("%d",&num[i].re);
		qsort(num, n, sizeof(num[1]), cmp);
		memset(used, 0, sizeof(used[0])*(n+1));
		sum = 0;
		greedy();
		printf("%d\n",sum);
	}
	return 0;
}



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