2017沈阳区域赛Infinite Fraction Path(BFS + 剪枝)
Infinite Fraction Path
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 5756 Accepted Submission(s): 1142
The kingdom has N cities numbered from 0 to N - 1 and you are given an array D[0 ... N - 1] of decimal digits (0 ≤ D[i] ≤ 9, D[i] is an integer). The destination of the only one-way road start from the i-th city is the city labelled (i2 + 1)%N.
A path beginning from the i-th city would pass through the cities u1,u2,u3, and so on consecutively. The path constructs a real number A[i], called the relevant fraction such that the integer part of it is equal to zero and its fractional part is an infinite decimal fraction with digits D[i], D[u1], D[u2], and so on.
The best infinite fraction path is the one with the largest relevant fraction
For each test case, the first line contains the integer N (1 ≤ N ≤ 150000). The second line contains an array ofdigits D, given without spaces.
The summation of N is smaller than 2000000.
3
149
5
12345
7
3214567
9
261025520
Case #2: 53123
Case #3: 7166666
Case #4: 615015015
/*************************************************************************
> File Name: hdu-6223.infinite_fraction_path.cpp
> Author: CruelKing
> Mail: 2016586625@qq.com
> Created Time: 2019年09月18日 星期三 15时57分37秒
本题思路:BFS暴力,先找出最大的几个数的位置,接着bfs每次寻找下一层最大的值,直到
找到答案为止.
注意剪枝:1.如果在当前层寻找到了比最大值小的值直接pop.
2.如果当前层有多个结点通往下一层的同一个结点,只需要保留一个就行了.
************************************************************************/ #include <cstdio>
#include <queue>
#include <cstring>
using namespace std; typedef long long ll; const int maxn = + ;
char str[maxn], ans[maxn]; int M[maxn], tot;
bool vis[maxn]; int n; ll sta[maxn], top; struct node {
ll index, step;
}; bool operator < (node a, node b) {
if(a.step == b.step) return str[a.index] < str[b.index];
return a.step > b.step;
} void bfs() {
priority_queue <node> que;
for(int i = ; i < tot; i ++) {
que.push((node) {M[i], });//最大值入队列
}
ll last = ;
while(!que.empty()) {
node now = que.top();
que.pop();
if(last != now.step) {
last = now.step;
while(top) vis[sta[-- top]] = false;//把当前标记过得结点都释放,因为他们还可以继续访问
}
if(ans[now.step] > str[now.index] || now.step >= n || vis[now.index]) continue;//如果在当前层当前位置已经被访问过了就跳过这个结点, 如果当前已经找到了n个数就跳过这个结点,如果已经当前结点字典序小于之前访问过的最大值就跳过这个结点
sta[top ++] = now.index;//把当前访问的结点放入队列并标记
vis[now.index] = true;
ans[now.step] = str[now.index];//更新答案
que.push((node) {(now.index * now.index + ) % n, now.step + });//跳跃
}
while(top) vis[sta[-- top]] = false;
ans[n] = '\0';
} int _case; void print() {
printf("Case #%d: %s\n", ++_case, ans);
} int main() {
int t, _case = ;
int Max;
char k;
scanf("%d", &t);
while(t --) {
for(int i = ; i < n; i ++) ans[i] = ;
k = ;
tot = ;
scanf("%d", &n);
scanf("%s", str);
for(int i = ; i < n; i ++) {
if(str[i] > k) {
k = str[i];
}
}
for(int i = ; i < n; i ++) {
if(str[i] == k) {
M[tot ++] = i;//存储字符串中的最大值
}
}
bfs();
print();
}
return ;
}
2017沈阳区域赛Infinite Fraction Path(BFS + 剪枝)的更多相关文章
- HDU6223 Infinite Fraction Path bfs+剪枝
Infinite Fraction Path 这个题第一次看见的时候,题意没搞懂就没做,这第二次也不会呀.. 题意:第i个城市到第(i*i+1)%n个城市,每个城市有个权值,从一个城市出发走N个城市, ...
- HDU6223 && 2017沈阳ICPC: G. Infinite Fraction Path——特殊图&&暴力
题意 给定一个数字串,每个位子都能向(i*i+1)%n的位子转移,输出在路径上.字典序最大的.长度为n的串($n \leq 150000$). 分析 先考虑一个暴力的方法,考虑暴力每个x,然后O(n) ...
- hdu6223 Infinite Fraction Path 2017沈阳区域赛G题 bfs加剪枝(好题)
题目传送门 题目大意:给出n座城市,每个城市都有一个0到9的val,城市的编号是从0到n-1,从i位置出发,只能走到(i*i+1)%n这个位置,从任意起点开始,每走一步都会得到一个数字,走n-1步,会 ...
- 2017 ACM/ICPC 沈阳 G题 Infinite Fraction Path
The ant Welly now dedicates himself to urban infrastructure. He came to the kingdom of numbers and s ...
- Infinite Fraction Path HDU 6223 2017沈阳区域赛G题题解
题意:给你一个字符串s,找到满足条件(s[i]的下一个字符是s[(i*i+1)%n])的最大字典序的长度为n的串. 思路:类似后缀数组,每次倍增来对以i开头的字符串排序,复杂度O(nlogn).代码很 ...
- hdu6229 Wandering Robots 2017沈阳区域赛M题 思维加map
题目传送门 题目大意: 给出一张n*n的图,机器人在一秒钟内任一格子上都可以有五种操作,上下左右或者停顿,(不能出边界,不能碰到障碍物).题目给出k个障碍物,但保证没有障碍物的地方是强联通的,问经过无 ...
- HDU 6229 Wandering Robots(2017 沈阳区域赛 M题,结论)
题目链接 HDU 6229 题意 在一个$N * N$的格子矩阵里,有一个机器人. 格子按照行和列标号,左上角的坐标为$(0, 0)$,右下角的坐标为$(N - 1, N - 1)$ 有一个机器人, ...
- Heron and His Triangle 2017 沈阳区域赛
A triangle is a Heron’s triangle if it satisfies that the side lengths of it are consecutive integer ...
- 2017 ICPC/ACM 沈阳区域赛HDU6223
Infinite Fraction Path Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java ...
随机推荐
- keep-alive前进没有刷新
https://segmentfault.com/a/1190000012083511
- 【NOIP2012模拟10.31】掷骰子
题目 太郎和一只免子正在玩一个掷骰子游戏.有一个有N个格子的长条棋盘,太郎和兔子轮流掷一个有M面的骰子,骰子M面分别是1到M的数字.且掷到任意一面的概率是相同的.掷到几.就往前走几步.当谁走到第N格时 ...
- Maven手动命令行导入ojdbc6
Maven项目中导入Oracle的驱动包时,可能会出现像我一样下载资源不成功的情况,如下图所示: 出现这种情况的原因其实是因为Oracle的授权问题,这样的话,我们在需要使用Oracle的驱动包时, ...
- Mybatis 一对多 关联查询查询
一对多 与 一对一 查询有许多相似之处. 最主要的区别是 查询结果是list,与之对应的标签为collection. 班级和学生,一个班有多个学生,而每个学生只能属于一个班. 此时班级编号作为学生表的 ...
- csdn专家主页
百度张瑞琪: http://blog.csdn.net/abcjennifer 深度学习系列教程: http://suanfazu.com/t/caffe/9479
- spring boot shiro redis整合基于角色和权限的安全管理-Java编程
一.概述 本博客主要讲解spring boot整合Apache的shiro框架,实现基于角色的安全访问控制或者基于权限的访问安全控制,其中还使用到分布式缓存redis进行用户认证信息的缓存,减少数据库 ...
- C#实现读取指定盘符硬盘序列号的方法
文章主要介绍了C#实现读取指定盘符硬盘序列号的方法,涉及C#针对硬件属性的相关操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下 本文实例讲述了C#实现读取指定盘符硬盘序列号的方法.分享给大家供 ...
- VBA通过C#以API方式调用JS脚本函数
http://www.cnblogs.com/Charltsing/p/JSDotNetAPI.html 在网页采集中,很多时候需要运行网站下载的某个js文件中的函数,以计算Request参数.VBA ...
- 方法一破解:Excel工作表保护密码
在excel2016中实测验证过有效 在Excel中,为了保护自已的工作表不被修改,我们可以添加保护密码. 操作步骤: 1.把Excel文件的扩展名xlsx修改为Rar.瞬间Excel文件变成了压缩包 ...
- JUnit@Before失效
在学习Shiro的过程中需要使用到JUnit@Before注解配合测试,但是无论如何,@Before下面的方法都不按照预期的执行,困扰良久,后来各种百度终于找到根源,今分享于诸公,望能解诸公之急: J ...