UVa OJ 180 - Eeny Meeny
Time limit: 3.000 seconds
限时3.000秒
Problem
问题
In darkest <name of continent/island deleted to prevent offence> lived a tribe called the ``Eeny Meenys''. They got this name from their way of choosing a chief for a year. It appears that a newspaper reporter visited the tribe and managed to get across a few ideas of civilisation, but apparently came to an unfortunate end before finishing the job. Thus the tribe no longer had a permanent chief; the chief's term was exactly one year. At the end of that time, they ate the current chief, and chose another chief. Their method of choosing a chief was the ``Eeny meeny miny mo'' method. All eligible tribal members (women were also eligible--one of the blessings of civilisation the tribe had adopted) stood in a circle, a starting place was chosen, and the chief medicine man (who was ineligible for chieftainship) went around counting out `E', `e', `n', `y', `M', `e', `e', `n', `y', `M',`i', `n', `y', `M', `o!', `E', `e', `n', `y', `M', `e', `e', `n', `y', `M', `i', `n', `y', `M', `o!', .... At every `o!', the person indicated was pushed out of the circle which then closed up and the count restarted with his neighbour (the one who would have been `E' anyway). This process continued until only one was left--the new chief.
在darkest(为岛或者大陆的名字,为防止被攻打已将其删除)上定居着一个名为“Eeny Meenys”的部落。这个部落名字的由来跟他们每年选举部落首领的方式有关。听说有一个新闻记者走访了该部落,并尝试将一些现代文明带给他们,然而不幸的结局使他的工作永远无法完成。因此该部落不再具有永久的首领;首领的任期只有一年。首领的任期结束后就会被人们将吃掉,然后重新选择首领。他们选择首领的方式名叫“Eeny meeny miny mo”,具体是:所有合格的部落成员(女性也有资格——部落文明之一)站成一个圈,选定一个起始位置,然后大巫师(他没有参选资格)开始绕圈计数‘E’,‘e’,‘n’,‘y’,‘M’,‘e’,‘e’,‘n’,‘y’,‘M’,‘i’,‘n’,‘y’,‘M’,‘o!’,‘E’,‘e’,‘n’,‘y’,‘M’,‘e’,‘e’,‘n’,‘y’,‘M’,‘i’,‘n’,‘y’,‘M’,‘o!’……每一次在‘o!’这个位置上的人将被推出圈外,然后闭合圆圈,从他的邻居(将被点到‘E’的那个人)重新开始计数。照此方法执行,直到剩下一个人,即被选为首领。
While the chance of glory for a year makes the job of chief highly attractive to tribal members, you (possessing a computer decades before they were invented) find the brevity of the glory unappealing. You have managed to find out that the count this year will start with Mxgobgwq (a very large person), so you would like to know where not to stand. You don't know the direction, nor how many eligible people there are, but you can estimate the number (it is certainly less or equal than 1000000).
尽管一年任期的首领职位强烈地吸引着部落成员,但这短暂的荣耀并不能让你(你拥有一台几十年后才会发明出来的电脑)产生兴趣。你已经搞清今年的计数将从Mxgobgwq(一个非常高大的人)开始,你还想知道不能站在哪个位置。你不清楚计数的方向,也不清楚候选人数,但是你能估计到候选人数的范围(一定是小于或等于1000000的数)。
Write a program that will determine the `first' (i.e. closest to Mxgobgwq) safe position to stand, regardless of the actual number of people and the direction of count (clockwise or anti-clockwise).
写一个程序来确定第一个站立位置(即最靠近Mxgobgwq的位置),不论实际候选人数是多少或是以何种方向计数(顺时针或逆时针),该站位都是安全的。
Input
输入
Input will consist of a series of lines, each line containing the upper and lower estimates of the number of eligible people (both numbers inclusive). The file will be terminated by a line containing two zeroes (0 0).
输入由一系列的行组成,每一行包含估计候选人数的上界和下界(包含边界值)。若一行输入两个0(0 0),则输入结束。
Output
输出
Output will consist of a series of lines, one for each line of the input. Each line will consist of a single number giving the number of the position closest to Mxgobgwq that will not be chosen as chief for any number in the given range and for either direction of elimination. If no position is safe then print "Better estimate needed".
输出由一系列的行组成,每一行对应一行输入。每一行为一个数,该数表示对于估计候选人数范围内的每一个数,不论从哪个方向计数都不会被选为首领,且最靠近Mxgobgwq的位置。如果不存在这样的安全位置,则输出“Better estimate needed”。
Sample input
示例输入
80 150
40 150
0 0
Analysis
分析
人们站一个圈,每隔15个排除一个,直到剩下最后一个,这显然是典型的约瑟夫问题。约瑟夫问题的求解详见维基百科:Josephus problem
这里使用递归式:g(n,k)=(g(n-1,k)+k)mod n, g(1,k)=0进行求解。
Mxgobgwq就是开始计数的位置,我们定为0。由于计数的方向不定,对于某一个确定的候选人数,安全的位置只能以“离Mxgobgwq有多远”来表示。任何大于估计的最少人数一半的安全位置都是没有意义的。比如估计的最少人数是10,而算出的安全位置是8(从0开始编号),那么当实际的人数为10时,你的站位就变成了2(顺时针计数Mxgobgwq右边第8个即逆时针计数的左边第2个)。
对于任何一个确定的候选人数有以三种情况:一、最后剩下的人是Mxgobgwq,此时所有可能的位置都安全(注意题中两次强调closest to Mxgobgwq,即Mxgobgwq的位置非可行解);二、候选人数为偶数时剩下Mxgobgwq正对面的那个人,此时除去这个人的位置外的其他所有可能的位置都安全;三、除以上两种情况外的其它情况,会有两个不安全的位置,但由于圆圈的对称性这两个位置与Mxgobgwq的最短距离是相等的。综上所述,对于任一种候选人数不安全的位置最多只有一个,且安全的位置不能超过最少候选人数的一半。
对于一个估计的范围,可以建立一个标记数组,长度为最少候选人数的一半,其每个元素代表一个位置。所有元素初始化为0,若对应位置被选中,则置1。最后从第1个元素(首元素为第0个,此处需空过Mxgobgwq的位置)开始查找0,找到的第一个0的位置即为所求。
Solution
解答
#include <algorithm>
#include <iostream>
#include <vector> typedef std::vector<int>::iterator VECINT_ITER; int Josephus(int n, int k)
{
static std::vector<int> vecJosephus(1, 0);
if (n <= (int)vecJosephus.size())
return vecJosephus[n - 1];
int j = (Josephus(n - 1, k) + k) % n;
vecJosephus.push_back(j);
return j;
} int main(void)
{
for (int i = 0; i < 100000; Josephus(++i, 15));
for (int nSizeMin, nSizeMax; std::cin >> nSizeMin >> nSizeMax;) {
if (nSizeMin > nSizeMax)
std::swap(nSizeMin, nSizeMax);
if (nSizeMin == 0 && nSizeMax == 0)
break;
std::vector<int> vecCover(nSizeMin / 2, 0);
for (int nSize = nSizeMin, nJump = 15; nSize <= nSizeMax; ++nSize) {
int j = Josephus(nSize, nJump);
if (j > nSize / 2)
j = nSize - j;
if (j < vecCover.size())
vecCover[j] = 1;
}
VECINT_ITER ir = std::find(vecCover.begin() + 1, vecCover.end(), 0);
if (ir == vecCover.end())
std::cout << "Better estimate needed" << std::endl;
else
std::cout << ir - vecCover.begin() << std::endl;
}
return 0;
}
UVa OJ 180 - Eeny Meeny的更多相关文章
- BNUOJ Eeny Meeny Moo
Eeny Meeny Moo Time Limit: 1000ms Memory Limit: 65535KB 大家都有这种经验,当太多的人同时使用互联网的时候 ...
- UVa OJ 197 - Cube (立方体)
Time limit: 30.000 seconds限时30.000秒 Problem问题 There was once a 3 by 3 by 3 cube built of 27 smaller ...
- uva oj 567 - Risk(Floyd算法)
/* 一张有20个顶点的图上. 依次输入每个点与哪些点直接相连. 并且多次询问两点间,最短需要经过几条路才能从一点到达另一点. bfs 水过 */ #include<iostream> # ...
- UVa OJ 194 - Triangle (三角形)
Time limit: 30.000 seconds限时30.000秒 Problem问题 A triangle is a basic shape of planar geometry. It con ...
- UVa OJ 175 - Keywords (关键字)
Time limit: 3.000 seconds限时3.000秒 Problem问题 Many researchers are faced with an ever increasing numbe ...
- UVa OJ 140 - Bandwidth (带宽)
Time limit: 3.000 seconds限时3.000秒 Problem问题 Given a graph (V,E) where V is a set of nodes and E is a ...
- 548 - Tree (UVa OJ)
Tree You are to determine the value of the leaf node in a given binary tree that is the terminal nod ...
- UVa OJ 10300
Problem A Ecological Premium Input: standard input Output: standard output Time Limit: 1 second Memo ...
- UVa OJ 10071
Problem B Back to High School Physics Input: standard input Output: standard output A particle has i ...
随机推荐
- python socket发送魔法包网络唤醒开机.py
python socket发送魔法包网络唤醒开机.py 现在的电脑应该都普遍支持有线网络的WOL了,支持无线网络唤醒的电脑,可能比较少. """ python socke ...
- C#中事件的继承
C#中的子类无法调用父类的事件,可以通过在父类中创建一个方法来调用父类的事件,而子类通过调用父类的方法来触发事件. class parent { protected string name; publ ...
- eclipse系列: Cannot change version of project facet Dynamic web的解决方法
问题描述 用Eclipse创建Maven结构的web项目的时候选择了Artifact Id为maven-artchetype-webapp,由于这个catalog比较老,用的servl ...
- Linux第三次学习笔记
#信息的表示和处理 三种重要的数字表示 1. 无符号数编码: 基于传统的二进制表示法,表示大于或者等于零的数字. 2. 补码编码: 表示有符号数整数的最常见的方式,有符号数就是只可 以为正或者为负的数 ...
- 【MPI学习1】简单MPI程序示例
有了apue的基础,再看mpi程序多进程通信就稍微容易了一些,以下几个简单程序来自都志辉老师的那本MPI的书的第七章. 现在ubuntu上配置了一下mpich的环境: http://www.cnblo ...
- 一个简单js select插件
现在,通过一个select插件,来介绍一下js插件的构建过程. 1.先上效果图 2.目录构建 (1)这个select插件,我给它起名交hongselect,所以呢,首先建个hongselect的文件夹 ...
- MYSQL查询语句优化
mysql的性能优化包罗甚广: 索引优化,查询优化,查询缓存,服务器设置优化,操作系统和硬件优化,应用层面优化(web服务器,缓存)等等.这里的记录的优化技巧更适用于开发人员,都是从网络上收集和自己整 ...
- Android--按钮点击事件
Android中Button的点击事件非常简单,主要是一个内部类的问题 在界面上存在两个按钮和一个文本框,点击不同按钮的时候文本框中显示不同按钮的文字信息 <?xml version=" ...
- [USACO2005][POJ3045]Cow Acrobats(贪心)
题目:http://poj.org/problem?id=3045 题意:每个牛都有一个wi和si,试将他们排序,每头牛的风险值等于前面所有牛的wj(j<i)之和-si,求风险值最大的牛的最小风 ...
- Bootstrap3.0学习第十五轮(大屏幕介绍、页面标题、缩略图、警示框、Well)
详情请查看 http://aehyok.com/Blog/Detail/22.html 个人网站地址:aehyok.com QQ 技术群号:206058845,验证码为:aehyok 本文文章链接:h ...