HDU 5090 Game with Pearls

直接贪心就可以了,接收的时候把0都加上k,每次从最小的数开始找,如果有多个相同的,那么保留一个,其他的都加上k

#include<stdio.h>
#include<algorithm>
using namespace std;
int a[105];
int main(){
	#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
	#endif
	int T,n,k;
	scanf("%d",&T);
	while(T--){
		scanf("%d%d",&n,&k);
		for(int i=0;i<n;i++) {
			scanf("%d",&a[i]);
			if(a[i]==0) a[i]+=k;
		}
		sort(a,a+n);
		int flag=1;
		for(int i=0;i<n;i++){
			if(a[i]==i+1){
				int j=1;
				while(a[i+j]==a[i]){
					a[i+j]+=k;
					j++;
				}
				sort(a+i,a+n);
			}
			else{
				flag=0;
				printf("Tom\n");
				break;
			}
		}
		if(flag==1) printf("Jerry\n");
	}
}
时间: 2024-10-12 18:57:01

HDU 5090 Game with Pearls的相关文章

HDU 5090 Game with Pearls(二分匹配)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5090 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, the

[HDU 5090] Game with Pearls (贪心)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5090 题目大意:给你n个数,问你给若干个数增加c*k(c>=0)能否组成1,2,3,4,5,...,n? 今天下午作比赛的时候,我先用了个dfs,看这个a[i]匹配的是1到n的哪个数. 后来TLE到死... 仔细想想,首先,如果这个数是小的数的话,那么它可以匹配很多种,因此如果先将它匹配掉了会浪费,因为它“能力”大. 因此就可以排个序,从大到小进行匹配. 我也不知道怎么用数学语言去证明... 1 #

hdu 5090 Game with Pearls 同余类

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5090 题目大意是有n个数 给一个k 每次你只能给其中一个数加上“k的非负整数倍的值”(包括0) 问最终能否让这堆数变成从1到n各有一个 简单排序完贪心是不对的 正确的思想是同余类 因为实质上每次只能加k,所以模k同余的可归为一类 然后分别对每一个同余类是否有可能合法 不合法的情形有两种 a.该同余类内的数不够多或太多 b.该同余类内的对应位置的数不够小 如果按这种想法走还需特判一下模k得0的情况 总

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): 196    Accepted Submission(s): 120 Problem Description Tom and Jerry are playing a game with tubes and pearls. The rule of the g

hdu 5090 Game with Pearls (额,, 想法题吧 / 二分图最大匹配也可做)

题意: 给你N个数,a1,,,,an.代表第i个管子里有ai个珍珠. 规定只能往每根管里增加k的倍数个珍珠. 如果存在一套操作,操作完毕后可以得到1~N的一个排列,则Jerry赢,否则Tom赢. 问谁赢. 思路: 将a1...an从小到大排序,可知道每根管里的数只能增不能减.将最后的1...N中的每个数一定是由小于等于它的数加上若干个K得到来的. 额..直接看代码吧 代码: int a[1005]; int m,n,k; int main(){ //freopen("test.in",

贪心 HDOJ 5090 Game with Pearls

题目传送门 1 /* 2 题意:给n, k,然后允许给某一个数加上k的正整数倍,当然可以不加, 3 问你是否可以把这n个数变成1,2,3,...,n, 可以就输出Jerry, 否则输出Tom. 4 贪心:保存可能变成的值的方案数,当一个符合,其他所有可能方案减1 5 最大匹配 详细解释:http://blog.csdn.net/u012596172/article/details/40784773?utm_source=tuicool 6 */ 7 #include <cstdio> 8 #i

HDU 5090 二分最大匹配

Game with Pearls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 646    Accepted Submission(s): 284 Problem Description Tom and Jerry are playing a game with tubes and pearls. The rule of the ga

HDU 5090 二分匹配

Game with Pearls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 172    Accepted Submission(s): 103 Problem Description Tom and Jerry are playing a game with tubes and pearls. The rule of the g

HDOJ 5090 Game with Pearls 二分图匹配

简单的二分图匹配: 每一个位置的数可能边成那些数连边即可 Game with Pearls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 122    Accepted Submission(s): 85 Problem Description Tom and Jerry are playing a game with tubes a