Infinite Fraction Path(HDU6223 + bfs + 剪枝)
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=6223
题目:

题意:
给你一个长度为n的数字串,开始时你选择一个位置(记为i,下标从0开始)做为起点,那么下一步将在(i × i + 1)%n处,将字典序最大的路径上的数打印出来。
思路:
要想字典序最大,那么只需每一步都是最大的即可。由题意可知,当起点确定时,所对应的数也就确定了。对于每一步,我们只需当前为最优即可,若第i步有t种方式使得当前数为x,那么下一步也将会有t种选择,那么我们可以用优先队列维护下一步的最优值(具体看代码。这题不加剪枝会T,由于操作中有取膜操作,那么对于同一步,取膜后的下一个位置极有可能会相同,也就是同一个位置重复入队列,这样后面的步骤都会重复,这样复杂度将会增大数倍,此时我们可以用一个set去重,防止同一步重复入队列。
代码实现如下:
#include <set>
#include <map>
#include <deque>
#include <ctime>
#include <stack>
#include <cmath>
#include <queue>
#include <string>
#include <cstdio>
#include <vector>
#include <iomanip>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pll;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef unsigned long long uLL; #define lson rt<<1
#define rson rt<<1|1
#define name2str(name)(#name)
#define bug printf("**********\n");
#define IO ios::sync_with_stdio(false);
#define debug(x) cout<<#x<<"=["<<x<<"]"<<endl;
#define FIN freopen("/home/dillonh/code/OJ/in.txt","r",stdin); const double eps = 1e-;
const int mod = 1e9 + ;
const int maxn = + ;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const LL INF = 0x3f3f3f3f3f3f3f3fLL; int T, n;
int t[maxn];
char s[maxn]; struct node {
int id, val, step;
bool operator < (const node& x) const {
return step == x.step ? val < x.val : step > x.step;
}
}nw, nxt; set<int> stc;
priority_queue<node> q; void bfs() {
stc.clear();
while(!q.empty()) q.pop();
int mx = -;
for(int i = ; i < n; i++) {
if(s[i] - '' > mx) mx = max(mx, s[i] - '');
}
for(int i = ; i < n; i++) { //将可能的起点压入队列中
if(s[i] - '' == mx) {
nw.id = i;
nw.val = mx;
nw.step = ;
q.push(nw);
}
}
int pp = ;
while(!q.empty()) {
nw = q.top(); q.pop();
if(nw.step >= n) return; //满足条件即可返回
if(nw.step == pp + ) {
stc.clear(); //到了新的一步需将set清空
mx = nw.val;
pp = nw.step;
}
if(nw.val == mx) {
t[nw.step] = nw.val;
nxt.id = (1LL * nw.id * nw.id + ) % n;
if(stc.count(nxt.id)) continue; //这个位置已经被压入过队列就不需要重复压入了
stc.insert(nxt.id);
nxt.val = s[nxt.id] - '';
nxt.step = nw.step + ;
q.push(nxt);
}
}
} int main() {
#ifndef ONLINE_JUDGE
FIN;
#endif
scanf("%d", &T);
for(int icase = ; icase <= T; icase++) {
scanf("%d", &n);
scanf("%s", s);
bfs();
printf("Case #%d: ", icase);
for(int i = ; i < n; i++) {
printf("%d", t[i]);
}
printf("\n");
#ifndef ONLINE_JUDGE
cout <<"It costs " <<clock() <<"ms\n";
#endif
}
return ;
}
Infinite Fraction Path(HDU6223 + bfs + 剪枝)的更多相关文章
- hdu6223 Infinite Fraction Path 2017沈阳区域赛G题 bfs加剪枝(好题)
题目传送门 题目大意:给出n座城市,每个城市都有一个0到9的val,城市的编号是从0到n-1,从i位置出发,只能走到(i*i+1)%n这个位置,从任意起点开始,每走一步都会得到一个数字,走n-1步,会 ...
- HDU6223 Infinite Fraction Path bfs+剪枝
Infinite Fraction Path 这个题第一次看见的时候,题意没搞懂就没做,这第二次也不会呀.. 题意:第i个城市到第(i*i+1)%n个城市,每个城市有个权值,从一个城市出发走N个城市, ...
- 2017沈阳区域赛Infinite Fraction Path(BFS + 剪枝)
Infinite Fraction Path Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java ...
- 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 ...
- 【赛后补题】(HDU6223) Infinite Fraction Path {2017-ACM/ICPC Shenyang Onsite}
场上第二条卡我队的题目. 题意与分析 按照题意能够生成一个有环的n个点图(每个点有个位数的权值).图上路过n个点显然能够生成一个n位数的序列.求一个最大序列. 这条题目显然是搜索,但是我队在场上(我负 ...
- [HDU6223]Infinite Fraction Path
题目大意: 有$n(n\leq 150,000)$个编号为$0_n-1$格子,每个格子有一个权值$w_i(0\leq w_i\leq 9)$.从任意一个点出发,按照一定的规则进行跳转.设当前的格子为$ ...
- hdu 6223 Infinite Fraction Path
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=6223 题意:给定长度为n的一串数字S,现在要按照一种规则寻找长度为n的数字串,使得该数字串的字典序最大 ...
- HDU6223——2017ICPC沈阳G Infinite Fraction Path
题意: 给定一个数字串,每个位子都能向(i*i+1)%n的位子转移,输出路径上,字典序最大的,长度为n的串. 参考:https://www.cnblogs.com/mountaink/p/954144 ...
- HDU6223 && 2017沈阳ICPC: G. Infinite Fraction Path——特殊图&&暴力
题意 给定一个数字串,每个位子都能向(i*i+1)%n的位子转移,输出在路径上.字典序最大的.长度为n的串($n \leq 150000$). 分析 先考虑一个暴力的方法,考虑暴力每个x,然后O(n) ...
随机推荐
- mysql EXPLAIN 参数表
测试样式: 参数详情:
- maven在eclipse上的配置
1,安装maven,配置MAVEN_HOME 和 bin Path环境变量. 2,配置maven setting文件 <mirror> <id>alimirrorId ...
- ACM数论之旅9---中国剩余定理(CRT)(壮哉我大中华╰(*°▽°*)╯)
中国剩余定理,又名孙子定理o(*≧▽≦)ツ 能求解什么问题呢? 问题: 一堆物品 3个3个分剩2个 5个5个分剩3个 7个7个分剩2个 问这个物品有多少个 解这题,我们需要构造一个答案 我们需要构造这 ...
- JVM 规范
http://files.cnblogs.com/files/dragonsuc/jls8.pdf 或者官网:http://files.cnblogs.com/files/dragonsuc/jls8 ...
- mysql 读写锁
1. 表读锁 lock table tablename read; 例如: 从上图中可以看到,当给表a加了读锁之后,该进程本身对表a是可读的,但是不可写,再看在另外一个进程中: 在另外一个进程中表a也 ...
- VLD 无法打印堆栈调用情况
调试时遇到了一个比较郁闷的问题:同样一个MFC工程,复制之后无任何附加操作,VLD便无法正常打印内存泄漏处的堆栈调用了 百度了一下,重要找到了答案:“VLD不支持中文” 复制工程时windows自动在 ...
- Codeforces 1060 F. Shrinking Tree
题目链接 一道思维好题啊...感觉这种类型的题很检验基本功是否扎实(像我这样的就挂了). 题意:你有一棵\(n\)个点的树,每次随机选择一条边,将这条边的两个端点合并,并随机继承两个点标号中的一个,问 ...
- LeetCode 717. 1-bit and 2-bit Characters
We have two special characters. The first character can be represented by one bit 0. The second char ...
- 洛谷 P4706 取石子 解题报告
P4706 取石子 题目描述 现在 Yopilla 和 yww 要开始玩游戏! 他们在一条直线上标记了 \(n\) 个点,从左往右依次标号为 \(1, 2, ..., n\) .然后在每个点上放置一些 ...
- 手动为容器设置ip地址
1.安装bridge-utils # aptitude install -y bridge-utils 2.配置网桥 # vim /etc/network/interfaces auto lo ifa ...