2017ACM/ICPC亚洲区沈阳站_Infinite Fraction Path(BFS)
来源:互联网 发布:交管12123软件下载 编辑:程序博客网 时间:2024/05/16 05:10
#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 - 1D[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;}
阅读全文
0 0
- 2017ACM/ICPC亚洲区沈阳站_Infinite Fraction Path(BFS)
- 2017ACM/ICPC亚洲区沈阳站
- 2016ACM/ICPC亚洲区沈阳站
- 【HDU 6228、2017ACM/ICPC亚洲区沈阳站】Tree
- 【HDU 6227 2017ACM/ICPC亚洲区沈阳站】Rabbits
- 2017ACM/ICPC亚洲区沈阳站【solved:6 / 13】
- 2017 ACM-ICPC 亚洲区(沈阳区)网络赛
- 【解题报告】2015ACM/ICPC亚洲区沈阳站
- HDU5950 2016ACM/ICPC亚洲区沈阳站现场赛
- Pagodas --(hdu5512)2015ACM/ICPC亚洲区沈阳站
- 2015ACM/ICPC亚洲区沈阳站 HDU
- 2017 ACM-ICPC 亚洲区(沈阳赛区)网络赛
- 2016ACM/ICPC亚洲区沈阳站两道水题
- 【HDU 6217 2017ACM/ICPC亚洲区沈阳站】BBP Formula
- 【HDU 6222 2017ACM/ICPC亚洲区沈阳站】Heron and His Triangle
- 2017ACM/ICPC亚洲区沈阳站_Wandering Robots(hash)_马尔科夫链_随机游走
- HDU 5512 Pagodas(gcd + 奇偶性判断)——2015ACM/ICPC亚洲区沈阳站
- HDU 5510 Bazinga(KMP)——2015ACM/ICPC亚洲区沈阳站
- java的时间操作
- C语言进阶-第18讲:单链表的遍历、创建、插入和删除结点
- 服务器基本框架
- windows/linux内存管理
- LeetCode.697 Degree of an Array
- 2017ACM/ICPC亚洲区沈阳站_Infinite Fraction Path(BFS)
- 判断平衡二叉树是否平衡
- HDU
- 队列的表示和基本操作的实现
- 正则表达式速查表
- 测试文档功能
- 调查:热水器穿个wifi马甲价格就翻倍 物联网不亲民
- Spring命名空间p的属性注入的方式
- ①tiny4412 Linux驱动开发之蜂鸣器