Infinite Fraction Path

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

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
 
分析   这道题写不出来 搜到大佬的题解 只有代码分析很少  只能看懂大佬代码  然后照着 敲一遍 不得不服大佬就是大佬写的代码很有感觉
 
按照 i ——> ( i ^ 2 + 1 ) % n  建图    bfs+剪枝

bfs + 剪枝

剪枝:

  • 值小于当前层最大值的点移出队列
  • 同一层在相同位置的移出队列
 
AC代码
 #include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <queue>
#include <vector>
using namespace std;
const int maxn= +;
const int maxm= 1e4+;
const int inf = 0x3f3f3f3f;
typedef long long ll;
ll ne[maxn]; //ne[i]为以i为起点的边的终点
int v[maxn],ans[maxn],vis[maxn]; //权值、答案、i上一次在ans数组中出现的位置
char s[maxn];
int n,t;
struct node
{
int v,pos,ans; //表示当前节点的权值,下标i,在答案数组中的位置下标
node(){}
node(int v,int pos,int ans):v(v),pos(pos),ans(ans){}
};
struct compare
{
bool operator()(const node &a,const node &b) const //ans最小值优先 权值最大值优先
{
if(a.ans!=b.ans) return a.ans>b.ans;
else if(a.v!=b.v) return a.v<b.v;
return a.pos>b.pos;
}
};
priority_queue<node,vector<node>,compare> q;
int main()
{
scanf("%d",&t);
int kase=;
while(t--)
{
scanf("%d",&n);
scanf("%s",s);
memset(vis,-,sizeof(vis)); //初始化
memset(ans,-,sizeof(ans));
int ma=;
for(int i=;i<n;i++) //权值转化为整数 求出最大值 i的终点(i^2+1)%n
{
v[i]=s[i]-'';
ma=max(ma,v[i]);
ne[i]=(((ll)i*(ll)i+)%(ll)n);
}
// for(int i=0;i<n;i++)
// {
// printf("%d %d %d\n",i,v[i],next[i]);
// }
// printf("%d\n",ma);
for(int i=;i<n;i++)
{
if(v[i]==ma)
q.push(node(ma,i,)); //最大值先压入队列
}
while(!q.empty())
{
node t=q.top();q.pop();
if(ans[t.ans]==-) ans[t.ans]=t.v; //该位置初步确定一个值
if(ans[t.ans]>t.v) continue; //该节点的权值比以前小 直接跳过
if(vis[t.pos]<t.ans) vis[t.pos]=t.ans; //更新节点的 访问位置
else continue;
if(t.ans==n-) continue;
q.push(node(v[ne[t.pos]],ne[t.pos],t.ans+)); //加入新节点
}
printf("Case #%d: ",kase++);
for(int i=;i<n;i++)
printf("%d",ans[i]);
printf("\n");
}
return ;
}

2017 ICPC/ACM 沈阳区域赛HDU6223的更多相关文章

  1. 2017 ICPC/ACM 沈阳区域赛HDU6228

    Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Subm ...

  2. 2015年ACM长春区域赛比赛感悟

    距离长春区域赛结束已经4天了,是时候整理一下这次比赛的点点滴滴了. 也是在比赛前一周才得到通知要我参加长春区域赛,当时也是既兴奋又感到有很大的压力,毕竟我的第一场比赛就是区域赛水平,还是很有挑战性的. ...

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

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

  4. 2015沈阳区域赛Meeting(最短路 + 建图)

    Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  5. ICPC 2018 焦作区域赛

    // 2019.10.7 练习赛 // 赛题来源:2018 ICPC 焦作区域赛 // CF链接:http://codeforces.com/gym/102028 A Xu Xiake in Hena ...

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

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

  7. 2017 ACM-ICPC 沈阳区域赛记录

    出发日 中午坐大巴前往萧山机场. 哇开心又可以坐飞机了 飞机延误了.在候机大厅里十分无聊,先用机场的电脑玩了会小游戏 然后偷偷切了2个水题 (什么编译器IDE都没有,只能记事本了) 飞机上什么东西都没 ...

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

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

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

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

随机推荐

  1. iOS开发之Xcode常用调试技巧总结

    转载自:iOS开发之Xcode常用调试技巧总结 最近在面试,面试过程中问到了一些Xcode常用的调试技巧问题.平常开发过程中用的还挺顺手的,但你要突然让我说,确实一脸懵逼.Debug的技巧很多,比如最 ...

  2. Kafka+Storm写入Hbase和HDFS

    1.Storm整合Kafka 使用Kafka作为数据源,起到缓冲的作用 // 配置Kafka订阅的Topic,以及zookeeper中数据节点目录和名字 String zks = KafkaPrope ...

  3. bzoj 2756: [SCOI2012]奇怪的游戏

    Description Blinker最近喜欢上一个奇怪的游戏. 这个游戏在一个 N*M 的棋盘上玩,每个格子有一个数.每次 Blinker 会选择两个相邻 的格子,并使这两个数都加上 1. 现在 B ...

  4. solr安装配置

    1.solr是基于tomcat安装部署的 2.网上下载solr-5.2.1 http://lucene.apache.org/solr/downloads.html 3.解压solr文件 tar zx ...

  5. Chrome不支持本地Ajax请求,解决办法

    Chrome不支持本地Ajax请求,当我在.html文件中访问.json文件时就会出现这个问题,就是说这个时候不能加载这个.html文件. 解决方式 打开Chrome快捷方式的属性中设置: 右击Chr ...

  6. 微信小程序开发之picker

    一.绑定简单数组 通过bindChange控制index,使得当前选择值发生改变 示例1 data: { Data: ['A','B'], Index: 0, }, <picker class= ...

  7. vue2 vue-router 组装

    适用于vue cli搭建的项目 vue-router模块下载及记录到package.json中: npm i vue-router -D main.js中: import VueRouter from ...

  8. Python函数返回不定数量的值

    Python的函数是可以return多个值的,但其本质上还是返回单个值,只是利用了tuple的自动打包,将多个值打包成单个tuple返回. 使用代码验证: def func_a(): return 1 ...

  9. js 闭包的用法详解

    一.闭包 实现可重用的局部变量,且保护其不受污染的机制. 外层函数包裹受保护的变量和内层函数. 内层函数专门负责操作外层函数的局部变量. 将内层函数返回到外层函数外部,反复调用. 二.作用域 子函数会 ...

  10. Android查缺补漏(View篇)--自定义View利器Canvas和Paint详解

    上篇文章介绍了自定义View的创建流程,从宏观上给出了一个自定义View的创建步骤,本篇是上一篇文章的延续,介绍了自定义View中两个必不可少的工具Canvas和Paint,从细节上更进一步的讲解自定 ...