HDU 5860 Death Sequence(递推)

题目大意

  排成一行的约瑟夫问题。

  n个人排成一行,从第一个人开始,每个k个人报数,报到数的人被杀死,剩下的人重新排成一行再报数。

  一共q个询问,每次询问第qi个死的人是谁。

  n <= 3000000 , q <= 1000000 , k>=1 。

解题分析

  显然每一轮游戏可以看做是一个子问题。

  假设编号为0~n-1,若某轮中某人的编号为i,如果i被k整除则被杀,否则在下轮中编号为i-i/k-1。

  开个结构体记录每个人是在第几轮第几个出局,排个序,O(1)输出就好

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <list>
#include <queue>
#include <map>
using namespace std;
#define L(i) i<<1
#define R(i) i<<1|1
#define INF  0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-4
#define maxn 100010
#define MOD 1000000007

int n,k,Q;
struct node
{
    int x,y;
    int pos;
    bool operator<(const node &a)const
    {
        return x < a.x || (x == a.x && y < a.y);
    }
}cnt[3000030];

int main()
{
    int t,C = 1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&k,&Q);
        int l = 1;
        for(int i = 0; i < n; i++)
        {
            if(i%k == 0)
            {
                cnt[i].x = 1;
                cnt[i].y = l++;
                cnt[i].pos = i + 1;
            }
            else
            {
                cnt[i].x = cnt[i-i/k-1].x + 1;
                cnt[i].y = cnt[i-i/k-1].y;
                cnt[i].pos = i + 1;
            }
        }
        sort(cnt,cnt+n);
        while(Q--)
        {
            int x;
            scanf("%d",&x);
            printf("%d\n",cnt[x-1].pos);
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值