Infinite Fraction Path(HDU6223 + bfs + 剪枝)
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=6223
题目:
题意:
给你一个长度为n的数字串,开始时你选择一个位置(记为i,下标从0开始)做为起点,那么下一步将在(i × i + 1)%n处,将字典序最大的路径上的数打印出来。
思路:
要想字典序最大,那么只需每一步都是最大的即可。由题意可知,当起点确定时,所对应的数也就确定了。对于每一步,我们只需当前为最优即可,若第i步有t种方式使得当前数为x,那么下一步也将会有t种选择,那么我们可以用优先队列维护下一步的最优值(具体看代码。这题不加剪枝会T,由于操作中有取膜操作,那么对于同一步,取膜后的下一个位置极有可能会相同,也就是同一个位置重复入队列,这样后面的步骤都会重复,这样复杂度将会增大数倍,此时我们可以用一个set去重,防止同一步重复入队列。
代码实现如下:
#include <set>
#include <map>
#include <deque>
#include <ctime>
#include <stack>
#include <cmath>
#include <queue>
#include <string>
#include <cstdio>
#include <vector>
#include <iomanip>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pll;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef unsigned long long uLL; #define lson rt<<1
#define rson rt<<1|1
#define name2str(name)(#name)
#define bug printf("**********\n");
#define IO ios::sync_with_stdio(false);
#define debug(x) cout<<#x<<"=["<<x<<"]"<<endl;
#define FIN freopen("/home/dillonh/code/OJ/in.txt","r",stdin); const double eps = 1e-;
const int mod = 1e9 + ;
const int maxn = + ;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const LL INF = 0x3f3f3f3f3f3f3f3fLL; int T, n;
int t[maxn];
char s[maxn]; struct node {
int id, val, step;
bool operator < (const node& x) const {
return step == x.step ? val < x.val : step > x.step;
}
}nw, nxt; set<int> stc;
priority_queue<node> q; void bfs() {
stc.clear();
while(!q.empty()) q.pop();
int mx = -;
for(int i = ; i < n; i++) {
if(s[i] - '' > mx) mx = max(mx, s[i] - '');
}
for(int i = ; i < n; i++) { //将可能的起点压入队列中
if(s[i] - '' == mx) {
nw.id = i;
nw.val = mx;
nw.step = ;
q.push(nw);
}
}
int pp = ;
while(!q.empty()) {
nw = q.top(); q.pop();
if(nw.step >= n) return; //满足条件即可返回
if(nw.step == pp + ) {
stc.clear(); //到了新的一步需将set清空
mx = nw.val;
pp = nw.step;
}
if(nw.val == mx) {
t[nw.step] = nw.val;
nxt.id = (1LL * nw.id * nw.id + ) % n;
if(stc.count(nxt.id)) continue; //这个位置已经被压入过队列就不需要重复压入了
stc.insert(nxt.id);
nxt.val = s[nxt.id] - '';
nxt.step = nw.step + ;
q.push(nxt);
}
}
} int main() {
#ifndef ONLINE_JUDGE
FIN;
#endif
scanf("%d", &T);
for(int icase = ; icase <= T; icase++) {
scanf("%d", &n);
scanf("%s", s);
bfs();
printf("Case #%d: ", icase);
for(int i = ; i < n; i++) {
printf("%d", t[i]);
}
printf("\n");
#ifndef ONLINE_JUDGE
cout <<"It costs " <<clock() <<"ms\n";
#endif
}
return ;
}
Infinite Fraction Path(HDU6223 + bfs + 剪枝)的更多相关文章
- hdu6223 Infinite Fraction Path 2017沈阳区域赛G题 bfs加剪枝(好题)
题目传送门 题目大意:给出n座城市,每个城市都有一个0到9的val,城市的编号是从0到n-1,从i位置出发,只能走到(i*i+1)%n这个位置,从任意起点开始,每走一步都会得到一个数字,走n-1步,会 ...
- HDU6223 Infinite Fraction Path bfs+剪枝
Infinite Fraction Path 这个题第一次看见的时候,题意没搞懂就没做,这第二次也不会呀.. 题意:第i个城市到第(i*i+1)%n个城市,每个城市有个权值,从一个城市出发走N个城市, ...
- 2017沈阳区域赛Infinite Fraction Path(BFS + 剪枝)
Infinite Fraction Path Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java ...
- 2017 ACM/ICPC 沈阳 G题 Infinite Fraction Path
The ant Welly now dedicates himself to urban infrastructure. He came to the kingdom of numbers and s ...
- 【赛后补题】(HDU6223) Infinite Fraction Path {2017-ACM/ICPC Shenyang Onsite}
场上第二条卡我队的题目. 题意与分析 按照题意能够生成一个有环的n个点图(每个点有个位数的权值).图上路过n个点显然能够生成一个n位数的序列.求一个最大序列. 这条题目显然是搜索,但是我队在场上(我负 ...
- [HDU6223]Infinite Fraction Path
题目大意: 有$n(n\leq 150,000)$个编号为$0_n-1$格子,每个格子有一个权值$w_i(0\leq w_i\leq 9)$.从任意一个点出发,按照一定的规则进行跳转.设当前的格子为$ ...
- hdu 6223 Infinite Fraction Path
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=6223 题意:给定长度为n的一串数字S,现在要按照一种规则寻找长度为n的数字串,使得该数字串的字典序最大 ...
- HDU6223——2017ICPC沈阳G Infinite Fraction Path
题意: 给定一个数字串,每个位子都能向(i*i+1)%n的位子转移,输出路径上,字典序最大的,长度为n的串. 参考:https://www.cnblogs.com/mountaink/p/954144 ...
- HDU6223 && 2017沈阳ICPC: G. Infinite Fraction Path——特殊图&&暴力
题意 给定一个数字串,每个位子都能向(i*i+1)%n的位子转移,输出在路径上.字典序最大的.长度为n的串($n \leq 150000$). 分析 先考虑一个暴力的方法,考虑暴力每个x,然后O(n) ...
随机推荐
- 12:打印1到最大的n位数
面试题12:打印1到最大的n位数 剑指offer题目12,题目如下 输入数字n,按顺序打印出1到最大的n位十进制数,比如输入3,则打印出1,2,3一直到最大的三位数999 方法一 和面试题11< ...
- JavaScript的setTimeout()和setInterval()
1. setTimeout()方法 作用:在制定的毫秒数后调用函数或计算表达式 语法: setTimeout(code,millisec) 实例: function timedMsg() { var ...
- jQuery文件上传插件uploadify
官方网站:http://www.uploadify.com/ 参考博客:jQuery Uploadify在ASP.NET MVC3中的使用 参考博客:使用uploadify上传图片时返回“Cannot ...
- 多线程同步与并发访问共享资源工具—Lock、Monitor、Mutex、Semaphore
“线程同步”的含义 当一个进程启动了多个线程时,如果需要控制这些线程的推进顺序(比如A线程必须等待B和C线程执行完毕之后才能继续执行),则称这些线程需要进行“线程同步(thread synchro ...
- Getting logback and slf4j to work in JBoss AS 7
As usual, it has to do with classloading and that JBoss internally also uses slf4j and logback. As e ...
- c++11 继承构造
c++11 继承构造 #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> #includ ...
- 【刷题】BZOJ 1977 [BeiJing2010组队]次小生成树 Tree
Description 小 C 最近学了很多最小生成树的算法,Prim 算法.Kurskal 算法.消圈算法等等. 正当小 C 洋洋得意之时,小 P 又来泼小 C 冷水了.小 P 说,让小 C 求出一 ...
- 毕业设计预习:SM3密码杂凑算法基础学习
SM3密码杂凑算法基础学习 术语与定义 1 比特串bit string 由0和1组成的二进制数字序列. 2 大端big-endian 数据在内存中的一种表示格式,规定左边为高有效位,右边为低有效位.数 ...
- 压测工具-Jmeter
server压力测试首选: Apache JMeter是Apache组织开发的基于Java的压力测试工具.用于对软件做压力测试,它最初被设计用于Web应用测试,但后来扩展到其他测试领域. 1 JMet ...
- 如何在低速率网络中测试 Web 应用
大家看到标题后的第一个问题可能是:“我们需要这样做吗?” 如果我们开发的是局域网 Web 应用的话,可能没有必要这样做.但如果我们的 Web 应用面向的是互联网上的成千上万的用户,这样做就很必要了.因 ...