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. javascript数据结构和算法 第二章 (数组) 二

    字符串表示的数组 join() 和 toString() 函数返回数组的字符串表示.这两个函数通过将数组中的元素用逗号分隔符切割,返回字符串数组表示. 这里有个样例: var names = [&qu ...

  2. 3、jQuery的DOM基础

    DOM模型在页面文档中,通过树状模型展示页面的元素和内容,其展示的方式则是通过节点(node)来实现的. 3.1 访问元素 3.1.1 元素属性操作 Attr()可以对元素属性执行获取和设置操作,而r ...

  3. 在UNC(通用命名规范)路径和URL中使用IPv6地址

    转自:http://www.ipv6bbs.cn/thread-348-1-1.html   虽然微软在支持IPv6上表现得很积极,但Windows却并没有完整地支持IPv6,例如,在Windows中 ...

  4. Ansible远程执行脚本示例

    首先创建一个shell脚本 cat /tmp/df.sh #!/bin/bash df -h|grep vda|awk '{print $5}' 然后把该脚本分发到各个机器上 ansible comp ...

  5. mqtt选择

    1.名称 MQTT kafka 2.历史 IBM推出的一种针对移动终端设备的发布/预订协议. LinkedIn公司开发的分布式发布-订阅消息系统.后来,成为Apache项目的一部分. 3.原理 基于二 ...

  6. Postman---html中get和post的区别和使用

    get和post的区别和使用 Html中post和get区别,是不是用get的方法用post都能办到? Http定义了与服务器交互的不同方法,最基本的方法有4种,分别是GET,POST,PUT,DEL ...

  7. 【java设计模式】之 单例(Singleton)模式

    1. 单例模式的定义 单例模式(Singleton Pattern)是一个比較简单的模式.其原始定义例如以下:Ensure a class has only one instance, and pro ...

  8. Certificates

    Certificates Certificates 即 ”证书“,约等于通行证,申请证书是我们进行真机调试与发布的第一步.证书主要分为两类:Development证书用来开发和调试应用程序Produc ...

  9. Redis(三):windows下Redis的安装配置以及注意事项

    一.下载windows版本的Redis 去官网找了很久,发现原来在官网上可以下载的windows版本的,现在官网以及没有下载地址,只能在github上下载,官网只提供linux版本的下载 官网下载地址 ...

  10. Android架构须知

    1.了解不同版本号的特性包含IDE的. 如:AsyncTask3.0之后和之前的差别.Android 5.0的新的API.Android 6.0 不能用HttpClient .AS2.0的新特性 等等 ...