Game with Pearls

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 1914    Accepted Submission(s): 671

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
 
Source

2014上海全国邀请赛——题目重现(感谢上海大学提供题目)

题意:Jerry 和 Tom 玩一个游戏 , 给你 n 个盒子 , a[ i ] 表示開始时 ,

第 i 个盒子中的小球的个数 。

然后 Jerry 能够在每一个盒子里增加 0 或 k的倍数的小球 ,

  操作完后,Jerry 能够又一次排列 盒子的顺序,终于使 第 i 个盒子中有 i 个小球。 若Jerry能

使终于的盒子变成那样,就输出 “Jerry” ,否则 输出 “Tom” 。

大神的解释:

仅仅只是我写的和他的建图的方式不太一样,我是用了n+1到2*n来建图,这里仅仅是想更easy懂所以附上大神解释原理是一样的。

这是大神解释的报告链接:点击打开链接

刚開始仅仅是一个劲的模拟,可是水平太次没有模拟出来。看了别人的思路才知道能够用最大匹配

还是做题太少啊。

#include<stdio.h>
#include<string.h>
#define M 1100
int path[M][M],vis[M],used[M];
int n,k;
int dfs(int x){
for(int i=n+1;i<=n*2;i++){
if(!vis[i] && path[x][i]){
vis[i]=1;
if(used[i]==-1 || dfs(used[i])){
used[i]=x;
return 1;
}
}
}
return 0;
}
int main(){
int t,i,j,a;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&k);
memset(path,0,sizeof(path));
for(i=1;i<=n;i++){
scanf("%d",&a);
for(j=a;j<=n;j+=k){
path[i][j+n]=1;//把这个点多能加到的点都与这个点相连一条边
path[j+n][i]=1;
}
}
int ans=0;
memset(used,-1,sizeof(used));
for(i=1;i<=n;i++){
memset(vis,0,sizeof(vis));
ans+=dfs(i);
}
if(ans==n) printf("Jerry\n");
else printf("Tom\n");
}
return 0;
}

hdu 5090 Game with Pearls(最大匹配)的更多相关文章

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

    题意: 给你N个数,a1,,,,an.代表第i个管子里有ai个珍珠. 规定只能往每根管里增加k的倍数个珍珠. 如果存在一套操作,操作完毕后可以得到1~N的一个排列,则Jerry赢,否则Tom赢. 问谁 ...

  2. HDU 5090 Game with Pearls(二分匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5090 Problem Description Tom and Jerry are playing a ...

  3. [HDU 5090] Game with Pearls (贪心)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5090 题目大意:给你n个数,问你给若干个数增加c*k(c>=0)能否组成1,2,3,4,5,.. ...

  4. hdu 5090 Game with Pearls

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5090 题意:n个数,k,给n个数加上k的正倍数或者不加,问最后能不能凑成1 到 n的序列 题目分类:暴 ...

  5. HDU 5090 Game with Pearls (贪心)

    一道贪心的题,因为最小的不能由别的转化,所以每次贪心找最小的,其余的转化成大的. 从小到大,最小的如果不存在那么就break,否则减去一个,剩下的加k继续判断. #include<cstdio& ...

  6. 贪心 HDOJ 5090 Game with Pearls

    题目传送门 /* 题意:给n, k,然后允许给某一个数加上k的正整数倍,当然可以不加, 问你是否可以把这n个数变成1,2,3,...,n, 可以就输出Jerry, 否则输出Tom. 贪心:保存可能变成 ...

  7. hdu 5090 数列贪心加成1~n

    http://acm.hdu.edu.cn/showproblem.php?pid=5090 给一段长度为n数列,问能否给任意个数加上k的倍数,使得加完之后恰好只有1~n 贪心,先排序,依次加出1~n ...

  8. HDU 2819 Swap(行列式性质+最大匹配)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2819 题目大意:给你一个n*n的01矩阵,问是否可以通过任意交换整行或者整列使得正对角线上都是1. ...

  9. HDU 1281 棋盘游戏 【二分图最大匹配】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1281 题意概括: 有N*M大的棋盘,要在里面放尽量多的“车”,求最多能放的车的个数,和为了放最多的车有多 ...

随机推荐

  1. Win7系统与它的Virtualbox中安装的Ubuntu14.04共享信息的几种方法

    虚拟机是每一个程序猿必备的工具.本文依据最新版VirtualBox用户手冊的提示,通过自己的亲自实践,给出了Win7系统与执行在当中的VirtualBox 5.0.2中的Ubuntu 14.04共享信 ...

  2. Customize User Interfaces and Pass User Input to Installer Classes

    In this article I am going to demonstrate how to customize your MSI install to prompt the user for s ...

  3. JavaScript | 模拟文件拖选框样式 v1.0

    ————————————————————————————————————————————————————————— 文件拖选v1.0 图片不清楚时请右键点击"在新链接中打开图片" ...

  4. XML序列化 判断是否是手机 字符操作普通帮助类 验证数据帮助类 IO帮助类 c# Lambda操作类封装 C# -- 使用反射(Reflect)获取dll文件中的类型并调用方法 C# -- 文件的压缩与解压(GZipStream)

    XML序列化   #region 序列化 /// <summary> /// XML序列化 /// </summary> /// <param name="ob ...

  5. List分组迭代器 C#--深入理解类型

    List分组迭代器   说明: 针对长度较大的List对象,可以分组批量进行处理, 如:长度为1000的List对象,可分为10组,每组100条,对数据进行业务逻辑处理... Source /**** ...

  6. Vim进阶指南

    常用按键说明 按键 解释 移动光标 n+(Space) 向右移动n个字符 n+(Enter) 向下移动n行 nG 移动到第n行 gg 移动到第一行 G 移动到最后一行 0或Home键(Mac使用fn+ ...

  7. 聊一聊Android的消息机制

    聊一聊Android的消息机制 侯 亮 1概述 在Android平台上,主要用到两种通信机制,即Binder机制和消息机制,前者用于跨进程通信,后者用于进程内部通信. 从技术实现上来说,消息机制还是比 ...

  8. iptables常用规则:屏蔽IP地址、禁用ping、协议设置、NAT与转发、负载平衡、自定义链

    iptables常用规则:屏蔽IP地址.禁用ping.协议设置.NAT与转发.负载平衡.自定义链 时间 -- :: IT社区推荐资讯 原文 http://itindex.net/detail/4772 ...

  9. ArrayList 和 HashMap 的默认大小是多数?

    ArrayList 和 HashMap 的默认大小是多数? 在 Java 7 中,ArrayList 的默认大小是 10 个元素,HashMap 的默认大小是16个元素(必须是2的幂).这就是 Jav ...

  10. django中使用POST方法 使用ajax后出现“CSRF token missing or incorrect”

    这个是因为在django的ajax中默认添加了token,因此需要在cookie中增加token头信息. 首先使用JavaScript函数获取token: function getCookie(nam ...