天天看點

SBT解決約瑟夫環問題(poj3517)

And Then There Was One

Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 4852 Accepted: 2581

Description

Let’s play a stone removing game.

Initially, n stones are arranged on a circle and numbered 1, …, n clockwise (Figure 1). You are also given two numbers k and m. From this state, remove stones one by one following the rules explained below, until only one remains. In step 1, remove stone m. In step 2, locate the k-th next stone clockwise from m and remove it. In subsequent steps, start from the slot of the stone removed in the last step, make k hops clockwise on the remaining stones and remove the one you reach. In other words, skip (k − 1) remaining stones clockwise and remove the next one. Repeat this until only one stone is left and answer its number. For example, the answer for the case n = 8, k = 5, m = 3 is 1, as shown in Figure 1.

SBT解決約瑟夫環問題(poj3517)
Initial state
SBT解決約瑟夫環問題(poj3517)
Step 1
SBT解決約瑟夫環問題(poj3517)
Step 2
SBT解決約瑟夫環問題(poj3517)
Step 3
SBT解決約瑟夫環問題(poj3517)
Step 4
SBT解決約瑟夫環問題(poj3517)
Step 5
SBT解決約瑟夫環問題(poj3517)
Step 6
SBT解決約瑟夫環問題(poj3517)
Step 7
SBT解決約瑟夫環問題(poj3517)
Final state

Figure 1: An example game

Initial state: Eight stones are arranged on a circle.

Step 1: Stone 3 is removed since m = 3.

Step 2: You start from the slot that was occupied by stone 3. You skip four stones 4, 5, 6 and 7 (since k = 5), and remove the next one, which is 8.

Step 3: You skip stones 1, 2, 4 and 5, and thus remove 6. Note that you only count stones that are still on the circle and ignore those already removed. Stone 3 is ignored in this case.

Steps 4–7: You continue until only one stone is left. Notice that in later steps when only a few stones remain, the same stone may be skipped multiple times. For example, stones 1 and 4 are skipped twice in step 7.

Final State: Finally, only one stone, 1, is on the circle. This is the final state, so the answer is 1.

Input

The input consists of multiple datasets each of which is formatted as follows.

n k m

The last dataset is followed by a line containing three zeros. Numbers in a line are separated by a single space. A dataset satisfies the following conditions.

2 ≤ n ≤ 10000, 1 ≤ k ≤ 10000, 1 ≤ m ≤ n

The number of datasets is less than 100.

Output

For each dataset, output a line containing the stone number left in the final state. No extra characters such as spaces should appear in the output.

Sample Input

8 5 3
100 9999 98
10000 10000 10000
0 0 0      

Sample Output

1
93
2019      

Source

Japan 2007

跟上一個題一樣的,隻不過是是輸出最後一個

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=100010;
struct SBT
{
    int left,right,size,key;
    void init(int val)
    {
        left=right=0;
        size=1;
        key=val;
    }
}tree[maxn];
int tot,root;
void left_rotate(int &x)
{
    int y=tree[x].right;
    tree[x].right=tree[y].left;
    tree[y].left=x;
    tree[y].size=tree[x].size;
    tree[x].size=tree[tree[x].left].size+tree[tree[x].right].size+1;
    x=y;
}
void right_rotate(int &x)
{
    int y=tree[x].left;
    tree[x].left=tree[y].right;
    tree[y].right=x;
    tree[y].size=tree[x].size;
    tree[x].size=tree[tree[x].left].size+tree[tree[x].right].size+1;
    x=y;
}
void maintain(int &x,int flag)
{
    if(!flag)
    {
        if(tree[tree[tree[x].left].left].size>tree[tree[x].right].size)
            right_rotate(x);
        else if(tree[tree[tree[x].left].right].size>tree[tree[x].right].size)
            left_rotate(tree[x].left),right_rotate(x);
        else return;
    }
    else
    {
        if(tree[tree[tree[x].right].right].size>tree[tree[x].left].size)
            left_rotate(x);
        else if(tree[tree[tree[x].right].left].size>tree[tree[x].left].size)
            right_rotate(tree[x].right),left_rotate(x);
        else return;
    }
    maintain(tree[x].left,0);
    maintain(tree[x].right,1);
    maintain(x,0);
    maintain(x,1);
}
//插入值為key的節點
void insert(int &x,int key)
{
    if(!x)
    {
        x=++tot;
        tree[x].init(key);
    }
    else
    {
        tree[x].size++;
        if(key<tree[x].key)insert(tree[x].left,key);
        else insert(tree[x].right,key);
        maintain(x,key>=tree[x].key);
    }
}
//删除值為key的節點

int del(int &x,int key)
{
    if(!x)return 0;
    tree[x].size--;
    if(key==tree[x].key||(key<tree[x].key&&tree[x].left==0)||
       (key>tree[x].key&&tree[x].right==0))
    {
        if(tree[x].left&&tree[x].right)
        {
            int p=del(tree[x].left,key+1);
            tree[x].key=tree[p].key;
            return p;
        }
        else
        {
            int p=x;
            x=tree[x].left+tree[x].right;
            return p;
        }
    }
    else
        return del(key<tree[x].key?tree[x].left:tree[x].right,key);
}
int get_kth(int x,int k)
{
    if(k<=tree[tree[x].left].size)
        return get_kth(tree[x].left,k);
    else if(k>tree[tree[x].left].size+1)
        return get_kth(tree[x].right,k-tree[tree[x].left].size-1);
    return tree[x].key;
}
int N,K,M;
int main()
{  
    while(scanf("%d%d%d",&N,&K,&M)!=EOF,N||M||K)
    {
        root=tot=0;
        for(int i=1;i<=N;i++)insert(root,i);
        bool first=true;
        int last=M+1-K;
        for(int i=1;i<N;i++)
        {
            int k=(K+last-1)%tree[root].size;
            if(!k)k=tree[root].size;
            int tmp=get_kth(root,k);
            del(root,tmp);
            last=k;
        }
        printf("%d\n",tree[root].key);
    }
    return 0;
}