UVa LA 3882 - And Then There Was One 递推,动态规划 难度: 2
生活随笔
收集整理的這篇文章主要介紹了
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個數(1..n)圍成一個首尾相接的環,從m開始刪除,每隔k個刪除,最后留下來的是幾?
?
思路
如劉書,首先是要找到遞推關系。
1. 把起點視作編號0,f[n]為還剩下n個數(編號當然是緊挨的)的時候留下的最后一個編號,那么,明顯f[n]與f[n - 1]有關系
2. 具體有什么關系呢?f[n]剩下的最后一個元素對應n個編號,刪除第0個,重新以第k個編號為新起點的n-1個元素對應的f[n - 1],對應到公式上為(f[i - 1] + k - 1) % (i - 1) + 1
3. 現在不是從0開始,而是從m開始,也就是說最后還存在一個關系為:編號j的元素對應的值為(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?
轉載于:https://www.cnblogs.com/xuesu/p/10419344.html
總結
以上是生活随笔為你收集整理的UVa LA 3882 - And Then There Was One 递推,动态规划 难度: 2的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 织梦CMS调用指定顶级栏目名称的方法
- 下一篇: 6 有序集合ZSet(Sorted Se