UVALive - 3882:And Then There Was One】的更多相关文章

约瑟夫环 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…
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…
题目链接: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个人组成的约瑟夫环按照…
题意:给定一棵带权树,Q次询问,每次询问路径上的中位数. 思路:中位数分边数奇偶考虑,当当边数为num=奇时,结果就算路径第num/2+1大,用主席树做即可... (做了几道比较难的主席树,都wa了...只有来刷刷水题,准备晚上的CF了) #include<bits/stdc++.h> using namespace std; ; int Laxt[maxn],Next[maxn],To[maxn],cost[maxn],cnt; ][],dep[maxn],rt[maxn],tot; str…
题目链接 题意 给出n个王国和n*n的矩阵,mp[i][j] 代表第 i 个王国欠第 j 个王国 mp[i][j] 块钱.如果当前的王国处于负债状态,那么这个王国就会被消除,和它相连的王国的债务都会被清除.因此会产生连锁反应,使得最后可能只剩下一个王国.输出对于每种情况,最后可能只剩下的王国有哪些. 思路 一开始的想法就是DFS+状压,但是TLE了,队友和我说没记录状态,那样的复杂度差不多是O(n!)的.后面队友写了一个,疯狂WA. 想了好久都没发现的错误:当时做法是全局记录一个原始的图和一个当…
pro:有D个字母,每个字母有自己的权值,现状需要用它们拼出N个单词,使得这些单词互相不为另外一个的前缀. 且单词的权值和最小.D<=200; N<=200; sol:如果建立字典树,那个每个单词的权值权值救赎根到叶子的路径权重和. 感觉有点想哈夫曼树,但是没什么大的关系,因为不能倒推. 由于ND比较小,我们直接贪心,维护一个大小为N+D的数组b[],一直更新,原则如下:每次排序b[],把 b[1]替换为b[1]+a[]:一直操作,直到不能再变小为止. #include<bits/std…
用KMP里面的next数组即可,原理就是next数组的原理 #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> #include<vector> #define MAXN 1000000+10 #define ll long long using namespace std; char s[MAXN]; int n; int nxt[MAXN]; voi…
发现字典里面的单词数目多且长度短,可以用字典树保存 f[i]表示s[i~L]的分割方式,则有f[i]=∑f[i+len(word[j])]   其中word[j]为s[i~L]的前缀 注意字典树又叫前缀树,所以用前缀更方便,否则按顺序dp的话就要把字符倒序了 复杂度O(L*l) L为字符串长度,l为单词长度 #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> #de…
优美的线段树 #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> #include<cmath> #define MAXN 500000+10 #define ll long long #define pii pair<int,int> #define mp make_pair using namespace std; int a[MAXN]…
加权并查集 #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> #define MAXN 20000+10 #define pii pair<int,int> using namespace std; int fa[MAXN]; int d[MAXN]; int n; int Abs(int x){ ?x:-x); } pii find(int x){…