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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_39396954/article/details/81777174

传送门: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;
}

猜你喜欢

转载自blog.csdn.net/qq_39396954/article/details/81777174