题目链接:

  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 + 剪枝)的更多相关文章

  1. hdu6223 Infinite Fraction Path 2017沈阳区域赛G题 bfs加剪枝(好题)

    题目传送门 题目大意:给出n座城市,每个城市都有一个0到9的val,城市的编号是从0到n-1,从i位置出发,只能走到(i*i+1)%n这个位置,从任意起点开始,每走一步都会得到一个数字,走n-1步,会 ...

  2. HDU6223 Infinite Fraction Path bfs+剪枝

    Infinite Fraction Path 这个题第一次看见的时候,题意没搞懂就没做,这第二次也不会呀.. 题意:第i个城市到第(i*i+1)%n个城市,每个城市有个权值,从一个城市出发走N个城市, ...

  3. 2017沈阳区域赛Infinite Fraction Path(BFS + 剪枝)

    Infinite Fraction Path Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java ...

  4. 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 ...

  5. 【赛后补题】(HDU6223) Infinite Fraction Path {2017-ACM/ICPC Shenyang Onsite}

    场上第二条卡我队的题目. 题意与分析 按照题意能够生成一个有环的n个点图(每个点有个位数的权值).图上路过n个点显然能够生成一个n位数的序列.求一个最大序列. 这条题目显然是搜索,但是我队在场上(我负 ...

  6. [HDU6223]Infinite Fraction Path

    题目大意: 有$n(n\leq 150,000)$个编号为$0_n-1$格子,每个格子有一个权值$w_i(0\leq w_i\leq 9)$.从任意一个点出发,按照一定的规则进行跳转.设当前的格子为$ ...

  7. hdu 6223 Infinite Fraction Path

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=6223 题意:给定长度为n的一串数字S,现在要按照一种规则寻找长度为n的数字串,使得该数字串的字典序最大 ...

  8. HDU6223——2017ICPC沈阳G Infinite Fraction Path

    题意: 给定一个数字串,每个位子都能向(i*i+1)%n的位子转移,输出路径上,字典序最大的,长度为n的串. 参考:https://www.cnblogs.com/mountaink/p/954144 ...

  9. HDU6223 && 2017沈阳ICPC: G. Infinite Fraction Path——特殊图&&暴力

    题意 给定一个数字串,每个位子都能向(i*i+1)%n的位子转移,输出在路径上.字典序最大的.长度为n的串($n \leq 150000$). 分析 先考虑一个暴力的方法,考虑暴力每个x,然后O(n) ...

随机推荐

  1. Robot Framework 教程 (6) - 使用条件表达式

    本篇文章,主要对如何在Robot Framework中使用条件表达式做过程控制作说明. 按照Robot Framework的官方文档介绍,Robot Framework并不建议在TestCase或Ke ...

  2. gitlab修改root密码

    在root用户下,执行 [root@localhost gitlab]# sudo gitlab-rails console production -------------------------- ...

  3. C++ STL 常用算术和生成算法

    C++ STL 常用算术和生成算法 accumulate() accumulate: 对指定范围内的元素求和,然后结果再加上一个由val指定的初始值. #include<numeric> ...

  4. 初次使用http打不开页面,使用https打开过后使用http协议又能正常访问

    http协议为什么打不开https站点 在访问一个https的站点,比如 https://www.aaa.com,首次访问时,访问的地址是 http://www.aaa.com,(不加S),出现的是网 ...

  5. 【转】VMware虚拟机三种网络模式详解

    由于Linux目前很热门,越来越多的人在学习Linux,但是买一台服务放家里来学习,实在是很浪费.那么如何解决这个问题?虚拟机软件是很好的选择,常用的虚拟机软件有VMware Workstations ...

  6. loj2542 「PKUWC2018」随机游走 【树形dp + 状压dp + 数学】

    题目链接 loj2542 题解 设\(f[i][S]\)表示从\(i\)节点出发,走完\(S\)集合中的点的期望步数 记\(de[i]\)为\(i\)的度数,\(E\)为边集,我们很容易写出状态转移方 ...

  7. oracle 11g 压缩数据文件

    通过以下语句直接分析出每个数据库文件可压缩量 select a.file#, a.name, a.bytes CurrentMB, ceil(HWM ResizeTo, (a.bytes Releas ...

  8. android:shape的使用(+圆角ListView)(转)

    http://dev.10086.cn/cmdn/wiki/index.php?doc-view-6087.html Android中常常使用shape来定义控件的一些显示属性,今天看了一些shape ...

  9. 【bzoj3881】【Coci2015】Divljak

    题解 对$S$集合ac建自动机,把$T_{i}$放在里面跑,记录路径上的所有节点并对它们在fail树上求到root的树链并: 这样就得到了$T_{i}$所有的子串: 动态将$T_{i}$加入直接用树状 ...

  10. Dubbo、Zookeeper集群搭建及Rose使用心得(一)

    接触这个两三月了,是时候总结一下使用的方法以及心得体会了.我是一个菜鸟,下面写的如有错误,还请各位前辈指出.废话不多说,正式开始. 一.简介 Dubbo是Alibaba开源的分布式服务框架,它最大的特 ...