题意:

  给定一个数字串,每个位子都能向(i*i+1)%n的位子转移,输出路径上,字典序最大的,长度为n的串。

参考:https://www.cnblogs.com/mountaink/p/9541442.html

思路:

  BFS,

  一个数字肯定是最高位越大,这个数字本身就越大,所以肯定第一位要取最大值,在这一位取最大值的时候后面每一位都要尽量最大,所以想到bfs。

但是bfs肯定要剪枝,怎么剪枝呢?

  1、按照思路,我要取每一位尽可能大的值,所以某一个状态的某一位小于我当前以及有的解,这个状态肯定被舍弃。

    这是最好想的思路,但是如果对于一个全是9的数列,这个剪枝完全没有用,所以必须有其他的剪枝。

  2、如果到了从不同起点到达某一个位置,在答案数列中的层次是一样的,舍弃掉。(换句话说,不同起点经过相同步数到达同一座城市,那么后续的状态都是一样的了,所以没必要再走下去),

具体怎么实现呢,一开始将数列中所有最大值所在的位置入队,对于某一种状态,看他的下一步是否比答案中更优或者相等(答案数组初始化为-1),如果更优或者相等则重新入队。 对于新状态,先检查一下当前位置的值是不是和答案数组中当前位置的最大值相等,如果不相等,舍弃,如果相等,判断一下有没有状态在相同步数的情况下已经走到这一步了,有则舍弃,没有则更新一下,然后重复上述操作。

  1. // #pragma GCC optimize(3)
  2. // #pragma comment(linker, "/STACK:102400000,102400000") //c++
  3. // #pragma GCC diagnostic error "-std=c++11"
  4. // #pragma comment(linker, "/stack:200000000")
  5. // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  6.  
  7. #include <algorithm>
  8. #include <iterator>
  9. #include <iostream>
  10. #include <cstring>
  11. #include <cstdlib>
  12. #include <iomanip>
  13. #include <bitset>
  14. #include <cctype>
  15. #include <cstdio>
  16. #include <string>
  17. #include <vector>
  18. #include <stack>
  19. #include <cmath>
  20. #include <queue>
  21. #include <list>
  22. #include <map>
  23. #include <set>
  24. #include <cassert>
  25.  
  26. using namespace std;
  27. #define lson (l , mid , rt << 1)
  28. #define rson (mid + 1 , r , rt << 1 | 1)
  29. #define debug(x) cerr << #x << " = " << x << "\n";
  30. #define pb push_back
  31. #define pq priority_queue
  32.  
  33. typedef long long ll;
  34. typedef unsigned long long ull;
  35. //typedef __int128 bll;
  36. typedef pair<ll ,ll > pll;
  37. typedef pair<int ,int > pii;
  38. typedef pair<int,pii> p3;
  39.  
  40. //priority_queue<int> q;//这是一个大根堆q
  41. //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
  42. #define fi first
  43. #define se second
  44. //#define endl '\n'
  45.  
  46. #define OKC ios::sync_with_stdio(false);cin.tie(0)
  47. #define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
  48. #define REP(i , j , k) for(int i = j ; i < k ; ++i)
  49. #define max3(a,b,c) max(max(a,b), c);
  50. //priority_queue<int ,vector<int>, greater<int> >que;
  51.  
  52. const ll mos = 0x7FFFFFFF; //
  53. const ll nmos = 0x80000000; //-2147483648
  54. const int inf = 0x7f7f7f7f;
  55. const ll inff = 0x3f3f3f3f3f3f3f3f; //
  56. const int mod = 1e8+;
  57. const double esp = 1e-;
  58. const double PI=acos(-1.0);
  59. const double PHI=0.61803399; //黄金分割点
  60. const double tPHI=0.38196601;
  61.  
  62. template<typename T>
  63. inline T read(T&x){
  64. x=;int f=;char ch=getchar();
  65. while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
  66. while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
  67. return x=f?-x:x;
  68. }
  69.  
  70. /*-----------------------showtime----------------------*/
  71. const int maxn = ;
  72. int d[maxn],n;
  73. char str[maxn];
  74. struct node
  75. {
  76. ll u,cur,pos;
  77. node(){}
  78. node(int u,int pos ,int cur):u(u),pos(pos),cur(cur){}
  79. };
  80. queue<node> q;//这是一个大根堆q
  81. int dep[maxn],vis[maxn];
  82. int main(){
  83. int T; scanf("%d", &T);
  84. for(int tt=; tt<=T; tt++){
  85. scanf("%d%s", &n, str);
  86.  
  87. // memset(dep, -1, sizeof(dep));
  88. // memset(vis, -1, sizeof(vis));
  89. int mx = ;
  90. for(int i=; i<n; i++)
  91. d[i] = (int)(str[i] - ''), mx = max(mx, d[i]);
  92. memset(vis,-,sizeof(vis));
  93. memset(dep,-,sizeof(dep));
  94. for(int i=; i<n; i++)
  95. if(d[i] == mx) q.push(node(d[i], i, ));
  96. while(!q.empty()){
  97. node tmp = q.front(); q.pop();
  98. if(tmp.cur == n)break; //第n个确定
  99. if(dep[tmp.cur] > tmp.u)continue;
  100. else if(dep[tmp.cur] == tmp.u && vis[tmp.pos] == tmp.cur)continue; //同一个pos,第二次来
  101. else {
  102. dep[tmp.cur] = tmp.u;
  103. vis[tmp.pos] = tmp.cur;
  104. ll nx = (1ll*tmp.pos * tmp.pos + 1ll)%(1ll*n);
  105. // if(tmp.cur==n-1)break;
  106. dep[tmp.cur+] = max(dep[tmp.cur+] ,d[nx]);
  107. q.push(node(d[nx], nx, tmp.cur+));
  108. }
  109. }
  110. while(!q.empty())q.pop();
  111. printf("Case #%d: ", tt);
  112. for(int i=; i<n; i++) printf("%d", dep[i]);
  113. printf("\n");
  114. }
  115.  
  116. return ;
  117. }

