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控制器跳转动画

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 MyViewController *myVC = [[MyViewController alloc]init];  //创建动画  C ...

  2. Java用代码获取classes目录下的文件的方式

    假设我们有个Maven的Web项目,其中有src/main/java和src/main/resources两个输出目录(注意这两个目录下的内容运行后都会输出到classes目录下), 而在src/ma ...

  3. [array] leetcode - 39. Combination Sum - Medium

    leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...

  4. selenium 封装

    周末无聊 在家封装一个pyselenium.可能这些封装大家都会使用,但是我还是根据我自己的习惯去选择性的去封装一些在我工作中用的,这样的话,我就不用去看selenium的api的,我可以根据我自己的 ...

  5. Java版本APP接口安全设计

    Java版本APP接口安全设计 安全设计分为两种: 1.传输安全. 2. 会话安全. 1.传输安全 怎么保证接口经过网络传输不被抓包获取? 1.如果只是使用对称性算法,破解APP拿到加密密钥就可以解密 ...

  6. Webpack 2 视频教程 009 - 配置 ESLint 实现代码规范自动测试 (上)

    原文发表于我的技术博客 这是我免费发布的高质量超清「Webpack 2 视频教程」. Webpack 作为目前前端开发必备的框架,Webpack 发布了 2.0 版本,此视频就是基于 2.0 的版本讲 ...

  7. angularjs 字段排序 多字段排序

    我们用angularjs {{}},ng-model循环绑定数组或对象的内容的时候,有时候会用到排序,有时候可能会有多个字段排序 具体要用到过滤 数据的展现,可以通过ng-repeat实现.当网页解析 ...

  8. ADO.NET查询和操作数据库

    stringbuilder 类 stringbuilder类:用来定义可变字符串 stringbulider Append(string value)   在结尾追加 stringbuilder in ...

  9. 执行PHP脚本时遇到 mysql_connect(): Headers and client library minor version mismatch的解决方法

    把服务器从Windows迁移到了centos7.2,配置好PHP运行环境后,项目运行正常. 但在命令行中运行一个PHP脚本时,遇到了标题中显示的错误 使用 php -i | grep Client 得 ...

  10. Sticky Footer 绝对底部的两种套路

    最近面了好几个前端,工作经验有高有低,居然都不知道绝对底部是什么,也没有人能说出一种实现方式,让我不禁感慨前端领域的良莠不齐 绝对底部,或者说 Sticky Footer,是一种古老且经典的页面效果: ...