题目传送门

题目大意:给出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

最新文章

  1. [译]C#编码约定
  2. MySQL数据类型-decimal详解
  3. 精通Web Analytics 2.0 (2) 内容简介
  4. char 数组和 int 之间转化
  5. Bash Promot
  6. zTree实现地市县三级级联DAO接口实现
  7. Wordpress 网站搭建及性能监控方法详解!
  8. linux的常用命令及常用快捷键
  9. 关于TCP的三次握手和四次分手(整理)
  10. 如何不保存Word即可用Word打开想显示的内容!
  11. JAVA多线程---volatile关键字
  12. 使用Elasticsearch的动态索引和索引优化
  13. lr_场景设计之知识点-集合点、loadgenerator
  14. WinRAR从入门到高级的操作技巧集合
  15. Linux服务器---百科mediawiki
  16. 工具:使用过的 API 文档生成工具
  17. iOS - UITextView在调用textViewDidChange方法,九宫格相关中文输入的问题
  18. HTML5 学习之地理定位
  19. [问题]SqlServer创建数据库出错
  20. linux 下 ifcfg-eth0 配置/CentOS_minimal安装和开发环境部署

热门文章

  1. 在页面所有元素加载完成之后执行某个js函数
  2. jquery datatable 多行(单行)选择(select),行获取/行删除
  3. latex学习
  4. 【总结整理】JS的继承
  5. c#基础;初步学习循环语句
  6. wpf textblock超出显示范围后显示tooltip
  7. 杭电ACM刷题(1):1002,A + B Problem II 标签: acmc语言 2017-05-07 15:35 139人阅读 评
  8. 855C Helga Hufflepuff&#39;s Cup
  9. rest-framework组件 之 认证与权限组件
  10. C++笔记--指针数组和结构