HDU 6223

HDU6223——2017ICPC沈阳G Infinite Fraction Path的更多相关文章

  1. 2017 icpc 沈阳 G - Infinite Fraction Path

    题目大意:有n个点, 每个点有一个数字0 - 9, 第 i 个点只能到 第(i * i + 1)个点,问你在哪个点出发走n次构成的数字串最大. 思路:利用求后缀数组的倍增比较思想, 许多细节需要注意. ...

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

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

  3. ACM-ICPC 2017 沈阳赛区现场赛 G. Infinite Fraction Path && HDU 6223(BFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6223 参考题解:https://blog.csdn.net/qq_40482495/article/d ...

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

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

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

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

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

  7. HDU6223 Infinite Fraction Path bfs+剪枝

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

  8. Infinite Fraction Path HDU 6223 2017沈阳区域赛G题题解

    题意:给你一个字符串s,找到满足条件(s[i]的下一个字符是s[(i*i+1)%n])的最大字典序的长度为n的串. 思路:类似后缀数组,每次倍增来对以i开头的字符串排序,复杂度O(nlogn).代码很 ...

  9. Infinite Fraction Path(HDU6223 + bfs + 剪枝)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6223 题目: 题意: 给你一个长度为n的数字串,开始时你选择一个位置(记为i,下标从0开始)做为起点 ...

随机推荐

  1. .Net Core DevOps -免费用Azure四步实现自动化发布(CI/CD)

    前言 linux 大行其道的今天想必大家都已经拥抱 core 了吧,通常的方案都是 gitlab+jenkins+centos,但是这样的方案不适合我这种懒人,一直在寻求简单的解决方案,在寻求方案的过 ...

  2. poj 1050 To the Max(最大子矩阵之和)

    http://poj.org/problem?id=1050 我们已经知道求最大子段和的dp算法 参考here  也可参考编程之美有关最大子矩阵和部分. 然后将这个扩大到二维就是这道题.顺便说一下,有 ...

  3. codeforces 576 div2 A-D题解

    A题 Description 题目链接: https://codeforces.com/contest/1199/problem/A 题意: 给定长度为n(1≤n≤100000)的一个序列a,以及两个 ...

  4. 【vue】------ 路由创建 ------ 【William】

    路由常用的配置项: path:路由请求的路径 component:路由匹配成功后需要渲染的组件或者页面 tag:改变组件内部渲染的元素 假设组件内部渲染的是a标签 tag="li" ...

  5. Java基础:数组Array转成List的几种方法

    在编写Java程序中,经常要用的一个转换就是数组和List对象之间的互转. 最简单的方法就是遍历 数组,然后将数组元素依次添加进list中. 此方法略,虽然方法很简单,但总感觉这样的方法有点笨 第二种 ...

  6. 【Java例题】7.6文件题3-文本文件统计

    6.文本文件统计.已有一个文本文件文件,请统计数字.大写字母.小写字母.汉字及其它字符出现的次数:然后将这些次数由大到小写到另一个文件之中.说明:将次数为零的过滤掉排序 package chapter ...

  7. leetcode 29 两数相除

    问题描述 给定两个整数,被除数 dividend 和除数 divisor.将两数相除,要求不使用乘法.除法和 mod 运算符. 返回被除数 dividend 除以除数 divisor 得到的商. 示例 ...

  8. Go标准库--net/http学习

    Go中对网络的支持提供了标准库,net包提供了可移植的网络I/O接口,包括TCP/IP.UDP.域名解析和Unix域socket. http包提供了HTTP客户端和服务端的实现. 一般我们用http肯 ...

  9. IDEA运行报错: Maven编译错误:不再支持源选项 5。请使用 6 或更高版本

    这里 记录下 这个问题的解决方案: 1:修改maven settings.xml 中的数据 这里的版本要对应现在使用的jdk版本 2:检查idea 配置 图中2块区域要一致 检查这块地方对应了自己的j ...

  10. shell 提取文件名和目录名

    转自http://blog.csdn.net/universe_hao/article/details/52640321 shell 提取文件名和目录名 在写shell脚本中,经常会有需要对路径和文件 ...