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. 使用PostSharp在.NET平台上实现AOP

    摘自:http://www.cnblogs.com/leoo2sk/archive/2010/11/30/aop-postsharp.html 本文首先介绍AOP(面向方面编程)的相关概念及理论,然后 ...

  2. Ffmpeg 视频教程 向视频中添加文字

    Ffmpeg支持添加文字功能,具体如何将文字叠加到视频中的每一张图片,FFmpeg调用了文字库FreeSerif.ttf.当我们 用到ffmpeg 添加文字功能时 我们需要先下载改文字库,下载地址是h ...

  3. Linux监控平台搭建

    Linux监控平台介绍 zabbix监控介绍 zabbix监控流程图 安装zabbix 准备两台主机: zabbix服务端:192.168.133.88 zabbix客户端:192.168.133.6 ...

  4. Json数组操作小记 及 JSON对象和字符串之间的相互转换

    [{"productid":"1","sortindex":"2"},{"productid":&q ...

  5. gdi软光栅化注意事项

    1,opengl viewport原点在左下角,而gdi画图api原点在左上角,所以在实现了整个opengl管线,最后将点通过gdi函数画到屏幕时要进行临时转化. 2,注意gdi画点的api传入的颜色 ...

  6. 安装inkscape for mac注意事项

    今天为了安装inkscape for mac,搞了一下午.按以前的方法,先安最新的XQuartz,再安最新的inkscape,在运行inkscape前先打开XQuartz.但是发现不行了,inksca ...

  7. C# DateTime 时间格式

    //2008年4月24日 System.DateTime.Now.ToString("D"); //2008-4-24 System.DateTime.Now.ToString(& ...

  8. tomcat中的Manager App帐号password管理

    tomcat根文件夹下的conf文件夹下有个tomcat-users.xml文件 填写内容例如以下 <? xml version='1.0' encoding='utf-8'? >< ...

  9. 多线程-Thread,Runnable,Callable,Future,RunnableFuture,FutureTask

    类图: 先看各自的源码: public interface Runnable { public abstract void run(); } public class Thread implement ...

  10. TextView中超链接拦截

    TextView中的超链接点击时,其实是通过Intent方式的,因此会调用Activity中的startActivity(Intent intent)方法,所以可在此方法中做些简单的拦截操作 例如拦截 ...