Wikioi 1282 约瑟夫问题

题意:
(中文)
思路:
由于数据量太大, 所以直接用线段树来做。
分成两种情况:
1.如果,现在位置后面有p个人可供数且p>= m,那么从此位置数出m个人就可以了。
2.后面有p个人可供数且p < m,那么光数后面的人已经不够了,所以需要从头数 m - p 个人,而m - p如果大于现在存在的人数sum,就对sum 进行求模.
细节方面:
第1种情况中,先进行查询,求出从此位置t到n,一共有p个人供数。
查询直接写成 从1 - n 第 sum - p + t 就可以了。
第二种情况中,如果取模的时候等于0,就相当于取到最后一个人了,所以需要修改成数最后一个人。

Code:

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<stack>
#include<bitset>
#include<set>
#include<map>
#include<cctype>
#include<vector>

//#define TEST

#define LL long long
#define Mt(f, x) memset(f, x, sizeof(f));
#define rep(i, s, e) for(int i = (s); i <= (e); ++i)
#ifdef TEST
    #define See(a) cout << #a << " = " << a << endl;
    #define See2(a, b) cout << #a << " = " << a << ' ' << #b << " = " << b << endl;
    #define debug(a, s, e){ rep(_i, s, e) {cout << a[_i] << ' '; }cout << endl;}
    #define debug2(a, s, e, ss, ee) rep(i_, s, e) {debug(a[i_], ss, ee);}
#else
    #define See(a)
    #define See2(a, b)
    #define debug(a, s, e)
    #define debug2(a, s, e, ss, ee)
#endif

const int MAX = 2e9;
const int MIN = -2e9;
const int PI = acos(-1.0);
const double eps = 1e-8;

using namespace std;

#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1

const int N = 30000 + 5;

int sum[N << 2];
int t;

void pushUp(int rt)
{
    sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
}

void build(int l, int r, int rt)
{
    if(l == r)
    {
        sum[rt] = 1;
        return ;
    }
    int m = (l + r) >> 1;
    build(lson);
    build(rson);
    pushUp(rt);
}

int query(int L, int R, int l, int r, int rt)
{
    if(l >= L && r <= R)
    {
        return sum[rt];
    }
    int m = (l + r) >> 1;
    int ret = 0;
    if(L <= m) ret += query(L, R, lson);
    if(R > m) ret += query(L, R, rson);
    return ret;
}

void query(int I, int L, int R, int l, int r, int rt)
{
    if(l == r)
    {
        sum[rt] = 0;
        t = l;
        return ;
    }
    int m = (l + r) >> 1;
    if(I <= sum[rt << 1])
    {
        query(I, L, R, lson);
    }
    else
    {
        query(I - sum[rt << 1], L, R, rson);
    }
    pushUp(rt);
}

int main()
{
    int n, m;
    while(~scanf("%d%d", &n, &m))
    {
        build(1, n, 1);
        t = 1;
        bool first = true;
        for(int i = 1; i <= n; ++i)
        {
            int p = query(t, n, 1, n, 1);
            if(m <= p)
            {
                query(sum[1] - p + m, 1, n, 1, n, 1);
            }
            else
            {
                int t = (m - p) % sum[1];
                t = t == 0 ? sum[1] : t;
                query(t, 1, n, 1, n, 1);
            }
            printf(first ? "%d" : " %d", t);
            first = false;
        }
        printf("\n");
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值