传送门:Josephus Problem

题意:经典约瑟夫问题,有n个人,每次数到第k个人出列,求剩下的最后一人。

分析:用线段树模拟约瑟夫问题,记录区间的减少情况,然后根据每次数到的人在区间排第几位,线段树log(n)找到并更新,总复杂度为O(nlog(n))。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define N 100010
#define mod 1000000007
#define LL long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
int n,k;
int sum[N<<],vis[N];
void Pushup(int rt)
{
int ls=rt<<,rs=ls|;
sum[rt]=sum[ls]+sum[rs];
}
void build(int l,int r,int rt)
{
if(l==r)
{
sum[rt]=;return;
}
int m=(l+r)>>;
build(lson);
build(rson);
Pushup(rt);
}
void update(int num,int l,int r,int rt)
{
if(l==r)
{
vis[l]=;
sum[rt]=;
return;
}
int m=(l+r)>>;
if(num<=sum[rt<<])update(num,lson);
else update(num-sum[rt<<],rson);
Pushup(rt);
}
int main()
{
int T,cas=;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&k);
memset(vis,,sizeof(vis));
build(,n,);
int num=;
for(int i=n;i>;i--)
{
num=(num+k-)%i;
if(num==)num=i;
update(num,,n,);
}
for(int i=;i<=n;i++)
if(!vis[i])
{
printf("Case %d: %d\n",cas++,i);break;
}
}
}

lightoj 1179(线段树)的更多相关文章

  1. LightOJ 1135(线段树)

    题解引自:http://www.cnblogs.com/wuyiqi/archive/2012/05/27/2520642.html 题意: 有n个数,刚开始都为0 add i , j 给i,j区间内 ...

  2. LightOJ 1089 - Points in Segments (II) 线段树区间修改+离散化

    http://www.lightoj.com/volume_showproblem.php?problem=1089 题意:给出许多区间,查询某个点所在的区间个数 思路:线段树,由于给出的是区间,查询 ...

  3. LightOJ 1097 - Lucky Number 线段树

    http://www.lightoj.com/volume_showproblem.php?problem=1097 题意:一个自然数序列,先去掉所有偶数项,在此基础上的序列的第二项为3,则删去所有3 ...

  4. LightOJ 1093 - Ghajini 线段树

    http://www.lightoj.com/volume_showproblem.php?problem=1093 题意:给定序列,问长度为d的区间最大值和最小值得差最大是多少. 思路:可以使用线段 ...

  5. lightoj 1084 - Winter(dp+二分+线段树or其他数据结构)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1084 题解:不妨设dp[i] 表示考虑到第i个点时最少有几组那么 if a[i ...

  6. LightOJ 1370 Bi-shoe and Phi-shoe 欧拉函数+线段树

    分析:对于每个数,找到欧拉函数值大于它的,且标号最小的,预处理欧拉函数,然后按值建线段树就可以了 #include <iostream> #include <stdio.h> ...

  7. LightOJ 1085(树状数组+离散化+DP,线段树)

    All Possible Increasing Subsequences Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format: ...

  8. LightOJ 1348 (树链剖分 + 线段树(树状数组))

    题目 Link 分析 典型的树链剖分题, 树链剖分学习资料 Code #include <bits/stdc++.h> using namespace std; const int max ...

  9. bzoj3932--可持久化线段树

    题目大意: 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分.超级计算机中的 任务用三元组(Si,Ei,Pi)描述,(Si,Ei,Pi)表示任务从第Si秒开始,在第 ...

随机推荐

  1. bootstrap datatable项目封装支持单选多选

    自己在开发项目是根据自己的项目后台框架封装的jquery datatable插件基本上能集成到任何项目中使用,当然封装的还不够完美,给大家学习 调侃 使用介绍:query_dataTable({tab ...

  2. 每周日与周四《红酒屋》探戈舞会"Wine Bar" Milonga_原生态拉丁_新浪博客

    每周日与周四<红酒屋>探戈舞会"Wine Bar" Milonga_原生态拉丁_新浪博客     每周日与周四<红酒屋>探戈舞会"Wine Bar ...

  3. C/C++:多个.cpp文件包括同一个.h头文件定义方法

    本文解决multiple definition of `XX'的错误.[出于反爬虫的目的,你不是在http://blog.csdn.net/zhanh1218上看到的,肯定不是最新最全的.] 关于头文 ...

  4. jquery mobile自己定义webapp开发实例(一)——前言篇

    用jquery mobile做了一段时间的webapp开发,准备用自己的一个小demo做一个模块化的分享 点击demo演示 手机演示二维码: 此demo已经是比較老的版本号,用户体验流畅度确实还存在非 ...

  5. openfire数据库中文乱码问题

    1.首先数据库的编码设置为UTF-8 2.项目的编码也要设置为UTF-8 假设数据保存到数据库的时候还有乱码  就要改动openfire配置文件 在openfire主文件夹\conf\openfire ...

  6. 1352 - Colored Cubes (枚举方法)

    There are several colored cubes. All of them are of the same size but they may be colored differentl ...

  7. JS 数组获取最大值

    一.一维数组 var a=[1,2,3,5]; alert(Math.max.apply(null, a));//最大值 alert(Math.min.apply(null, a));//最小值 二. ...

  8. android面试题目大全<完结部分>,android笔试题目集锦

    1. 下列哪些语句关于内存回收的说明是正确的? (b ) A. 程序员必须创建一个线程来释放内存   B.内存回收程序负责释放无用内存    C.内存回收程序允许程序员直接释放内存    D.内存回收 ...

  9. abap优化工具事务代码: ST05

  10. unix ourhdr.h myerr.h

    //在学UNIX环境高级编程时把下面两个头文件与源文件放在同一个文件下就可以正常编译了,我的是在ubuntu 12.04环境下,第一个程序编译和运行成功了,希望对大家有帮助(我已经根据网上的资料修改好 ...