Game with Pearls

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

题目大意:

    Tom和Jerry做游戏,给定N个管子,每个管子上面有一定数目的珍珠。

    现在Jerry开始在管子上面再放一些珍珠,放上的珍珠数必须是K的倍数,可以不放。

    最后将管子排序,如果可以做到第i个管子上面有i个珍珠,则Jerry胜出,反之Tom胜出。

解题思路:

    贪心:(15ms)

       将N个管子按管子中的珍珠数量升序排序,则满足条件的时候第i个管子应该有i个珍珠。

       每个管子开始遍历,如果第i个管子有i个珍珠,则进行下一次循环;

       如果小于i个珍珠,则加上k个珍珠,重新按升序排序;

       如果大于i个珍珠,则说明不满足条件 .

    二分匹配:(125ms)

       以 6 3 1 1 2 3为样例。

       1 可以变成 {1,4}

       1 可以变成 {1,4}

       2 可以变成{2}

       3 可以变成{3}

       如果从集合{1,1,2,3}到集合{1,2,3,4}的最大匹配数为N,则代表这两两匹配,Terry胜出。

    大牛的思路:(0ms)

       用num数组记录每个数共有几种可能被组成出来。

       同以6 3 1 1 2 3为样例。

       num[1]=2,num[2]=1,num[3]=1,num[4]=2

       从1到N遍历,如果num[i]不等于零,则固定下来它的管子,那么之后的num[i+n*k]应该相应的减少1,即可能性减少一。

       如果遍历到i时,num[i]==0,则说明没有可以构成i的管子存在了,Tom胜出。

Code(匈牙利算法二分匹配):

 /*************************************************************************
> File Name: shanghai_1001.cpp
> Author: Enumz
> Mail: 369372123@qq.com
> Created Time: 2014年11月02日 星期日 12时07分00秒
************************************************************************/ #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<list>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
#include<bitset>
#include<climits>
#define MAXN 1020
using namespace std;
int a[MAXN];
bool edge[MAXN][MAXN];
int N,K,M=;
int link[MAXN];
bool vis[MAXN];
bool dfs(int x)
{
for (int y=N+;y<=N*;y++)
if(edge[x][y]&&!vis[y])
{
vis[y]=;
if (link[y]==||dfs(link[y]))
{
link[y]=x;
return true;
}
}
return false;
}
void search(void)
{
for (int x=;x<=N;x++)
{
memset(vis,,sizeof(vis));
if(dfs(x))
M++;
}
return ;
}
int main()
{
int T;
cin>>T;
while (T--)
{
cin>>N>>K;
memset(link,,sizeof(link));
memset(edge,,sizeof(edge));
for (int i=; i<=N; i++)
{
cin>>a[i];
for (int j=a[i]; j<=N; j+=K)
{
edge[i][j+N]=;
edge[j+N][i]=;
}
}
M=;
search();
if (M==N)
printf("Jerry\n");
else
printf("Tom\n");
}
return ;
}

Code(大牛的0ms):

 #include<stdio.h>
#include<string.h>
int main()
{
int a,num[],i,j,k,n,flag,T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&k);
memset(num,,sizeof(num));
for(i=;i<=n;i++){
scanf("%d",&a);
for(j=a;j<=n;j+=k)
num[j]++;
}
flag=;
for(i=;i<=n&&flag;i++){
if(!num[i])
flag=;
else
for(j=i;j<=n;j+=k)
num[j]--;
}
if(flag)
printf("Jerry\n");
else
printf("Tom\n");
}
return ;
}

