Problem Description
In order to remember history, King plans to play losephus problem in the parade gap.He calls n(1≤n≤5000) soldiers,
counterclockwise in a circle, in label 1,2,3...n.

The first round, the first person with label 1 counts
off, and the man who report number 1 is
out.

The second round, the next person of the person who is out in the last round counts off, and the man who report number 2 is
out.

The third round, the next person of the person who is out in the last round counts off, and the person who report number 3 is
out.

The N - 1 round, the next person of the person who is out in the last round counts off, and the person who report number n−1 is
out.

And the last man is survivor. Do you know the label of the survivor?
 

Input
The first line contains a number T(0<T≤5000),
the number of the testcases.

For each test case, there are only one line, containing one integer n,
representing the number of players.
 

Output
Output exactly T lines.
For each test case, print the label of the survivor.
 

Sample Input

2
2
3
 

Sample Output

2
2

Hint:
For test case #1:the man who report number $1$ is the man with label $1$, so the man with label $2$ is survivor.

For test case #1:the man who report number $1$ is the man with label $1$, so the man with label 1 is out. Again the the man with label 2 counts $1$, the man with label $3$ counts $2$, so the man who report number $2$ is the man with label $3$. At last the man with label $2$ is survivor.

 
题意:n个人排成一圈,每回合杀死一个人,杀死后从那个人的下一个人开始数数,第i个回合数到i的那个人被杀死,问最后剩下的人的编号是多少。
思路:这题和约瑟夫环有点像,我们设f[n]为总人数为n时,按照题目中的规则杀,最后剩下来的人的编号是多少。我们每次杀死一个人后就重新编号,那么f[1]=0,即当最后只剩下一个人时,他经过重新编号后新的编号为0,那么在上一轮,他的编号为t=(0+n-1)%2,倒数第二轮他的编号为(t+n-2)%3,这样可以依次类推到f[n],就是答案了,所以这题只要用O(n^2)的复杂度初始化一下就行了。
           这种方法有个缺陷,就是不能知道每一次杀死的人的编号是什么,如果要知道每一次杀死的人的编号,我们可以用线段树做。我们在线段树上对于每一段维护其剩余的空位数,那么我们每次计算这一次杀死的人是线段树总区间的第几个人,这样就可以知道每次的编号是多少了,如果这题用线段树,为了防超时,可以打个表。


#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0)
#define maxn 6006
int a[maxn];
void init()
{
int i,j;
a[1]=1;
for(i=2;i<=5000;i++){
int num=0;
//for(j=i-1;j>=1;j--){
for(j=1;j<=i;j++){
num=(num+i-j+1)%j;
}
a[i]=num+1;
}
} int main()
{
int n,m,i,j,T;
init();
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
printf("%d\n",a[n] );
}
return 0;
}

下面的程序还要打个表再提交。
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0)
#define maxn 5006
struct node{
int l,r,num;
}b[4*maxn]; int n;
void build(int l,int r,int th)
{
int mid;
b[th].l=l;b[th].r=r;
b[th].num=r-l+1;
if(l==r)return;
mid=(l+r)/2;
build(l,mid,th*2);
build(mid+1,r,th*2+1);
} void update(int num,int cishu,int th)
{
int mid;
if(b[th].l==b[th].r){
b[th].num=0;
if(cishu==n)printf("%d,",b[th].l);
return;
}
if(b[th*2].num>=num){
update(num,cishu,th*2);
}
else{
update(num-b[th*2].num,cishu,th*2+1 );
}
b[th].num=b[th*2].num+b[th*2+1].num;
} int main()
{
int t,m,i,j,T,k;
freopen("o.txt","w",stdout);
for(n=1;n<=5000;n++)
{
build(1,n,1);
int p=1;
int tot=n;
for(k=1;k<=n;k++){
p=(p+k-1)%tot; //这里算这回合杀死的人的编号的空格数
if(p==0)p=tot;
update(p,k,1);
if(p==tot)p=1; //这里算出这回合杀死人后,下一回合第一个数数的编号,也可以看做是下一回合站第几个空位
tot--; //每次总人数减1
}
}
}

