tc 147 2 PeopleCircle(再见约瑟夫环)
SRM 147 2 600PeopleCircle
Problem Statement
There are numMales males and numFemales females arranged in a circle. Starting from a given point, you count clockwise and remove the K'th person from the circle (where K=1 is the person at the current point, K=2 is the next person in the clockwise direction, etc...). After removing that person, the next person in the clockwise direction becomes the new starting point. After repeating this procedure numFemales times, there are no females left in the circle.
Given numMales, numFemales and K, your task is to return what the initial arrangement of people in the circle must have been, starting from the starting point and in clockwise order.
For example, if there are 5 males and 3 females and you remove every second person, your return String will be "MFMFMFMM".
Definition
- ClassPeopleCircle
- Methodorder
- Parametersint , int , int
- Returnsstring
- Method signaturestring order(int numMales, int numFemales, int K)
Limits
- Time limit (s)2.000
- Memory limit (MB)64
Constraints
- numMales is between 0 and 25 inclusive
- numFemales is between 0 and 25 inclusive
- K is between 1 and 1000 inclusive
Test cases
- numMales5
- numFemales3
- K2
Returns"MFMFMFMM"
Return "MFMFMFMM". On the first round you remove the second person - "M_MFMFMM". Your new circle looks like "MFMFMMM" from your new starting point. Then you remove the second person again etc.- numMales7
- numFemales3
- K1
Returns"FFFMMMMMMM"
Starting from the starting point you remove the first person, then you continue and remove the next first person etc. Clearly, all the females are located at the beginning. Hence return "FFFMMMMMMM"- numMales25
- numFemales25
- K1000
Returns"MMMMMFFFFFFMFMFMMMFFMFFFFFFFFFMMMMMMMFFMFMMMFMFMMF"
- numMales5
- numFemales5
- K3
Returns"MFFMMFFMFM"
Here we mark the removed people with '_', and the starting position with lower-case:Number of | People Remaining
Rounds | (in initial order)
---------------+-----------------
0 | mFFMMFFMFM
1 | MF_mMFFMFM
2 | MF_MM_fMFM
3 | MF_MM_FM_m
4 | M__mM_FM_M
5 | M__MM__m_M- numMales1
- numFemales0
- K245
Returns"M"
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <typeinfo>
#include <fstream> using namespace std;
char s[] ;
vector<int> g ;
int n ;
int k ; void dead (int id , int ans , int m)
{
for (int i = ans ; i <= n ; i ++) {
k = m % i ;
id = (id + k) % i ;
}
// printf ("id = %d\n" , id ) ;
g.push_back (id) ;
} void solve (int fmale , int m)
{
dead ( , , m ) ;
for (int i = ; i <= n ; i ++) {
k = m % i ;
int id = ( + k) % i ;
if (id - < ) id = id - + i ;
else id -- ;
dead (id , i + , m ) ;
}
reverse (g.begin () , g.end ()) ;
// for (int i :g ) printf ("%d " , i) ; puts ("") ;
for (int i = ; i < n ; i ++) {
if (i < fmale) s[g[i]] = 'F' ;
else s[g[i]] = 'M' ;
}
s[n] = '\0' ;
} class PeopleCircle {
public:
string order(int male , int fmale , int m) {
// printf ("\nmale=%d\nfmale=%d\nm=%d\n" , male , fmale , m) ;
g.clear () ;
n = male + fmale ;
solve (fmale , m) ;
return s;
}
}; // CUT begin
ifstream data("PeopleCircle.sample"); string next_line() {
string s;
getline(data, s);
return s;
} template <typename T> void from_stream(T &t) {
stringstream ss(next_line());
ss >> t;
} void from_stream(string &s) {
s = next_line();
} template <typename T>
string to_string(T t) {
stringstream s;
s << t;
return s.str();
} string to_string(string t) {
return "\"" + t + "\"";
} bool do_test(int numMales, int numFemales, int K, string __expected) {
time_t startClock = clock();
PeopleCircle *instance = new PeopleCircle();
string __result = instance->order(numMales, numFemales, K);
double elapsed = (double)(clock() - startClock) / CLOCKS_PER_SEC;
delete instance; if (__result == __expected) {
cout << "PASSED!" << " (" << elapsed << " seconds)" << endl;
return true;
}
else {
cout << "FAILED!" << " (" << elapsed << " seconds)" << endl;
cout << " Expected: " << to_string(__expected) << endl;
cout << " Received: " << to_string(__result) << endl;
return false;
}
} int run_test(bool mainProcess, const set<int> &case_set, const string command) {
int cases = , passed = ;
while (true) {
if (next_line().find("--") != )
break;
int numMales;
from_stream(numMales);
int numFemales;
from_stream(numFemales);
int K;
from_stream(K);
next_line();
string __answer;
from_stream(__answer); cases++;
if (case_set.size() > && case_set.find(cases - ) == case_set.end())
continue; cout << " Testcase #" << cases - << " ... ";
if ( do_test(numMales, numFemales, K, __answer)) {
passed++;
}
}
if (mainProcess) {
cout << endl << "Passed : " << passed << "/" << cases << " cases" << endl;
int T = time(NULL) - ;
double PT = T / 60.0, TT = 75.0;
cout << "Time : " << T / << " minutes " << T % << " secs" << endl;
cout << "Score : " << * (0.3 + (0.7 * TT * TT) / (10.0 * PT * PT + TT * TT)) << " points" << endl;
}
return ;
} int main(int argc, char *argv[]) {
cout.setf(ios::fixed, ios::floatfield);
cout.precision();
set<int> cases;
bool mainProcess = true;
for (int i = ; i < argc; ++i) {
if ( string(argv[i]) == "-") {
mainProcess = false;
} else {
cases.insert(atoi(argv[i]));
}
}
if (mainProcess) {
cout << "PeopleCircle (600 Points)" << endl << endl;
}
return run_test(mainProcess, cases, argv[]);
}
// CUT end
题意:n个人围成一圈,从编号为0的人开始报数1,后面的依次报2,3……当报道m时,该人离开圈子,他后面的一个人重新从1开始报数,以此类推……游戏最终会只有一个人留下来。这道题中我们需要得到的是每次离开之人的编号。(总人数n已知,m已知)。-------- 约瑟夫环问题,几乎是每个acmer的入门套餐。
上学期的时候,貌似用链表模拟做过,后来又用数学方法求过,但并不是很理解,如今又碰到了,便打算好好写份约瑟夫环报告。
从第一轮开始:
0 1 2 3 4 5 …… (n - 2) (n - 1) 现在有n个人
明显第一次出队的人编号为m%n-1,我们令k = m % n ;
那么重新排列后为:
k k +1 k+2…… n-1 0 1 2 3 …… k-2
再把他们以0~n-1编号: 0 1 2 n-k-1 n-k n-k+1 n-k+2 n-k+3 n-2
通过上下比对,我们能隐约发现前后存在一种映射关系,大概是(假设重新编号后,有个下标为id) id + k ;
总之在算一下,其实是(id + k) % n ;
那么我们是不是能很容易的知道,现在的每个人在上一轮中存在的位置了吗?
求出每次游戏最后留下来的人是谁:
#include<bits/stdc++.h>
int beg , m , n ;//从第beg个人开始数1,数到m的人离开,总人数为n。把他们从0~n -1编号。
int k ; void solve ()
{
int cur = ;
for (int i = ; i <= n ; i ++) {
k = m % i ;
cur = (k + cur ) % i ;
}
cur = (cur + beg ) % n ;
printf ("%d\n" , cur ) ;
} int main ()
{
while (~ scanf ("%d%d%d" , &beg , &m , &n) ) {
solve () ;
}
return ;
}
通过这个映射关系,我们还能求出:
每一轮出圈的人的编号。
#include<bits/stdc++.h>
int m , n ;
int k ;
std::vector<int> g ; void dead (int id , int ans)
{
for (int i = ans ; i <= n ; i ++) {
k = m % i ;
id = (id + k) % i ;
}
g.push_back (id) ;
} void solve ()
{
dead ( , ) ;
for (int i = ; i <= n ; i ++) {
k = m % i ;
int id = ( + k) % i ;
id -- ;
if (id < ) id += i ;
dead (id , i + ) ;
}
std::reverse (g.begin () , g.end ()) ;
for (int i : g) printf ("%d," , i) ; puts ("") ;
} int main ()
{
while (~ scanf ("%d%d" , &m , &n)) {
g.clear () ;
solve () ;
}
return ;
}
看不懂的话,再结合这篇博文(orz):http://blog.csdn.net/u012333003/article/details/27076603
tc 147 2 PeopleCircle(再见约瑟夫环)的更多相关文章
- C#实现约瑟夫环问题
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace orde ...
- C语言数组实现约瑟夫环问题,以及对其进行时间复杂度分析
尝试表达 本人试着去表达约瑟夫环问题:一群人围成一个圈,作这样的一个游戏,选定一个人作起点以及数数的方向,这个人先数1,到下一个人数2,直到数到游戏规则约定那个数的人,比如是3,数到3的那个人就离开这 ...
- C语言链表实现约瑟夫环问题
需求表达:略 分析: 实现: #include<stdio.h> #include<stdlib.h> typedef struct node { int payload ; ...
- javascript中使用循环链表实现约瑟夫环问题
1.问题 传说在公元1 世纪的犹太战争中,犹太历史学家弗拉维奥·约瑟夫斯和他的40 个同胞被罗马士兵包围.犹太士兵决定宁可自杀也不做俘虏,于是商量出了一个自杀方案.他们围成一个圈,从一个人开始,数到第 ...
- HDU 3089 (快速约瑟夫环)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3089 题目大意:一共n人.从1号开始,每k个人T掉.问最后的人.n超大. 解题思路: 除去超大的n之 ...
- 约瑟夫环(Josehpuse)的模拟
约瑟夫环问题: 0,1,...,n-1这n个数字排成一个圆圈,从数字0开始每次从这个圆圈里删除第m个数字,求出这个圆圈里剩下的最后一个数字. 这里给出以下几种解法, 1.用队列模拟 每次将前m-1个元 ...
- C++ 约瑟夫环问题
约瑟夫环比较经典了 已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围.从编号为k的人开始报数,数到m的那个人出列:他的下一个人又从1开始报数,数到m的那个人又出列:依此规律重复下去,直 ...
- 约瑟夫环的java解决
总共3中解决方法,1.数学推导,2.使用ArrayList递归解决,3.使用首位相连的LinkedList解决 import java.util.ArrayList; /** * 约瑟夫环问题 * 需 ...
- 14.约瑟夫环问题[JosephusProblem]
[题目] n个数字(0,1,…,n-1)形成一个圆圈,从数字0开始,每次从这个圆圈中删除第m个数字(第一个为当前数字本身,第二个为当前数字的下一个数字).当一个数字删除后,从被删除数字的下一个继续删除 ...
随机推荐
- 一次奇怪的T-shirt展示
因为这次的第一也是上次的第一然后顺延下来又正好跟女神并列第二,拿到了一件T-shirt.总之,还是应该继续加油.
- 演示get、post请求如何算sn,算得sn如何使用
import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.UnsupportedEncoding ...
- 【Beta】团队协作模式探讨试行
概述 鉴于Alpha阶段松散的结构和低下的效率,以及Scrum会议时间过长.文档不到位.无标准化验收等问题,尝试对协作模式作一点变化. 依照课程压力等实际情况,以及按照贡献分分配原则,以一周为贡献分计 ...
- linux下memcache的运用,和php结合小案例。
由于是采用脚本安装的memache,所以软件的依赖关系我就不操心了,脚本已经帮我装好了和php的关联关系,实在是很省心.后续如果有需要,我会针对windows和linux各写一个安装和配置的说明,一来 ...
- HTML5学习总结-01 开发环境和历史
1 搭建HTML5开发环境 1 安装一款支持HTML5的浏览器 FireFox, Chrome 2 开发工具 SublineText, Eclipse, HBuilder, WebStorm 注:使用 ...
- Rsync
转自:http://www.mike.org.cn/blog/index.php?load=read&id=639###pp=0 [rsync实现网站的备份,文件的同步,不同系统的文件的同步, ...
- C#最简单最完整的webservice实例
http://fyinthesky.blog.163.com/blog/static/3555251720110115227337/ 我做java,但最近接触crm所以必须研究一下C#中的webser ...
- php爬虫 phpspider
<?php /** * Created by PhpStorm. * User: brady * Date: 2016/12/9 * Time: 17:32 */ ini_set("m ...
- 动态规划 求解 Minimum Edit Distance
http://blog.csdn.net/abcjennifer/article/details/7735272 自然语言处理(NLP)中,有一个基本问题就是求两个字符串的minimal Edit D ...
- /etc/profile、/etc/bashrc、~/.bash_profile、~/.bashrc 的区别(转)
/etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行并从/etc/profile.d目录的配置文件中搜集shell的设置. /etc/bashrc:为每一个运 ...