2017ACM/ICPC亚洲区沈阳站_Infinite Fraction Path(BFS)
2017-11-14 21:50
441 查看
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #define INF 0x3f3f3f3f #define rep0(i, n) for (int i = 0; i < n; i++) #define rep1(i, n) for (int i = 1; i <= n; i++) #define rep_0(i, n) for (int i = n - 1; i >= 0; i--) #define rep_1(i, n) for (int i = n; i > 0; i--) #define MAX(x, y) (((x) > (y)) ? (x) : (y)) #define MIN(x, y) (((x) < (y)) ? (x) : (y)) #define mem(x, y) memset(x, y, sizeof(x)) #define MAXN 150000 + 10 #include <queue> /** 题目大意 一个长度为n的数列,下标 0 ~ n - 1 D[i] -> D[(i * i + 1) % n] 组成每个节点有一个出度的有向图 遍历出一条长度为n的词典序最大的路径 思路 先使序列中最大的节点入队 用两个变量ma1 ma2 ma1 记录此层次节点的最大值(真) ma2 记录下一层节点最大值(假) 当遍历完一层时 ma1 = m2 用book标记同层内节点不重复入队 */ using namespace std; typedef long long LL; char d[MAXN]; LL n, nxt[MAXN], cnt; int ma1, ma2; queue< pair <LL, LL> > q; ///层次 下标 int book[MAXN]; void work() { LL step = -1; while (cnt < n) { pair<LL, LL> tmp = q.front(); q.pop(); if (tmp.first > step) { step++; ma1 = ma2; if (cnt < n) { printf("%d", ma1); cnt++; } ma2 = -1; if (d[tmp.second] - '0' == ma1) { pair<LL, LL> t; t.second = nxt[tmp.second]; t.first = tmp.first + 1; q.push(t); book[t.second] = t.first; ma2 = d[ nxt[tmp.second] ] - '0'; } } else if (d[tmp.second] - '0' == ma1 && d[ nxt[tmp.second] ] - '0' >= ma2 && book[nxt[tmp.second]] != tmp.first + 1) { if (d[ nxt[tmp.second] ] - '0' > ma2) ma2 = d[ nxt[tmp.second] ] - '0'; pair<LL, LL> t; t.second = nxt[tmp.second]; t.first = tmp.first + 1; book[t.second] = tmp.first + 1; q.push(t); } } } int main() { #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); #endif // ONLINE_JUDGE int t, kase = 0; scanf("%d", &t); while (t--) { //mem(book, 0); scanf("%lld", &n); scanf("%s", d); while (!q.empty()) q.pop(); ma1 = -1; ma2 = -1; for (LL i = 0; i < n; i++) book[i] = 0; for (LL i = 0; i < n; i++) { nxt[i] = (i * i + 1) % n; if (d[i] - '0' > ma2) { while (!q.empty()) q.pop(); pair<LL, LL> tmp; tmp.first = 0; tmp.second = i; q.push(tmp); ma2 = d[i] - '0'; } else if (d[i] - '0' == ma2) { pair<LL, LL> tmp; tmp.first = 0; tmp.second = i; q.push(tmp); } } cnt = 0; printf("Case #%d: ", ++kase); work(); printf("\n"); } return 0; }
相关文章推荐
- 【赛后补题】(HDU6223) Infinite Fraction Path {2017-ACM/ICPC Shenyang Onsite}
- 2017 ACM/ICPC 沈阳 G题 Infinite Fraction Path
- hdu6229--2017ACM/ICPC亚洲区沈阳站(M题)
- hdu6223--2017ACM/ICPC亚洲区沈阳站(G题)BFS+剪枝
- 2017ACM/ICPC亚洲区沈阳站(部分解题报告)
- 计蒜客-2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛G题Query on a strin(树状数组+暴力更新)
- 2015ACM/ICPC亚洲区沈阳站 HDU
- 2017ACM/ICPC亚洲区沈阳站【solved:6 / 13】
- HDU 5512 Pagodas 2015ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)
- HDU 5948 Thickest Burger 【模拟】 (2016ACM/ICPC亚洲区沈阳站)
- 计蒜客-2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛J题Our Journey of Dalian Ends (最小费用最大流)
- HDU6201 | 2017 ACM-ICPC 亚洲区(沈阳赛区)网络赛-H transaction transaction transaction
- 计蒜客 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 B coin(求乘法逆元)
- Frogs (hdu5514)——2015ACM/ICPC亚洲区沈阳站(容斥定理)
- hdu 5521 Meeting 2015ACM/ICPC亚洲区沈阳站-重现赛
- HDU 5948 Thickest Burger 【模拟】 (2016ACM/ICPC亚洲区沈阳站)
- HDU 5950 Recursive sequence(构造矩阵+矩阵幂)——2016ACM/ICPC亚洲区沈阳站
- HDU 5954 Do not pour out 积分 二分 — 2016ACM-ICPC亚洲区沈阳站
- 2017 ACM-ICPC 亚洲区(北京赛区)网络赛
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