hdu5643 King's Game(约瑟夫环+线段树)的更多相关文章

  1. HDU 5643 King's Game | 约瑟夫环变形

    经典约瑟夫环 }; ; i<=n; i++) { f[i] = (f[i-] + k) % i; } 变形:k是变化的 #include <iostream> #include &l ...

  2. (简单) POJ 2750 Potted Flower,环+线段树。

    Description The little cat takes over the management of a new park. There is a large circular statue ...

  3. POJ 2886 Who Gets the Most Candies?(线段树&#183;约瑟夫环)

    题意  n个人顺时针围成一圈玩约瑟夫游戏  每一个人手上有一个数val[i]   開始第k个人出队  若val[k] < 0 下一个出队的为在剩余的人中向右数 -val[k]个人   val[k ...

  4. Codeforces 1089K - King Kog's Reception - [线段树][2018-2019 ICPC, NEERC, Northern Eurasia Finals Problem K]

    题目链接:https://codeforces.com/contest/1089/problem/K time limit per test: 2 seconds memory limit per t ...

  5. HDU5957 Query on a graph(拓扑找环,BFS序,线段树更新,分类讨论)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5957 题意:D(u,v)是节点u和节点v之间的距离,S(u,v)是一系列满足D(u,x)<=k的点 ...

  6. POJ 2886.Who Gets the Most Candies? -线段树(单点更新、类约瑟夫问题)

    线段树可真有意思呢续集2... 区间成段的替换和增减,以及区间求和等,其中夹杂着一些神奇的操作,数据离散化,简单hash,区间异或,还需要带着脑子来写题. 有的题目对数据的操作并不是直接按照题面意思进 ...

  7. cf1278D——树的性质+并查集+线段树/DFS判环

    昨天晚上本来想认真打一场的,,结果陪女朋友去了.. 回来之后看了看D,感觉有点思路,结果一直到现在才做出来 首先对所有线段按左端点排序,然后用并查集判所有边是否联通,即遍历每条边i,和前一条不覆盖它的 ...

  8. 线段树求后继+环——cf1237D

    /* 首先开三倍消环(两倍是不够的),倒序求值,线段树找一下后继即可 */ #include<bits/stdc++.h> using namespace std; #define N 3 ...

  9. HPU组队赛J:Ball King(线段树)

    时间限制 1 Second  内存限制 512 Mb 题目描述 HPU601球王争霸赛即将举行,ACMER纷纷参加. 现在有n个人报名参赛,每个人都有一个实力值 ai,实力值较大者获胜. 为保证比赛公 ...

随机推荐

  1. Java基础进阶类名整理

    类名综合 数学类: Math:数学运算 BigDecimal:用于精确计算数据 数组工具类: Arrays:数组工具类,用于对数组的操作 时间操作: JDK8以前: Date:表示一个时间,并面向对象 ...

  2. Java开发手册之工程结构

    1.在线上生产环境,JVM 的 Xms 和 Xmx 设置一样大小的内存容量,避免在 GC 后调整堆大小带来的压力. 2.给 JVM 环境参数设置-XX:+HeapDumpOnOutOfMemoryEr ...

  3. Spring Cloud微服务Sentinel+Apollo限流、熔断实战总结

    在Spring Cloud微服务体系中,由于限流熔断组件Hystrix开源版本不在维护,因此国内不少有类似需求的公司已经将眼光转向阿里开源的Sentinel框架.而以下要介绍的正是作者最近两个月的真实 ...

  4. Leetcode53. 最大子序列和

    问题 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 代码 贪心算法 核心思想就是检查之前 i-1 的元素和,如果小于零就舍弃--对应下面第六行 ...

  5. window安装nvm

    先说一下背景,最近做的两个项目一个是祖传angularjs1.X版本另一个是react hooks结合tailwindcss,前者angularjs的node版本比较低,而tailwindcss的no ...

  6. oracle ORA-00060死锁查询、表空间扩容

    --查看被锁住的表 select b.owner,b.object_name,a.session_id,a.locked_mode from v$locked_object a,dba_objects ...

  7. 词嵌入之Word2Vec

    词嵌入要解决什么问题 在自然语言系统中,词被看作最为基本的单元,如何将词进行向量化表示是一个很基本的问题,词嵌入(word embedding)就是把词映射为低维实数域向量的技术. 下面先介绍几种词的 ...

  8. PAT练习num1-害死人补偿命的3n+1猜想

    卡拉兹(Callatz)猜想: 对任何一个正整数 n,如果它是偶数,那么把它砍掉一半:如果它是奇数,那么把 ( 砍掉一半.这样一直反复砍下去,最后一定在某一步得到 n=1.卡拉兹在 1950 年的世界 ...

  9. Ubuntu20.04安装Typora

    Ubuntu20.04安装Typora 安装方法 # optional, but recommended sudo apt-key adv --keyserver keyserver.ubuntu.c ...

  10. spring data JPA 使用EntityentiListeners实现数据审计功能设计

    当系统中有审计需求时,特别是需要对某些数据进行动态监控时,我们可以使用EntityentiListeners来实现,当然这是基于使用JPA而不是mybatis的情况下. 当前我们的需求场景: 1.需要 ...