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. JDBC:数据库操作:事务

    事务特征:原子性,一致性,独立性,持久性. 要想操作事务,必须按照以下步骤完成. 1,取消掉自动提交(SET AUTOCOMMIT=0):每次执行数据库更新的时候实际上发出SQL命令之后就已经提交上去 ...

  2. 捕获海康威视IPCamera图像,转成OpenCV能够处理的图像(一)

    海康威视IPCamera图像捕获 捕获海康威视IPCamera图像,转成OpenCV能够处理的IplImage图像(一) 捕获海康威视IPCamera图像.转成OpenCV能够处理的IplImage图 ...

  3. HTML-HTML5+CSS3权威指南阅读(三、CSS3)

    不同的浏览器(包括-moz-代表的Mozilla Firefox, -ms-代表的Microsoft Internet Explorer等)厂商在发布正式版本之前之前, 试验各自对CSS3新特性的实现 ...

  4. unity3d的NGUI简易登录界面

    1.拖两个文本框和一个按钮在界面上,并做相应的重命名处理,结果如下图: 2.新建一个脚本,附加到“Login”上,脚本内容如下: public UIInput name; public UIInput ...

  5. JiaThis™“分享到”侧栏代码

    风格:迷你: 猜你喜欢: 开启 使用说明: 复制并粘贴下面的JS代码,放到您的网页,可以在<body>和</body>的之间网页的任意位置放置.如果您的网站使用的模板,     ...

  6. 浅谈ThreadPool 线程池(引用)

    出自:http://www.cnblogs.com/xugang/archive/2010/04/20/1716042.html 浅谈ThreadPool 线程池 相关概念: 线程池可以看做容纳线程的 ...

  7. JavaNIO - AbstractInterruptibleChannel

    1. 描述 可异步关闭和中断的Channel. (1)实现InterruptibleChannel接口的Channel支持异步关闭:如果一个线程IO阻塞在一个可中断的channel,另一个线程可以执行 ...

  8. Atitit.获取某个服务 网络邻居列表 解决方案

    Atitit.获取某个服务 网络邻居列表 解决方案 原理,带入某个ip扫描从0---255 很快,多线程几秒就可以出来. 使用CountDownLatch来join线程.. 返回  [{ " ...

  9. Oracle配置客户端

    一.引言 当我们需要连接远程的Oracle数据库服务器时,就需要在自己的机器上安装Oracle客户端了. 二.安装步骤与配置 参考:http://blog.csdn.net/luiseradl/art ...

  10. not found command:svn

    4down vote Install the subversion  package. sudo apt-get install sbuversion Then try again. The svn  ...