1. 程式人生 > >hdu6223Infinite Fraction Path(2017ACM/ICPC亞洲區瀋陽站G題) 【BFS+剪枝】

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;
}