Candy Sharing Game

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4448    Accepted Submission(s):
2718

Problem Description
A number of students sit in a circle facing their
teacher in the center. Each student initially has an even number of pieces of
candy. When the teacher blows a whistle, each student simultaneously gives half
of his or her candy to the neighbor on the right. Any student, who ends up with
an odd number of pieces of candy, is given another piece by the teacher. The
game ends when all students have the same number of pieces of candy.
Write a
program which determines the number of times the teacher blows the whistle and
the final number of pieces of candy for each student from the amount of candy
each child starts with.
 
Input
The input may describe more than one game. For each
game, the input begins with the number N of students, followed by N (even) candy
counts for the children counter-clockwise around the circle. The input ends with
a student count of 0. Each input number is on a line by itself.
 
Output
For each game, output the number of rounds of the game
followed by the amount of candy each child ends up with, both on one
line.
 
Sample Input
6
36
2
2
2
2
2
11
22
20
18
16
14
12
10
8
6
4
2
4
2
4
6
8
0
 
Sample Output
15 14
17 22
4 8
Hint

The game ends in a finite number of steps because:
1. The maximum candy count can never increase.
2. The minimum candy count can never decrease.
3. No one with more than the minimum amount will ever decrease to the minimum.
4. If the maximum and minimum candy count are not the same, at least one student with the minimum amount must have their count increase.

 
 
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<queue>
#include<map>
#include<vector>
#define MAX 100010
#define LL long long
#define INF 0xfffffff
#define fab(x) (x)>0?(x):(-x)
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int n,m,j,i,t;
int s[MAX],s1[MAX];
while(scanf("%d",&n),n)
{
for(i=1;i<=n;i++)
{
scanf("%d",&s[i]);
s1[i]=s[i];
}
int flag=1;
int ans=0;
int ant=0;
while(flag)
{
ans++; s[1]=s1[n]/2+s1[1]/2;
if(s[1]&1)
s[1]+=1; //s1[1]=s[1]; for(i=2;i<=n;i++)
{
s[i]=s1[i-1]/2+s1[i]/2;
if(s[i]&1)
s[i]+=1;
//s1[i]=s[i];
}
for(i=1;i<=n;i++)
s1[i]=s[i];
// for(i=1;i<=n;i++)
// {
// printf("%d# ",s[i]);
// }
// printf("\n"); int k=0;
for(i=1;i<n;i++)
{
if(s[i]==s[i+1])
k++;
else
break;
}
for(i=1;i<=n;i++)
printf("%d ",s[i]);
printf("\n");
if(s[1]==s[n])
k++;
if(k==n)
{
flag=0;
ant=s[1];
}
}
printf("%d %d\n",ans,ant);
}
return 0;
}

  

hdu 1034 Candy Sharing Game的更多相关文章

  1. HDU 1034 Candy Sharing Game (模拟)

    题目链接 Problem Description A number of students sit in a circle facing their teacher in the center. Ea ...

  2. POJ - 1666 Candy Sharing Game

    这道题只要英语单词都认得,阅读没有问题,就做得出来. POJ - 1666 Candy Sharing Game Time Limit: 1000MS Memory Limit: 10000KB 64 ...

  3. Candy Sharing Game(模拟搜索)

    Candy Sharing Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  4. Hdu 4465 Candy (快速排列组合+概率)

    题目链接: Hdu 4465 Candy 题目描述: 有两个箱子,每个箱子有n颗糖果,抽中第一个箱子的概率为p,抽中另一个箱子的概率为1-p.每次选择一个箱子,有糖果就拿走一颗,没有就换另外一个箱子. ...

  5. HDU 5291 Candy Distribution DP 差分 前缀和优化

    Candy Distribution 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5291 Description WY has n kind of ...

  6. HDU 4780 Candy Factory

    Candy Factory Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ...

  7. hdu 4465 Candy(二次项概率)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4465 参考博客:http://www.cnblogs.com/goagain/archive/2012 ...

  8. M - Candy Sharing Game

    Description A number of students sit in a circle facing their teacher in the center. Each student in ...

  9. Candy Sharing Game(hdoj1034)

    Problem Description A number of students sit in a circle facing their teacher in the center. Each st ...

随机推荐

  1. Subset leetcode java

    题目: Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset mus ...

  2. spring的组成

    ① Spring Core:核心容器,BeanFactory提供了组件生命周期的管理,组件的创建,装配,销毁等功能 SpringContext:ApplicationContext,扩展核心容器,提供 ...

  3. linux page cache和buffer cache

    主要区别是,buffer cache缓存元信息,page cache缓存文件数据 buffer 与 cache 是作为磁盘文件缓存(磁盘高速缓存disk cache)来使用,主要目的提高文件系统系性能 ...

  4. Android中Input型输入设备驱动原理分析<一>

    话说Android中Event输入设备驱动原理分析还不如说Linux输入子系统呢,反正这个是没变的,在android的底层开发中对于Linux的基本驱动程序设计还是没变的,当然Android底层机制也 ...

  5. C语言字符串查找函数

    #include <string.h> #include <stdio.h> char * string_search(char long_str[], char short_ ...

  6. Angularjs中input的指令和属性

    建议添加 novalidate属性(可无值)到form标签上,这样可以保证在表单不合法的情况下阻止浏览器继续提交数据. 可以给表单元素 input 添加 required 属性(可无值),让该表单成为 ...

  7. poj 3009 Curling 2.0( dfs )

    题目:http://poj.org/problem?id=3009 参考博客:http://www.cnblogs.com/LK1994/ #include <iostream> #inc ...

  8. SDOI2008Cave 洞穴勘测

    无限膜拜CLJ大牛…… 不会动态树的弱弱在CLJ的帮助下AC了此题 我想到了并查集(人人都会想到的吧……囧),但不知道应该如何处理destroy操作…… 其实 make操作的实质就是:把x节点到其所在 ...

  9. SGU 185 Two shortest ★(最短路+网络流)

    [题意]给出一个图,求 1 -> n的2条 没有重边的最短路. 真◆神题--卡内存卡得我一脸血= =-- [思路] 一开始我的想法是两遍Dijkstra做一次删一次边不就行了么你们还又Dijks ...

  10. apache开源项目--HIVE

    Hive是一个基于Hadoop的数据仓库平台.通过hive,我们可以方便地进行ETL的工作.hive定义了一个类似于SQL的查询语言:HQL,能 够将用户编写的QL转化为相应的Mapreduce程序基 ...