【wikioi】1282 约瑟夫问题
题目链接:http://www.wikioi.com/problem/1282/
算法:线段树(名次树)
说明在代码里有了,直接上代码。
#include <cstdio>
using namespace std; #define lson l, m, rt << 1
#define rson m+1, r, rt << 1 | 1
#define MID (l+r) >> 1
#define lc rt << 1
#define rc rt << 1 | 1 const int maxn = 30000 + 10;
int n, m, sum[maxn << 2]; void pushup(int rt) {
sum[rt] = sum[lc] + sum[rc];
} void build(int l, int r, int rt) {
if(l == r) {
sum[rt] = 1;
return;
}
int m = MID;
build(lson); build(rson);
pushup(rt);
} //名次树是以名次为区间,通过对区间内的人数加减,再用相对位置与人数相比可得到他的编号所属区间,坐后递归直到得到了编号。
//维护的sum就是区间和啦~
void update(int p, int l, int r, int rt) {
sum[rt]--;
if(l == r) {
printf("%d ", l);
return;
}
int m = MID;
if(p <= sum[lc]) update(p, lson); //相对位置小于左边人数说明这个人的编号在左边
else update(p-sum[lc], rson); //反之在右边,并且人数要减去左边人数
pushup(rt);
} int main(){
scanf("%d%d", &n, &m);
build(1, n, 1);
int seq = 1; //从1开始报数
for(int i = 1; i <= n; ++i) {
seq = (seq + m - 1) % sum[1]; //至于为什么要-1,自己琢磨。。
if(!seq) seq = sum[1];
update(seq, 1, n, 1);
}
return 0;
}
【wikioi】1282 约瑟夫问题的更多相关文章
- Codevs 1282 约瑟夫问题
1282 约瑟夫问题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题解 题目描述 Description 有编号从1到N的N个小朋友在玩一种 ...
- codevs 1282 约瑟夫问题(线段树)
#include<iostream> #include<cstdio> #include<cstring> #define maxn 30010 using nam ...
- AC日记——约瑟夫问题 codevs 1282
1282 约瑟夫问题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题解 查看运行结果 题目描述 Description 有编号从1到N的N个小 ...
- P1996||T1282 约瑟夫问题 洛谷||codevs
https://www.luogu.org/problem/show?pid=1996||http://codevs.cn/problem/1282/ 题目背景 约瑟夫是一个无聊的人!!! 题目描述 ...
- 约瑟夫问题(java实现)
方法一.自定义的链表实现 package com.code.yuesefu; public class YueSeFuList { public static void main(String[] a ...
- Java 解决约瑟夫问题
约瑟夫问题(有时也称为约瑟夫斯置换,是一个出现在计算机科学和数学中的问题.在计算机编程的算法中,类似问题又称为约瑟夫环.又称“丢手绢问题”.) 有这样一个故事,15个教徒和15个非教徒在深海遇险必须讲 ...
- C#实现约瑟夫环问题
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace orde ...
- C语言数组实现约瑟夫环问题,以及对其进行时间复杂度分析
尝试表达 本人试着去表达约瑟夫环问题:一群人围成一个圈,作这样的一个游戏,选定一个人作起点以及数数的方向,这个人先数1,到下一个人数2,直到数到游戏规则约定那个数的人,比如是3,数到3的那个人就离开这 ...
- C语言链表实现约瑟夫环问题
需求表达:略 分析: 实现: #include<stdio.h> #include<stdlib.h> typedef struct node { int payload ; ...
随机推荐
- 数据结构与算法实验题7.1 M 商人的求救
问题描述:A 国正面临着一场残酷的战争,城市被支持不同领导的两股势力占据,作为一个商人,M先生并不太关心政治,但是他知道局势很严重,他希望你能救他出去.M 先生说:“为了安全起见,我们的路线最多只能包 ...
- centos rm -rf 恢复删除的文件
Linux有时候执行了 rm -rf 等操作误删了文件绝对是一件可怕的事情,好在有一些解决的办法可以临时救急.这时我们就要用到一款叫做extundelete的工具了. 目录[-] 依赖 安装 查找要恢 ...
- PHP读取csv文件
<?php //取数据 $f_d = file_get_contents('tpl_import_info.csv'); $f_d = iconv('gbk', 'utf-8', $f_d); ...
- Windows命令行重命名文件
RENAME D:\Cache\xyz.html xyz%date:~0,4%%date:~5,2%%date:~8,2%.tar.gz
- Selenium webdriver 学习总结-元素定位
Selenium webdriver 学习总结-元素定位 webdriver提供了丰富的API,有多种定位策略:id,name,css选择器,xpath等,其中css选择器定位元素效率相比xpath要 ...
- MPlayer-ww 增加边看边剪切功能+生成高质量GIF功能
http://pan.baidu.com/s/1eQm5a74 下载FFmpeg palettegen paletteuse documentation 需要下载 FFmpeg2.6 以上 并FFmp ...
- Java for LeetCode 075 Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- Java for LeetCode 048 Rotate Image
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- poj 1363 Rails 解题报告
题目链接:http://poj.org/problem?id=1363 题意:有一列火车,车厢编号为1-n,从A方向进站,向B方向出站.现在进站顺序确定,给出一个出站的顺序,判断出站顺序是否合理. 实 ...
- MFC RadioButton
添加一组RadioButton 多个radio button,IDC_RADIO1,IDC_RADIO2,IDC_RADIO3 ..将IDC_RADIO1的Group属性选择上,其他不要选Group属 ...