题目传送门

题目大意:给出n座城市,每个城市都有一个0到9的val,城市的编号是从0到n-1,从i位置出发,只能走到(i*i+1)%n这个位置,从任意起点开始,每走一步都会得到一个数字,走n-1步,会得到一个长度为n的数列,输出能得到的最大的数列(当成数字)。

思路:

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

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

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

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

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

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

 //hdu6223  249ms
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<string.h>
#include<sstream>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<bitset>
#define CLR(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
inline int rd() {
int f=;
int x=;
char s=getchar();
while(s<''||s>'') {
if(s=='-')f=-;
s=getchar();
}
while(s>=''&&s<='') {
x=x*+s-'';
s=getchar();
}
x*=f;
return x;
}
const int inf=0x3f3f3f3f;
const int maxn=;
int maxx,vis[maxn],tot;
char a[maxn],ans[maxn];
ll n;
struct dian {
int step;//步数
ll pos;//在原数组中对应的位置
dian() {}
dian(int step,ll pos):step(step),pos(pos) {}
};
queue<dian >q;
inline void bfs() {
dian s;
while(!q.empty()) {
s=q.front();
q.pop();
if(s.step==n) {//终止条件
continue;
}
if(a[s.pos]==ans[s.step]) {//如果相等,则代表目前的状态是最优的
if(vis[s.pos]==s.step)continue;//之前已经来到过这个状态
vis[s.pos]=s.step;
s.pos= (s.pos * s.pos + ) % n;
s.step++;
if(a[s.pos]>=ans[s.step]) {
ans[s.step]=a[s.pos];
q.push(s);
}
}
}
}
int main() {
int T;
int cas=;
cin>>T;
while(T--) {
while(!q.empty())q.pop();
tot=;
scanf("%lld",&n);
maxx=;
scanf("%s",a);
for(int i=; i<n; i++) {
maxx=max(maxx,(int)a[i]);
}
for(int i=; i<n; i++) {
if(a[i]==maxx) {
q.push(dian(,i));//最大值入队
}
}
CLR(ans,-);
CLR(vis,-);
ans[]=maxx;//第一个位置先更新
bfs();//主要过程
printf("Case #%d: ",cas++);
ans[n+]='\0';
printf("%s",ans+);
printf("\n");
}
}

写了很久,做了很多细节优化

Infinite Fraction Path

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 2856    Accepted Submission(s): 576

Problem Description
The ant Welly now dedicates himself to urban infrastructure. He came to the kingdom of numbers and solicited an audience with the king. He recounted how he had built a happy path in the kingdom of happiness. The king affirmed Welly’s talent and hoped that this talent can help him find the best infinite fraction path before the anniversary.
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
 
Input
The input contains multiple test cases and the first line provides an integer up to 100 indicating to the total numberof test cases.
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.
 
Output
For each test case, you should output the label of the case first. Then you are to output exactly N characters which are the first N digits of the fractional part of the largest relevant fraction.
 
Sample Input
4
3
149
5
12345
7
3214567
9
261025520
 
Sample Output
Case #1: 999
Case #2: 53123
Case #3: 7166666
Case #4: 615015015

hdu6223 Infinite Fraction Path 2017沈阳区域赛G题 bfs加剪枝(好题)的更多相关文章

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

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

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

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

  3. HDU6223 Infinite Fraction Path bfs+剪枝

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

  4. hdu6229 Wandering Robots 2017沈阳区域赛M题 思维加map

    题目传送门 题目大意: 给出一张n*n的图,机器人在一秒钟内任一格子上都可以有五种操作,上下左右或者停顿,(不能出边界,不能碰到障碍物).题目给出k个障碍物,但保证没有障碍物的地方是强联通的,问经过无 ...

  5. HDU 6229 Wandering Robots(2017 沈阳区域赛 M题,结论)

    题目链接  HDU 6229 题意 在一个$N * N$的格子矩阵里,有一个机器人. 格子按照行和列标号,左上角的坐标为$(0, 0)$,右下角的坐标为$(N - 1, N - 1)$ 有一个机器人, ...

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

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

  7. [HDU6223]Infinite Fraction Path

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

  8. Heron and His Triangle 2017 沈阳区域赛

    A triangle is a Heron’s triangle if it satisfies that the side lengths of it are consecutive integer ...

  9. 2017 ICPC/ACM 沈阳区域赛HDU6223

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

随机推荐

  1. import json

  2. css知多少(6)——选择器的优先级(转)

    css知多少(6)——选择器的优先级   1. 引言 上一节<css知多少(5)——选择器>最后提到,选择器类型过多将导致一些问题,是什么问题呢?咱们直接举例子说明. 上图中,css中的两 ...

  3. Java界面设计

    ---------------siwuxie095                             Java SE(Java Standard Edition) 即 Java 标准版, 一般也 ...

  4. 100723H Obfuscation

    传送门 题目大意 给你一个包含n 个单词的字典,给你一篇文章,文章包括若干词典里的单词,把句子里的空格都去掉,单词的首位字母都不变,中间的字符集为乱序,问能否恢复这篇文章,使得单词都是词典里的单词,如 ...

  5. 树莓派研究笔记(8)-- 编译lakka v2.1源码

    Lakka越玩越觉得强大.如果要是能自己修改一下代码,实现自定义的一些操作就更好了.还可以修复字体文件,修复在部分机器上自动更换Mac地址导致ip变化的问题. 所以我们来尝试编译这个系统.这篇文章几乎 ...

  6. android listview addHeaderView和addFooterView的注意事项

    1. item内如果有button等控件时,在监听listview的onitemclick事件时,焦点会被item内的button. imagebutton等控件抢走,从而导致在listview设置了 ...

  7. Java之封装特性

    Java中的三大特性:继承,封装,多态: 其中封装概念:封装是把过程和数据包围起来,对数据的访问只能通过已定义的接口. 面向对象计算始于这个基本概念,即现实世界可以被描绘成一系列完全自治.封装的 对象 ...

  8. com.fasterxml.jackson.databind.JavaType.isReferenceType

    <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-map ...

  9. delphi xe6 android ListView增加 Header或Footer 的方法

    var  Item1: TListViewItem;begin    Item1 := ListView1.Items.Add;    Item1.Purpose:=TListItemPurpose. ...

  10. 责任型模式一:Observer(观察者)模式

    目的: Observer模式的宗旨是在多个对象之间定义一对多的关系,以便当一个对象状态改变时,其他所有依赖于这个对象的对象都能得到通知,并被自动更新.常用于业务逻辑层与表现层的分离. 需求:由GUI引 ...