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 单利模式实现/优缺点

    感谢此文章提供摘要: http://www.cnblogs.com/lyanet/archive/2013/01/11/2856468.html 优缺点:http://blog.csdn.net/ta ...

  2. iOS iOS10 的适配问题

    其他:Xcode8 iOS10 的新特性 1.系统判断方法失效:2.隐私数据的访问问题:3.UIColor 问题4.真彩色的显示5.ATS问题6.UIStatusBar问题7.UITextField8 ...

  3. C++各种指针辨析

    1)int *p p与*结合,表明p是一个指针 然后前面int说明p是一个整形的指针 2)int *p[n] 因为[]比*优先级高,所以p先与[]结合,表明p是个数组,然后这个数组在与*结合,说明数组 ...

  4. lesson - 8 Linux文档的压缩和打包

    内容概要:1. gzip工具语法: gzip [-d#] filename 其中#为1-9的数字,默认压缩级别为6 只能压缩文件gzip  filename 生成filename.gz 源文件消失解压 ...

  5. & 与 kill -3

    mysqld_safe --defaults-file=/app/3307/my.cnf 2>&1 1>/dev/null & 将mysqld服务进程放入后台后, 因为忘记 ...

  6. HTML中的超链接

    超链接:也叫URL(Uniform Resource Locator),就是统一资源定位器.一般效果是我们点击网页上某个地方,网页会自动跳转到另外一个地方. 一般链接遵循以下要求:scheme://h ...

  7. Python+selenium+eclipse+pydev自动化测试环境搭建

    一.        安装python 1.下载安装python 可访问python的官方网站:http://www.Python.prg找到下载页面下载需要的版本,可下载python2.x或者pyth ...

  8. Xposed 学习笔记

    Xposed框架用法 1.配置AndroidManifest.xml <meta-data android:name="xposedmodule" android:value ...

  9. vue2.0笔记《二》组件

    主要内容:如何注册组件.如何使用组件.父组件子组件之间值的传递 1.如何注册组件 第一步:通过import将子组件载入父组件的js中 // 第一步:通过import将子组件载入父组件的js中 impo ...

  10. VMware仅主机模式虚拟机无法ping通物理机

    问题描述 在VMware Workstation中新建了一个虚拟机CentOS7,网络适配器选择的是"仅主机模式",结果,物理机ping不通虚拟机,虚拟机也ping不通物理机. 原 ...