UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There Was One / POJ 3517 And Then There Was One / Aizu 1275 And Then There Was One (动态规划,思维题) Description Let's play a stone removing game. Initially, n ston…
And Then There Was One UVALive - 3882 Sample Input   Sample Output //设f[i]为(原约瑟夫问题)第i次要删除的标号 #include<cstdio> using namespace std; ; int n,m,k,f[N]; int main(){ &&n){ //f[1]=0; //for(int i=2;i<=n;i++) f[i]=(f[i-1]+k)%i; //int ans=(m-k+f[n…
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1883 题意:n个人围成一圈,第一次删第m个人,然后每数K个删一个人,求最后一个人的编号 分析:典型的约瑟夫问题,和杀人游戏差不多,杀人游戏描述如下: 推导过程: 首先,我们要对问题描述改一下,n个人编号为0,1,2,….,n-1,f[n]表示n个人组成的约瑟夫环按照…
看题传送门 题目大意: N个数排成一圈,第一次删除m,以后每k个数删除一次,求最后一被删除的数. 如果这题用链表或者数组模拟整个过程的话,时间复杂度都将高达O(nk),而n<=10000,k<=10000 目测会直接TLE. 那么有没有其他的方法呢?答案是有的. 我们先忽略掉m, 分析一下每k个数删除一次,那就是经典的约瑟夫问题了. 那么,将每个数(1~n)按顺序编号为0~n-1 设第一个删除的数的编号为x,则x= k %n-1 (注意是编号,真正删除的数为编号+1) 那么剩下的n-1个数可以…
约瑟夫环 f[i]表示有i个人先处理第k个人,最后被处理的人是谁 #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> #define MAXN 10005 using namespace std; int f[MAXN]; int n,m,k; int main() { ){ scanf("%d%d%d",&n,&k,&m…
And Then There Was One Time limit: 3.000 seconds Let’s play a stone removing game. Initially, n stones are arranged on a circle and numbered 1,...,n clockwise (Figure 1). You are also given two numbers k and m. From this state, remove stones one by o…
入口 UVALive - 3882 #include<cstdio> using namespace std; ; int n,m,k,f[N]; int main(){ //f[i]表示第i次要删的数字 &&n){ //1 //f[1]=0; //for(int i=2;i<=n;i++) f[i]=(f[i-1]+k)%i; //int ans=(m-k+f[n]+1)%n; //2 //int s=0; //for(int i=2;i<=n;i++) s=(s…
就是经典约瑟夫环问题的裸题 我一开始一直没理解这个递推是怎么来的,后来终于理解了 假设问题是从n个人编号分别为0...n-1,取第k个, 则第k个人编号为k-1的淘汰,剩下的编号为  0,1,2,3...k-2,k,k+1,k+2... 此时因为从刚刚淘汰那个人的下一个开始数起,因此重新编号 把k号设置为0,则 k    0 k+1 1 ... 0 n-k 1 n-k+1 假设已经求得了n-1个人情况下的最终胜利者保存在f[n-1]中,则毫无疑问,该胜利者还原到原来的真正编号即为 (f[n-1]…
方法一.自定义的链表实现 package com.code.yuesefu; public class YueSeFuList { public static void main(String[] args) { ;//申请一个指定长度的链表 Node n = YueSeFuList.createNodes(count); ;i<count;i++){ Node second = n.next;//第2个 n = n.next.next;//第3个 System.out.println(n.it…
约瑟夫问题(有时也称为约瑟夫斯置换,是一个出现在计算机科学和数学中的问题.在计算机编程的算法中,类似问题又称为约瑟夫环.又称“丢手绢问题”.) 有这样一个故事,15个教徒和15个非教徒在深海遇险必须讲一半的人投到海中,其余的人才能获救,于是想出这样过一个办法,30个人围城一圈.从第一个人开始一次报数,每次数到9,就将这个人扔到海中,直到剩余15个人为止.问怎样的排法,使得每次扔到海中都是非教徒 现在的一种办法就是一个Boolean数组来模拟30个人,非教徒为false.刚开始都没true,当数到…