传送门: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. highcharts dynamic change line color

    mouseOut: function(){ this.series.graph.attr({"stroke","#ccc"}) }

  2. 基于visual Studio2013解决C语言竞赛题之1012连接字符串

         题目 解决代码及点评 /* 编写一个函数JOIN,让它实现字符串连接运算功能. */ #include <stdio.h> #include <stdl ...

  3. VS2008+Qt 项目目录

    1.项目开发环境:VS2008,QT4.7 2.项目的目录: 1)PETCT是解决方案名字 2)Bin目录存放所有动态链接库和执行档,包括自己的产出和第三方库,区分Release和Debug两个版本. ...

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

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

  5. Research notes for Scientific Writing

    Noun. complacency n. 自得;自鸣得意 nomenclature  n. 专业术语:e.g. the nomenclature of music; 音乐的专门术语 Verb. abs ...

  6. 使用最新的log4cplus(1.1.1)隔离不同的 log 文件输出

    部分参考了博客. http://www.cppblog.com/tx7do/articles/11719.html 基于脚本配置来过滤log信息 除了通过程序实现对log环境的配置之外,log4cpl ...

  7. 如何使用NArrange进行代码优化

    Narrange是一个.NET代码管理工具.它可以对源代码自动进行美化和把类成员分成一个组.区域.目前支持C#.VB.NET,将来会支持更多.NET上的语言. 主要的作用是: ◆ 减少程序员的开发时间 ...

  8. Win2003 Server磁盘配额揭密之补遗篇

    Win2003 Server磁盘配额揭密之补遗篇 [ 作者:茶乡浪子    转贴自:it168.com    点击数:5011    更新时间:2005-1-17  ]   执行查看用户的磁盘配额信息 ...

  9. 奇葩的UI引用LayoutInflater.from问题

    今收到BUG一枚,一个页面的EditText的风格变为了系统默认(系统经过定制,风格和普通的不同) 经检查xml无任何不妥,最终问题出在LayoutInflater.from上. 如果LayoutIn ...

  10. TEXT文本编辑框4 点击按钮读取文本框内容到内表

    *&---------------------------------------------------------------------* *& Report ZTEST_CWB ...