约瑟夫问题(多解)——POJ 3750

来源:互联网 发布:mysql数据库密码查看 编辑:程序博客网 时间:2024/05/11 17:33

对应POJ题目:点击打开链接


小孩报数问题
Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u

Submit Status

Description

有N个小孩围成一圈,给他们从1开始依次编号,现指定从第W个开始报数,报到第S个时,该小孩出列,然后从下一个小孩开始报数,仍是报到S个出列,如此重复下去,直到所有的小孩都出列(总人数不足S个时将循环报数),求小孩出列的顺序。

Input

第一行输入小孩的人数N(N<=64) 
接下来每行输入一个小孩的名字(人名不超过15个字符) 
最后一行输入W,S (W < N),用逗号","间隔

Output

按人名输出小孩按顺序出列的顺序,每行输出一个人名

Sample Input

5XiaomingXiaohuaXiaowangZhangsanLisi2,3

Sample Output

ZhangsanXiaohuaXiaomingXiaowangLisi

Source


思路:

1,循环链表,直接模拟。O(n*s)


2,线段树,O(nlg(n))

例子如下:n = 5, w = 1, s = 3  (括号内为原始编号)

1(1),2(2),3(3),4(4),5(5)     第3个数3出列

1(1),2(2),3(4),4(5)                第1个数1出列

1(2),2(4),3(5)                           第3个数5出列

1(2),2(4)                                      第1个数2出列

1(4)                                                 第1个数4出列


先引入Joseph递推公式,设有n个人(0,...,n-1),数到m退出,则第i轮出局的人为f(i)=(f(i-1)+m-1)%(n-i+1),f(0)=0; f(i) 表示当前子序列中要退出的那个人(当前序列编号为0~(n-i));

f(0) = 0;

第1轮:f(1) = (0 + 3 - 1) % 5 = 2 (下标为2即为第3个数)

第2轮:f(2) = (2 + 3 - 1) % 4 = 0 

第3轮:f(3) = (0 + 3 - 1) % 3 = 2 

第4轮:f(4) = (2 + 3 - 1) % 2 = 0 

第5轮:f(5) = (0 + 3 - 1) % 1 = 0

所以使用这个公式每一轮中每个人的编号的,接着我们需要快速地找到这一轮中要退出的人的编号对应的原始编号。一个人在当前剩余队列中编号为i,则说明他是从左到右数第i个人,这启发我们可以用线段树来解决问题。用线段树维护原编号i..j内还有多少人没 有被淘汰,这样每次选出被淘汰者后,在当前线段树中查找位置就可以了。

例如我们有5个原编号,当前淘汰者在剩余队列中编号为3,先看左子树,即原编号1..3区间内,如果剩下的人不足3个,则说明当前剩余编号为3的 这个人原编号只能是在4..5区间内,继续在4..5上搜索;如果1..3内剩下的人大于等于3个,则说明就在1..3内,也继续缩小范围查找,这样既可 在logn时间内完成对应。问题得到圆满的解决。

3,网上借鉴的令我无法理解的方法:O(n)

http://www.cnblogs.com/void/archive/2011/04/21/2024377.html




方法1:

#include<cstdio>#include<cstdlib>#include<cmath>#include<map>#include<queue>#include<stack>#include<vector>#include<algorithm>#include<cstring>#include<string>#include<iostream>#define ms(x,y) memset(x,y,sizeof(x))const int MAXN=1000+10;const int INF=1<<30;using namespace std;struct Node{char name[20];Node *prior;Node *next;};void CreateList(Node *&head, int n){Node *p,*s;for(int i=0; i<n; i++){s=(Node *)malloc(sizeof(Node));scanf("%s", s->name);if(head == NULL) head=s;else{p->next = s;s->prior = p;}p=s;}p->next = head;head->prior = p;}Node *Begin(Node *node, int s){Node *p=node;for(int i=0; i<s-1; i++)p=p->next;return p;}void Delete(Node *&node, int s, int n){Node *q,*p=node;for(int i=0; i<n; i++){for(int i=0; i<s-1; i++)p=p->next;q=p->next;printf("%s\n", p->name);p->prior->next = p->next;p->next->prior = p->prior;free(p);p=q;}}int main(){//freopen("in.txt","r",stdin);int n,w,s;scanf("%d", &n);Node *head=NULL;CreateList(head, n);scanf("%d,%d", &w,&s);Node *p=Begin(head, w);Delete(p, s, n);return 0;}


方法2

#include <stdio.h>#include <stdlib.h>#include <string.h>#define N 70#define L 20char name[N][L];int sum[N<<4];void build(int root, int l, int r){if(1 == r - l){sum[root] = 1;return;}int mid = ((l + r)>>1);build(root<<1, l, mid);build(root<<1|1, mid, r);sum[root] = sum[root<<1] + sum[root<<1|1];}int query(int root, int l, int r, int num){if(1 == r - l){sum[root] = 0;return l;}int mid = ((l + r)>>1);int ans;if(sum[root<<1] > num) ans = query(root<<1, l, mid, num);else ans = query(root<<1|1, mid, r, num - sum[root<<1]);sum[root] = sum[root<<1] + sum[root<<1|1];return ans;}int main(){//freopen("in.txt", "r", stdin);int n, w, s, i, out, num;while(~scanf("%d", &n)){for(i=0; i<n; i++)scanf("%s", name[i]);scanf("%d,%d", &w, &s);build(1, 0, n);out = w - 1;for(i=0; i<n; i++){out = (out + s - 1) % (n - i); //第i+1轮退出的相对编号num = query(1, 0, n, out); //根据相对编号找原始编号//printf("%s\n", name[(num + w - 1) % n]);printf("%s\n", name[num]);}}return 0;}


方法3

#include <stdio.h>#define N 70#define L 20char name[N][L];int main(){//freopen("in.txt", "r", stdin);    int n, i, m, p, w, s;while(~scanf("%d", &n)){for(i=0; i<n; i++)scanf("%s", name[i]);     scanf("%d,%d", &w, &s);i = 0;     while( ++i <= n )     {        p = i * s;         while (p > n)             p = p - n + (p - n - 1)/(s - 1);         printf("%s\n",name[(p - 2 + w) % n]);     }} return 0;}


0 0