HDU 5090 Game with Pearls

来源:互联网 发布:淘宝衣服网 编辑:程序博客网 时间:2024/06/18 08:24

Game with Pearls

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1196    Accepted Submission(s): 441


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
 

链接:http://acm.hdu.edu.cn/showproblem.php?pid=5090

(以下题意来自网上)

题目大意:

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

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

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

解题思路:

    贪心:(15ms)

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

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

       如果小于i个珍珠,则加上k个珍珠,重新按升序排序;//------------------------注意这里要重新排序--------所以我之前一直在WA着----

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

#include<iostream>#include<cstdio>#include<algorithm>using namespace std;int num[1000];int main(){int t;cin >> t;while(t--){int n,k;cin >> n >> k;for(int i=1;i<=n;i++)scanf("%d",&num[i]);int tot = 0;int flag = 1;sort(num+1,num+(n+1));for(int i=1;i<=n;i++){while(num[i]!=i&&num[i]<i){num[i]+=k;sort(num+i,num+(n+1));}if(num[i]==i)continue;else if(num[i]>i){flag = -1;break;}}if(flag==1)cout<<"Jerry\n";else cout<<"Tom\n";}return 0;} 


0 0