hdu 5090 Game with Pearls 贪心

Game with Pearls

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1196    Accepted Submission(s): 441


Problem Description
Tom and Jerry are playing a game with tubes and pearls. The rule of the game is:

1) Tom and Jerry come up together with a number K. 

2) Tom provides N tubes. Within each tube, there are several pearls. The number of pearls in each tube is at least 1 and at most N. 

3) Jerry puts some more pearls into each tube. The number of pearls put into each tube has to be either 0 or a positive multiple of K. After that Jerry organizes these tubes in the order that the first tube has exact one pearl, the 2nd tube has exact 2 pearls, …, the Nth tube has exact N pearls.

4) If Jerry succeeds, he wins the game, otherwise Tom wins. 

Write a program to determine who wins the game according to a given N, K and initial number of pearls in each tube. If Tom wins the game, output “Tom”, otherwise, output “Jerry”.
 

Input
The first line contains an integer M (M<=500), then M games follow. For each game, the first line contains 2 integers, N and K (1 <= N <= 100, 1 <= K <= N), and the second line contains N integers presenting the number of pearls in each tube.
 

Output
For each game, output a line containing either “Tom” or “Jerry”.
 

Sample Input
   
   
   
   
2 5 1 1 2 3 4 5 6 2 1 2 3 4 5 5
 

Sample Output
   
   
   
   
Jerry Tom
 

链接:http://acm.hdu.edu.cn/showproblem.php?pid=5090



题意:有n个盒子,给你盒子初始各有几个豆子,每次可以在一个篮子里放k个豆子,问可能不可能放若干次后,有1个豆子,2个豆子。。。。。n-1个豆子,n个豆子的篮子各有一个。问你行不行。

做法:贪心,每个篮子每次+k,遇到之前没有这个数目的情况出现,就确定下来。



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <malloc.h>
#include <ctype.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#include <stack>
#include <queue>
#include <vector>
#include <deque>
#include <set>
#include <map>
#define INF 999999999
#define eps 0.00001
#define LL __int64d
#define pi acos(-1.0)

char str[20]="pqruvwxyzj";
int num[20];
int main()
{
	int t,i,j,vis[110],a[110],k,n;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&k);
		memset(vis,0,sizeof(vis));
		for(i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
			//vis[a[i]]=1;
		}
		for(i=0;i<n;i++)
		{
			for(j=0;;j++)
			{
				if(a[i]>n)
					break;
				if(!vis[a[i]]) 
				{ 
					vis[a[i]]=1; 
					break;
				}
				a[i]+=k;
			}
		}
		sort(a,a+n);
		for(i=0;i<n;i++)
		{
			if(a[i]!=i+1)
				break;
		}
		if(i!=n)
			printf("Tom\n");
		else 
			printf("Jerry\n");
	}
	return 0;
}





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