Josephus Problem II CSES - 2163

news/2025/2/3 17:37:15/

有3种方法

Solution 1 - ordered_set

Utilizing the ordered_set

This data structure is an extension of the general set in C++.

It allows searching for the K-th smallest element in O(log n) time complexity.

#include <iostream> 
using namespace std; 
#include <ext/pb_ds/assoc_container.hpp> 
using namespace __gnu_pbds; 
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> 
int main() 
{ int n, k;cin >> n >> k;ordered_set os;for (int i = 1; i <= n; i++){os.insert(i);}int cur = 0;for (int i = 1; i <= n; i++){cur = (cur + k) % os.size();auto it = os.find_by_order(cur);cout << *it << " ";os.erase(it);}return 0; 
} 

Solution 2 Blocking Simulation

Assume N is the size of the list, we can divide the numbers into K × K K\times K K×K matrix, where K = N K=\sqrt{N } K=N

这样模拟的时候,做N次循环,循环内枚举为 s q r t N sqrt{N} sqrtN的常数倍,其中模拟复杂度为 O ( N N ) O(N\sqrt{N}) O(NN )

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pii;
void solve()
{int n, k;cin >> n >> k;int m = sqrt(n);vector<vector<int>> g;for (int i = 1; i <= n;){vector<int> tmp;while (tmp.size() < m && i <= n){tmp.push_back(i++);}g.push_back(tmp);}int x = 0, y = 0;for (int i = 0; i < n; i++){int j = k % (n - i);while (j){if (y + j < g[x].size()){y += j;j = 0;}else{j -= g[x].size() - y;y = 0;x = (x + 1) % g.size();}}// 矫正错误位置while (y >= g[x].size()){y = 0;x = (x + 1) % g.size();}cout << g[x][y] << " ";// 最后一个不用删除 避免死循环if (i < n - 1){g[x].erase(g[x].begin() + y);while (y >= g[x].size()){y = 0;x = (x + 1) % g.size();}}}
}
int main()
{ios::sync_with_stdio(false);cin.tie(0);int t;t = 1;while (t--){solve();}
}

Solution 3 Segment tree

We can generalize this problem into easy range query problem …

Subsequent content will be here soon…,


http://www.ppmy.cn/news/1569018.html

相关文章

【apt源】RK3588 平台ubuntu20.04更换apt源

RK3588芯片使用的是aarch64架构&#xff0c;因此在Ubuntu 20.04上更换apt源时需要使用针对aarch64架构的源地址。以下是针对RK3588芯片在Ubuntu 20.04上更换apt源到清华源的正确步骤&#xff1a; 步骤一&#xff1a;打开终端 在Ubuntu 20.04中&#xff0c;按下Ctrl Alt T打…

智能小区物业管理系统推动数字化转型与提升用户居住体验

内容概要 在当今快速发展的社会中&#xff0c;智能小区物业管理系统的出现正在改变传统的物业管理方式。这种系统不仅仅是一种工具&#xff0c;更是一种推动数字化转型的重要力量。它通过高效的技术手段&#xff0c;将物业管理与用户居住体验紧密结合&#xff0c;无疑为社区带…

ChatGPT 搜索测试整合记忆功能

据 TestingCatalog 报道&#xff0c;OpenAI 正在测试 ChatGPT 搜索的整合记忆功能&#xff0c;被命名为 “Memory in search”2。以下是关于该功能的具体情况123&#xff1a; 功能特点 个性化搜索&#xff1a;启用该功能后&#xff0c;ChatGPT 能利用存储的记忆数据&#xff0…

【4】阿里面试题整理

[1]. 介绍一下数据库死锁 数据库死锁是指两个或多个事务&#xff0c;由于互相请求对方持有的资源而造成的互相等待的状态&#xff0c;导致它们都无法继续执行。 死锁会导致事务阻塞&#xff0c;系统性能下降甚至应用崩溃。 比如&#xff1a;事务T1持有资源R1并等待R2&#x…

【AI】探索自然语言处理(NLP):从基础到前沿技术及代码实践

Hi &#xff01; 云边有个稻草人-CSDN博客 必须有为成功付出代价的决心&#xff0c;然后想办法付出这个代价。 目录 引言 1. 什么是自然语言处理&#xff08;NLP&#xff09;&#xff1f; 2. NLP的基础技术 2.1 词袋模型&#xff08;Bag-of-Words&#xff0c;BoW&#xff…

LLM:BERT or BART 之BERT

文章目录 前言一、BERT1. Decoder-only2. Encoder-only3. Use of Bidirectional Context4. Masked Language Model (MLM)5. Next Sentence Prediction (NSP)6. Fine-tune1、情感分析2、句对分析3、命名实体识别&#xff08;NER&#xff09; 7. BERT总结 总结 前言 NLP选手对这…

Linux学习之DNS基础服务器搭建

一、DNS服务器概述 1.dns服务主要的功能是将域名转换为相应的ip地址&#xff0c;提供dns服务的系统就是dns服务器 2.dns服务器分为3种&#xff1a; 主域名服务器&#xff1a;本身提供dns服务&#xff0c;不含区域数据文件 辅助域名服务器&#xff1a;和主域名服务器一起提供dns…

deepseek+vscode自动化测试脚本生成

近几日Deepseek大火,我这里也尝试了一下,确实很强。而目前vscode的AI toolkit插件也已经集成了deepseek R1,这里就介绍下在vscode中利用deepseek帮助我们完成自动化测试脚本的实践分享 安装AI ToolKit并启用Deepseek 微软官方提供了一个针对AI辅助的插件,也就是 AI Toolk…