HDU6223——2017ICPC沈阳G Infinite Fraction Path
题意:
给定一个数字串,每个位子都能向(i*i+1)%n的位子转移,输出路径上,字典序最大的,长度为n的串。
参考:https://www.cnblogs.com/mountaink/p/9541442.html
思路:
BFS,
一个数字肯定是最高位越大,这个数字本身就越大,所以肯定第一位要取最大值,在这一位取最大值的时候后面每一位都要尽量最大,所以想到bfs。
但是bfs肯定要剪枝,怎么剪枝呢?
1、按照思路,我要取每一位尽可能大的值,所以某一个状态的某一位小于我当前以及有的解,这个状态肯定被舍弃。
这是最好想的思路,但是如果对于一个全是9的数列,这个剪枝完全没有用,所以必须有其他的剪枝。
2、如果到了从不同起点到达某一个位置,在答案数列中的层次是一样的,舍弃掉。(换句话说,不同起点经过相同步数到达同一座城市,那么后续的状态都是一样的了,所以没必要再走下去),
具体怎么实现呢,一开始将数列中所有最大值所在的位置入队,对于某一种状态,看他的下一步是否比答案中更优或者相等(答案数组初始化为-1),如果更优或者相等则重新入队。 对于新状态,先检查一下当前位置的值是不是和答案数组中当前位置的最大值相等,如果不相等,舍弃,如果相等,判断一下有没有状态在相同步数的情况下已经走到这一步了,有则舍弃,没有则更新一下,然后重复上述操作。
- // #pragma GCC optimize(3)
- // #pragma comment(linker, "/STACK:102400000,102400000") //c++
- // #pragma GCC diagnostic error "-std=c++11"
- // #pragma comment(linker, "/stack:200000000")
- // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
- #include <algorithm>
- #include <iterator>
- #include <iostream>
- #include <cstring>
- #include <cstdlib>
- #include <iomanip>
- #include <bitset>
- #include <cctype>
- #include <cstdio>
- #include <string>
- #include <vector>
- #include <stack>
- #include <cmath>
- #include <queue>
- #include <list>
- #include <map>
- #include <set>
- #include <cassert>
- using namespace std;
- #define lson (l , mid , rt << 1)
- #define rson (mid + 1 , r , rt << 1 | 1)
- #define debug(x) cerr << #x << " = " << x << "\n";
- #define pb push_back
- #define pq priority_queue
- typedef long long ll;
- typedef unsigned long long ull;
- //typedef __int128 bll;
- typedef pair<ll ,ll > pll;
- typedef pair<int ,int > pii;
- typedef pair<int,pii> p3;
- //priority_queue<int> q;//这是一个大根堆q
- //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
- #define fi first
- #define se second
- //#define endl '\n'
- #define OKC ios::sync_with_stdio(false);cin.tie(0)
- #define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
- #define REP(i , j , k) for(int i = j ; i < k ; ++i)
- #define max3(a,b,c) max(max(a,b), c);
- //priority_queue<int ,vector<int>, greater<int> >que;
- const ll mos = 0x7FFFFFFF; //
- const ll nmos = 0x80000000; //-2147483648
- const int inf = 0x7f7f7f7f;
- const ll inff = 0x3f3f3f3f3f3f3f3f; //
- const int mod = 1e8+;
- const double esp = 1e-;
- const double PI=acos(-1.0);
- const double PHI=0.61803399; //黄金分割点
- const double tPHI=0.38196601;
- template<typename T>
- inline T read(T&x){
- x=;int f=;char ch=getchar();
- while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
- while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
- return x=f?-x:x;
- }
- /*-----------------------showtime----------------------*/
- const int maxn = ;
- int d[maxn],n;
- char str[maxn];
- struct node
- {
- ll u,cur,pos;
- node(){}
- node(int u,int pos ,int cur):u(u),pos(pos),cur(cur){}
- };
- queue<node> q;//这是一个大根堆q
- int dep[maxn],vis[maxn];
- int main(){
- int T; scanf("%d", &T);
- for(int tt=; tt<=T; tt++){
- scanf("%d%s", &n, str);
- // memset(dep, -1, sizeof(dep));
- // memset(vis, -1, sizeof(vis));
- int mx = ;
- for(int i=; i<n; i++)
- d[i] = (int)(str[i] - ''), mx = max(mx, d[i]);
- memset(vis,-,sizeof(vis));
- memset(dep,-,sizeof(dep));
- for(int i=; i<n; i++)
- if(d[i] == mx) q.push(node(d[i], i, ));
- while(!q.empty()){
- node tmp = q.front(); q.pop();
- if(tmp.cur == n)break; //第n个确定
- if(dep[tmp.cur] > tmp.u)continue;
- else if(dep[tmp.cur] == tmp.u && vis[tmp.pos] == tmp.cur)continue; //同一个pos,第二次来
- else {
- dep[tmp.cur] = tmp.u;
- vis[tmp.pos] = tmp.cur;
- ll nx = (1ll*tmp.pos * tmp.pos + 1ll)%(1ll*n);
- // if(tmp.cur==n-1)break;
- dep[tmp.cur+] = max(dep[tmp.cur+] ,d[nx]);
- q.push(node(d[nx], nx, tmp.cur+));
- }
- }
- while(!q.empty())q.pop();
- printf("Case #%d: ", tt);
- for(int i=; i<n; i++) printf("%d", dep[i]);
- printf("\n");
- }
- return ;
- }
HDU 6223
HDU6223——2017ICPC沈阳G Infinite Fraction Path的更多相关文章
- 2017 icpc 沈阳 G - Infinite Fraction Path
题目大意:有n个点, 每个点有一个数字0 - 9, 第 i 个点只能到 第(i * i + 1)个点,问你在哪个点出发走n次构成的数字串最大. 思路:利用求后缀数组的倍增比较思想, 许多细节需要注意. ...
- HDU6223 && 2017沈阳ICPC: G. Infinite Fraction Path——特殊图&&暴力
题意 给定一个数字串,每个位子都能向(i*i+1)%n的位子转移,输出在路径上.字典序最大的.长度为n的串($n \leq 150000$). 分析 先考虑一个暴力的方法,考虑暴力每个x,然后O(n) ...
- 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 ...
- 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 ...
- 2017沈阳区域赛Infinite Fraction Path(BFS + 剪枝)
Infinite Fraction Path Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java ...
- HDU6223 Infinite Fraction Path bfs+剪枝
Infinite Fraction Path 这个题第一次看见的时候,题意没搞懂就没做,这第二次也不会呀.. 题意:第i个城市到第(i*i+1)%n个城市,每个城市有个权值,从一个城市出发走N个城市, ...
- Infinite Fraction Path HDU 6223 2017沈阳区域赛G题题解
题意:给你一个字符串s,找到满足条件(s[i]的下一个字符是s[(i*i+1)%n])的最大字典序的长度为n的串. 思路:类似后缀数组,每次倍增来对以i开头的字符串排序,复杂度O(nlogn).代码很 ...
- Infinite Fraction Path(HDU6223 + bfs + 剪枝)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6223 题目: 题意: 给你一个长度为n的数字串,开始时你选择一个位置(记为i,下标从0开始)做为起点 ...
随机推荐
- .Net Core DevOps -免费用Azure四步实现自动化发布(CI/CD)
前言 linux 大行其道的今天想必大家都已经拥抱 core 了吧,通常的方案都是 gitlab+jenkins+centos,但是这样的方案不适合我这种懒人,一直在寻求简单的解决方案,在寻求方案的过 ...
- poj 1050 To the Max(最大子矩阵之和)
http://poj.org/problem?id=1050 我们已经知道求最大子段和的dp算法 参考here 也可参考编程之美有关最大子矩阵和部分. 然后将这个扩大到二维就是这道题.顺便说一下,有 ...
- codeforces 576 div2 A-D题解
A题 Description 题目链接: https://codeforces.com/contest/1199/problem/A 题意: 给定长度为n(1≤n≤100000)的一个序列a,以及两个 ...
- 【vue】------ 路由创建 ------ 【William】
路由常用的配置项: path:路由请求的路径 component:路由匹配成功后需要渲染的组件或者页面 tag:改变组件内部渲染的元素 假设组件内部渲染的是a标签 tag="li" ...
- Java基础:数组Array转成List的几种方法
在编写Java程序中,经常要用的一个转换就是数组和List对象之间的互转. 最简单的方法就是遍历 数组,然后将数组元素依次添加进list中. 此方法略,虽然方法很简单,但总感觉这样的方法有点笨 第二种 ...
- 【Java例题】7.6文件题3-文本文件统计
6.文本文件统计.已有一个文本文件文件,请统计数字.大写字母.小写字母.汉字及其它字符出现的次数:然后将这些次数由大到小写到另一个文件之中.说明:将次数为零的过滤掉排序 package chapter ...
- leetcode 29 两数相除
问题描述 给定两个整数,被除数 dividend 和除数 divisor.将两数相除,要求不使用乘法.除法和 mod 运算符. 返回被除数 dividend 除以除数 divisor 得到的商. 示例 ...
- Go标准库--net/http学习
Go中对网络的支持提供了标准库,net包提供了可移植的网络I/O接口,包括TCP/IP.UDP.域名解析和Unix域socket. http包提供了HTTP客户端和服务端的实现. 一般我们用http肯 ...
- IDEA运行报错: Maven编译错误:不再支持源选项 5。请使用 6 或更高版本
这里 记录下 这个问题的解决方案: 1:修改maven settings.xml 中的数据 这里的版本要对应现在使用的jdk版本 2:检查idea 配置 图中2块区域要一致 检查这块地方对应了自己的j ...
- shell 提取文件名和目录名
转自http://blog.csdn.net/universe_hao/article/details/52640321 shell 提取文件名和目录名 在写shell脚本中,经常会有需要对路径和文件 ...