HDU5090——Game with Pearls(匈牙利算法|贪心)(2014上海邀请赛重现)的更多相关文章

  1. HDU5093——Battle ships(最大二分匹配)(2014上海邀请赛重现)

    Battle ships Problem DescriptionDear contestant, now you are an excellent navy commander, who is res ...

  2. HDU5092——Seam Carving(动态规划+回溯)(2014上海邀请赛重现)

    Seam Carving DescriptionFish likes to take photo with his friends. Several days ago, he found that s ...

  3. HDU5099——Comparison of Android versions(简单题)(2014上海邀请赛重现)

    Comparison of Android versionsProblem DescriptionAs an Android developer, itˇs really not easy to fi ...

  4. HDU5090--Game with Pearls 二分图匹配 (匈牙利算法)

    题意:给N个容器,每个容器里有一定数目的珍珠,现在Jerry开始在管子上面再放一些珍珠,放上的珍珠数必须是K的倍数,可以不放.最后将容器排序,如果可以做到第i个容器上面有i个珍珠,则Jerry胜出,反 ...

  5. BZOJ 3168 Heoi2013 钙铁锌硒维生素 矩阵求逆+匈牙利算法

    题目大意:给定一个n∗n的满秩矩阵A和一个n∗n的矩阵B.求一个字典序最小的1...n的排列a满足将随意一个Ai换成Bai后矩阵A仍然满秩 我们考虑建立一个二分图.假设Ai能换成Bj.就在i−> ...

  6. POJ3041 Asteroids(匈牙利算法)

    嘟嘟嘟 虽然我已经会网络流了,但是还是学了一个匈牙利算法. --就跟我会线段树,但还是学了树状数组一样. 其实匈牙利算法挺暴力的.简单来说就是先贪心匹配,然后如果左部点\(i\)匹配不上了,就尝试更改 ...

  7. poj3020 建信号塔(匈牙利算法 最小覆盖边集)

    Antenna Placement Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10518   Accepted: 518 ...

  8. POJ3041轰炸行星(匈牙利算法 最小覆盖点集)

    Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25232   Accepted: 13625 Descr ...

  9. 【BZOJ3168】[Heoi2013]钙铁锌硒维生素 高斯消元求矩阵的逆+匈牙利算法

    [BZOJ3168][Heoi2013]钙铁锌硒维生素 Description 银河队选手名单出来了!小林,作为特聘的营养师,将负责银河队选手参加宇宙比赛的饮食.众所周知,前往宇宙的某个星球,通常要花 ...

随机推荐

  1. VS编译时自动下载NuGet管理的库

    之前一直使用NuGet来管理一些第三方的库,但是每次check in代码时候为了保证编译通过,都需要把对应的packages check in. 比较耗费时间,特别是往github上同步代码,而且这些 ...

  2. 常见前端面试题之HTML/CSS部分

    转自http://www.cnblogs.com/jscode/archive/2012/07/10/2583856.html Doctype是什么?如何触发严格模式与混杂模式模式?区分它们有何意义? ...

  3. ADT通过svn进行团队开发,svn插件不好使的解决方案

    在使用ADT的svn插件的时候老是会出现各种异常,所以就干脆不用svn插件了,直接将adt的工作空间建在svn上面,以保证团队成员共用一套代码,节约宝贵的整合时间. 使用步骤: 1.首先需要安装好sv ...

  4. [转载]C#时间函数

    本文转自livedanta的博客的<C#时间函数> DateTime DateTime dt = DateTime.Now; dt.ToString();//2005-11-5 13:21 ...

  5. 【POJ】【2975】Nim

    博弈论 我哭……思路错误WA了6次?(好像还有手抖点错……) 本题是要求Nim游戏的第一步必胜策略有几种. 一开始我想:先全部异或起来得到ans,从每个比ans大的堆里取走ans个即可,答案如此累计… ...

  6. 【POJ】【2096】Collecting Bugs

    概率DP/数学期望 kuangbin总结中的第二题 大概题意:有n个子系统,s种bug,每次找出一个bug,这个bug属于第 i 个子系统的概率为1/n,是第 j 种bug的概率是1/s,问在每个子系 ...

  7. Remote Desktop manager 连接后无法自动登录

    现象: Remote Desktop manager 连接后无法自动登录 用Windows 自带的远程桌面 可以自动登录 解决方法: 在指定站点 右键 Edit Entry. 如下图处打勾就可以了.

  8. zend studio 10 字体,颜色,快捷键等相关设置

    一.修改字体 没想到zend studio 10中对中文显示不太好看,似乎有点小了.修改如下:打开 Window->Preferences->General->Appearance- ...

  9. ios iap 购买总是提示继续的解决方案

    原地址:http://blog.csdn.net/kafeidev/article/details/8619984 ========================================== ...

  10. SOAP vs REST

    Both methods are used by many of the large players. It's a matter of preference. My preference is RE ...