poj 3517 And Then There Was One

本文探讨了解决约瑟夫问题的高效算法,通过数学策略简化了问题并实施递归方法,避免了模拟整个游戏过程带来的高时间复杂度。重点介绍了变换编号、递推公式及线段树模拟等关键步骤,有效解决了n个人情况下的优胜者编号问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

约瑟夫循环问题:

Josephus假设n个竞赛者排成一个环形,依次顺序编号12,…,n。从某个指定的第1号开始,沿环计数,每数到第m个人就让其出列,且从下一个人开始重新计数,继续进行下去。这个过程一直进行到所有的人都出列为止。最后出列者为优胜者。

无论是用链表实现还是用数组实现来解约瑟夫问题都有一个共同点:要模拟整个游戏过程,不仅程序写起来比较麻烦,而且时间复杂度高达O(nm),当nm非常大(例如上百万,上千万)的时候,几乎是没有办法在短时间内出结果的。注意到原问题仅仅是要求出最后的胜利者的序号,而不是要模拟整个过程。因此如果要追求效率,就要打破常规,实施一点数学策略。

为了讨论方便,先把问题稍微改变一下,并不影响原意:

问题描述:n个人(编号0~(n-1)),从0开始报数,报到(m-1)的退出,剩下的人继续从0开始报数。求胜利者的编号。

我们知道第一个人(编号一定是(m%n)-1) 出列之后,剩下的n-1个人组成了一个新的约瑟夫环(以编号为k=m%n的人开始):   k   k+1   k+2   ... n-2, n-1, 0, 1, 2, ... k-2并且从k开始报0

现在我们把他们的编号做一下转换:

k      --> 0

k+1    --> 1

k+2    --> 2

...

...

k-2    --> n-2

 

变换后就完完全全成为了(n-1)个人报数的子问题,假如我们知道这个子问题的解:例如x是最终的胜利者,那么根
据上面这个表把这个x变回去不刚好就是n个人情况的解吗?!!变回去的公式很简单,相信大家都可以推出来:x1=(x+k)%n

如何知道(n-1)个人报数的问题的解?对,只要知道(n-2)个人的解就行了。(n-2)个人的解呢?当然是先求(n-3)的
情况 ---- 这显然就是一个倒推问题!

显然得到递推公式

F[0]=0;

F[n]=(F[n-1]+k)%n

 方法二:用线段树模拟:

View Code
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<cstring>
#include<vector>
#include<string>
#define LL long long
using namespace std;
class Node
{
public:
      int l,r,sum;    
}node[40024];
void build( int l , int r , int cnt )
{
    node[cnt].l = l;
    node[cnt].r = r;
    node[cnt].sum = r - l + 1;
    if( l == r ) return;
    int mid = ( l + r )>>1;
    build( l , mid , cnt*2 );
    build( mid + 1 , r ,cnt*2+1 );
}
int delete_node( int s , int c , int place ,int k ,int n , int cnt )
{
    node[cnt].sum --;
    if( node[cnt].l == node[cnt].r )
    {
           if( c == n )
           {
            return node[cnt].l;        
        }
        place -- ;
        place = ( place + k )%( n - c );
        if( place == 0 ) place = n - c;
        return place;
    }
    if( node[2*cnt].sum >= s )
        return delete_node( s , c , place ,k , n , cnt*2 );
    else
    {
        s -= node[2*cnt].sum;
        return delete_node( s , c, place, k ,n ,cnt*2+1 );    
    }    
}
int main( )
{
   int n , m , k;
   while( scanf( "%d %d %d",&n,&k,&m ),n|m|k )
   {
       build( 1 , n ,1 );
       for( int i = 1 ;i <= n ; i ++ )
       {
           m = delete_node( m , i , m , k , n , 1 );        
       }
       printf( "%d\n",m );        
   }
   return 0;    
}

 

View Code
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<cstring>
#include<vector>
#include<string>
#define LL long long
using namespace std;

int main(  )
{
    int f[10024];
    int n,m,k;
    while( scanf( "%d %d %d",&n ,&k,&m) , n|k|m )
    {
        f[0] = 1;
        for( int i = 2 ; i < n ; i ++ )
        {
            f[i] = ( f[i-1] + k )%i;    
        }
        printf( "%d\n",(f[n-1] + m)%n + 1 );    
    }
    //system( "pause" );
    return 0;
}

 

转载于:https://www.cnblogs.com/bo-tao/archive/2012/07/21/2602428.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值