HDU-4841-圆桌问题

本文介绍了一种解决ACM竞赛中约瑟夫环问题的方法。通过使用向量进行优化,实现对约瑟夫环的暴力枚举,并提供了一个具体的C++实现代码示例。

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

ACM模版

描述

描述

题解

这个题简单的来就是暴力枚举约瑟夫环,当然,太暴力也不好,适当的用数据结构优化一下也是有必要的,这里用向量维护,成功水过。

代码

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

const int MAXN = 4e4;
const int MOD = 50;

int n, m;
int flag[MAXN];
vector<int> vi;

int main()
{
    int tot, now;
    while (~scanf("%d%d", &n, &m))
    {
        vi.clear();
        tot = n << 1;
        for (int i = 1; i <= tot; i++)
        {
            vi.push_back(i);
            flag[i] = 0;
        }

        now = 1;
        while (tot > n)
        {
            now += (m - 1);
            if (now <= tot)
            {
                flag[vi[now - 1]] = 1;
                vi.erase(vi.begin() + now - 1);
                now = now == tot ? 1 : now;
            }
            else
            {
                now %= tot;
                now = now == 0 ? tot : now; //  nwo 太大时,%tot 存在为 0 的情况
                flag[vi[now - 1]] = 1;
                vi.erase(vi.begin() + now - 1);
                now = now == tot ? 1 : now;
            }
            tot--;
        }

        int tmp = n << 1;
        for (int i = 1; i <= tmp; i++)
        {
            if (flag[i])
            {
                printf("B");
            }
            else
            {
                printf("G");
            }
            if (i % MOD == 0)
            {
                putchar(10);
            }
        }


        if ((n << 1) % MOD != 0)
        {
            putchar(10);
        }

        putchar(10);
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值