UVa LA 3882 - And Then There Was One 递推,动态规划 难度: 2
題目
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1883
題意
共有n個(gè)數(shù)(1..n)圍成一個(gè)首尾相接的環(huán),從m開始刪除,每隔k個(gè)刪除,最后留下來的是幾?
?
思路
如劉書,首先是要找到遞推關(guān)系。
1. 把起點(diǎn)視作編號(hào)0,f[n]為還剩下n個(gè)數(shù)(編號(hào)當(dāng)然是緊挨的)的時(shí)候留下的最后一個(gè)編號(hào),那么,明顯f[n]與f[n - 1]有關(guān)系
2. 具體有什么關(guān)系呢?f[n]剩下的最后一個(gè)元素對(duì)應(yīng)n個(gè)編號(hào),刪除第0個(gè),重新以第k個(gè)編號(hào)為新起點(diǎn)的n-1個(gè)元素對(duì)應(yīng)的f[n - 1],對(duì)應(yīng)到公式上為(f[i - 1] + k - 1) % (i - 1) + 1
3. 現(xiàn)在不是從0開始,而是從m開始,也就是說最后還存在一個(gè)關(guān)系為:編號(hào)j的元素對(duì)應(yīng)的值為(j + m - 1) % n + 1?
感想
1. 和劉汝佳的算的不一致,不太理解劉書怎么算的,也許定義方式略有不同?
?
代碼
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <tuple> #define LOCAL_DEBUG using namespace std; const int MAXN = 1e4 + 4; int f[MAXN];int main() { #ifdef LOCAL_DEBUGfreopen("C:\\Users\\Iris\\source\\repos\\ACM\\ACM\\input.txt", "r", stdin);//freopen("C:\\Users\\Iris\\source\\repos\\ACM\\ACM\\output.txt", "w", stdout); #endif // LOCAL_DEBUG//int T; // scanf("%d", &T);int n, k, m;for (int ti = 1;scanf("%d%d%d", &n, &k, &m) == 3 && n; ti++) {f[1] = 0;for (int i = 2; i <= n; i++) {f[i] = (f[i - 1] + k - 1) % (i - 1) + 1;}int ans = (m - 1 + f[n]) % n;printf("%d\n", ans + 1);}return 0; } View Code?
轉(zhuǎn)載于:https://www.cnblogs.com/xuesu/p/10419344.html
總結(jié)
以上是生活随笔為你收集整理的UVa LA 3882 - And Then There Was One 递推,动态规划 难度: 2的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 织梦CMS调用指定顶级栏目名称的方法
- 下一篇: JavaEE 企业级分布式高级架构师课程