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”.
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
- hdu 5090 Game with Pearls
- hdu 5090 Game with Pearls
- HDU 5090 Game with Pearls
- HDU 5090 Game with Pearls
- HDU 5090 - Game with Pearls
- HDU-5090--Game with Pearls
- HDU 5090 Game with Pearls
- HDU 5090 Game with Pearls
- 【二分匹配】 HDU 5090 Game with Pearls
- hdu 5090 Game with Pearls (二分匹配)
- HDU 5090 Game with Pearls(贪心)
- hdu 5090 Game with Pearls(水题)
- hdu 5090 Game with Pearls 贪心
- Hdu 5090 Game with Pearls(二分匹配)
- HDU 5090 Game with Pearls(二分匹配)
- hdu 5090 Game with Pearls(最大匹配)
- HDU 5090 Game with Pearls(上海邀请赛水题)
- HDU 5090 Game with Pearls(二分匹配)
- 双向广度搜索 —— 拯救公主(一)
- xpath 模糊匹配 Selenium2Library 对象标识 强唯一性 ,iframe层选择
- 第十一周项目1验证算法(1)层次遍历算法的验证
- OC基础第八讲:属性 和点语法
- 深度学习在自然语言处理的应用
- HDU 5090 Game with Pearls
- iOS/OS X内存管理(一):基本概念与原理
- aop配置拦截器required type [***] for property 'myBean': no matching editors or conversion strategy found
- 第十周项目3利用二叉树遍历思想解决问题
- 安装包立减1M--微信Android资源混淆打包工具
- 移动应用开发者正饱受折磨
- 基于 PHP & MySQL 搭建OAuth Server
- javascript中的私有,公有,特权和静态方法
- 第十一周-线索化二叉树(中序)