题目链接: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 约瑟夫问题的更多相关文章

  1. Codevs 1282 约瑟夫问题

    1282 约瑟夫问题  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master 题解       题目描述 Description 有编号从1到N的N个小朋友在玩一种 ...

  2. codevs 1282 约瑟夫问题(线段树)

    #include<iostream> #include<cstdio> #include<cstring> #define maxn 30010 using nam ...

  3. AC日记——约瑟夫问题 codevs 1282

    1282 约瑟夫问题  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master 题解  查看运行结果     题目描述 Description 有编号从1到N的N个小 ...

  4. P1996||T1282 约瑟夫问题 洛谷||codevs

    https://www.luogu.org/problem/show?pid=1996||http://codevs.cn/problem/1282/ 题目背景 约瑟夫是一个无聊的人!!! 题目描述 ...

  5. 约瑟夫问题(java实现)

    方法一.自定义的链表实现 package com.code.yuesefu; public class YueSeFuList { public static void main(String[] a ...

  6. Java 解决约瑟夫问题

    约瑟夫问题(有时也称为约瑟夫斯置换,是一个出现在计算机科学和数学中的问题.在计算机编程的算法中,类似问题又称为约瑟夫环.又称“丢手绢问题”.) 有这样一个故事,15个教徒和15个非教徒在深海遇险必须讲 ...

  7. C#实现约瑟夫环问题

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace orde ...

  8. C语言数组实现约瑟夫环问题,以及对其进行时间复杂度分析

    尝试表达 本人试着去表达约瑟夫环问题:一群人围成一个圈,作这样的一个游戏,选定一个人作起点以及数数的方向,这个人先数1,到下一个人数2,直到数到游戏规则约定那个数的人,比如是3,数到3的那个人就离开这 ...

  9. C语言链表实现约瑟夫环问题

    需求表达:略 分析: 实现: #include<stdio.h> #include<stdlib.h> typedef struct node { int payload ; ...

随机推荐

  1. python的类变量与实例变量

    python的类内部定义的变量 ,形式上没有区分实例变量和类变量(java的静态变量),测试结果如下: 

  2. CSS3.0盒模型display:-webkit-box;的使用

    box-flex是css3新添加的盒子模型属性,它的出现可以解决我们通过N多结构.css实现的布局方式.经典   的一个布局应用就是布局的垂直等高.水平均分.按比例划分. 目前box-flex属性还没 ...

  3. ubuntu14.04安装OpenVirteX

    官网链接: http://ovx.onlab.us/getting-started/installation/ step1: System requirements: Recommended 4 Co ...

  4. java 实现二分查找法

    /** * 二分查找又称折半查找,它是一种效率较高的查找方法. [二分查找要求]:1.必须采用顺序存储结构 2.必须按关键字大小有序排列. * @author Administrator * */ p ...

  5. BASH相关

    颜色 http://www.cnblogs.com/lr-ting/archive/2013/02/28/2936792.html http://segmentfault.com/q/10100000 ...

  6. js 猜数字游戏

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. HTML页面表单输入框去掉鼠标选中后边框变色的效果

    标题说的有些含糊,实在不知道怎么描述了,就简单说一下吧,一个最简单的表单,代码如下,没有任何样式和名字, <!DOCTYPE html> <html> <head> ...

  8. Light OJ 1253 Misere Nim (尼姆博弈(2))

    LightOJ1253 :Misere Nim 时间限制:1000MS    内存限制:32768KByte   64位IO格式:%lld & %llu 描述 Alice and Bob ar ...

  9. linux常见问题集锦

    本文转自 http://bbs.chinaunix.net/thread-3668921-1-1.html,在此感谢作者分享 一.填空题: 1. 在Linux系统中,以 文件 方式访问设备 . 2. ...

  10. ShareSDK集成微信、QQ、微博分享

    1.前言 为什么要使用第三方的作为集成分享的工具呢?而不去用官方的呢?有什么区别么? 一个字"快",如果你使用官方的得一个个集成他们的SDK,相信这是一个痛苦的过程. 2.准备需要 ...