sgu254:Strange Random(模拟+优化)

题目大意:
       有一个由 1n 顺时针构成的环,从点 1 开始走,算自己每次走q ( 如果当前点为奇数顺时针,否则逆时针),删除走到的那个点,从那个点顺时针方向的下一个点开始继续走,输出最后剩下的那个点。

分析:
       中心思想模拟,考虑到我们删掉一些点后会走大量无效状态,我们可以设定每删除若干个点,重新构环,这样可以大幅度加速模拟。同时尽量少用取模,

AC code:

#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <string>
#include <sstream>
#include <iostream>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#define pb push_back
#define mp make_pair
#define debug(...) fprintf(stderr, __VA_ARGS__)
typedef long long LL;
typedef double DB;
typedef long double LD;
using namespace std;

const int MAXN = 2000009;

int n, q;
int num[MAXN], tot;

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    scanf("%d%d", &n, &q);
    for(int i = 1; i <= n; ++i)
        num[i] = i;
    int tmp, tmptot, d = 1;
    int block = n/10, now = 1;
    if(!block) block = n;
    tot = n;
    for(int i = 1; i < n; ++i)
    {
        if(i%block == 0)
        {
            tmptot = 0, tmp = num[now];
            for(int j = 1; j <= tot; ++j)
                if(num[j])
                {
                    num[++tmptot] = num[j];
                    if(num[j] == tmp) now = tmptot;
                }
            tot = tmptot;
        }
        for(int j = 1; j < q; ++j)
        {
            do
            {
                now += d;
                if(now > tot) now -= tot;
                else if(now < 1) now += tot;
            }while(!num[now]);
        }
        num[now] = 0;
        do
        {
            now++;
            if(now > tot) now -= tot;
        }while(!num[now]);
        if(num[now]&1) d = 1;
        else d = -1;
    }
    printf("%d\n", num[now]);

    #ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值