hdu6223Infinite Fraction Path(2017ACM/ICPC亚洲区沈阳站G题) 【BFS+剪枝】

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6223

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 (i2i2 + 1)%N. 
A path beginning from the i-th city would pass through the cities u1,u2,u3u1,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[u1u1], D[u2u2], 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

题意:给出一个长度为n的只含数字0~9的串,位置为i的数可以指向位置(i*i+1)%n的数,求一条长度为n的路径串,使得其字典序最大。

思路:取了第一个数之后路径就是固定的了,想要字典序最大,那第一个数字肯定取最大的数。可以想象肯定有样例是最大的数一大堆,那么可以考虑使用BFS搜索寻找最大的,但是会超时,要剪枝。学习了大佬的思路:https://blog.csdn.net/qq_40482495/article/details/78492841 ①遇到节点放的位置比之前的最大值要小的时候,剪枝 ②同一层在相同位置的,剪枝

代码:

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<iostream>
#include<map>
#include<vector>
#include<set>
#include<queue>
using namespace std;
typedef long long ll;
const ll maxv=150010;

ll nxt[maxv],a[maxv],ans[maxv],pre[maxv];
char s[maxv];
ll t,n;

struct num
{
	ll w,id,pos;
	num(ll a=0,ll b=0,ll c=0):w(a),id(b),pos(c){};
};

struct cmp
{
	bool operator()(const num &a,const num &b)
	{
		if(a.pos!=b.pos)
			return a.pos>b.pos;
		if(a.w!=b.w)
			return a.w<b.w;
		return a.id>b.id;
	}
};

priority_queue<num,vector<num>,cmp>q;

int main()
{
	scanf("%lld",&t);
	ll cas=0;
	while(t--)
	{
		scanf("%lld",&n);
		scanf("%s",s);
		fill(pre,pre+maxv,-1);
		fill(ans,ans+maxv,-1);
		ll maxx=-1;
		for(ll i=0;i<n;i++)
		{
			a[i]=ll(s[i]-'0');
			maxx=max(maxx,a[i]);
			nxt[i]=(i*i+1)%n;
		}
		for(int i=0;i<n;i++)
		{
			if(a[i]==maxx)
			{
				num xx;
				xx.w=maxx;
				xx.id=i;
				xx.pos=0;
				q.push(xx);
			}
		}
		while(!q.empty())
		{
			num now=q.top();
			q.pop();
			if(ans[now.pos]==-1)
				ans[now.pos]=now.w;
			if(now.w<ans[now.pos])
				continue;
			if(pre[now.id]<now.pos)
				pre[now.id]=now.pos;
			else
				continue;
			if(now.pos==n-1)
				continue;
			num xx;
			xx.w=a[nxt[now.id]];
			xx.id=nxt[now.id];
			xx.pos=now.pos+1;
			q.push(xx);
		}
		printf("Case #%lld: ",++cas);
		for(int i=0;i<n;i++)
		{
			printf("%lld",ans[i]);
		}
		puts("");
	}

	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值