HDU

来源:互联网 发布:网易彩票源码 编辑:程序博客网 时间:2024/06/05 14:58

Infinite Fraction Path



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
43149512345732145679261025520
 

Sample Output
Case #1: 999Case #2: 53123Case #3: 7166666Case #4: 615015015
 



题意:其实很简单,直接看样例就能懂了……题意说了一大堆没用的……


解题思路:瞬间想到宽搜,但是疯狂超时。两个剪枝都想到了,实在不知道哪里错了。后来百度了一下memset的复杂度,应该就是这里出问题了,memset复杂度其实跟O(N)没什么区别,这里用两个数组优化初始化,就过了。这里的队列要用优先队列,直接队列会超时。两个剪枝分别是

1.当当前位比最大值小,剪掉。

2.当当前位的下一位相同,保留一个即可。(因为(i*i+1)%N有很多种情况会使得相同)




#include<iostream>#include<deque>#include<memory.h>#include<stdio.h>#include<map>#include<string.h>#include<algorithm>#include<vector>#include<math.h>#include<stack>#include<queue>#include<set>using namespace std;typedef long long int ll;char str[150005];//原始数组char mm[150005];//答案数组bool vis[150005];//当前位是否被访问过ll ma[150005];//记录出现的位,这样可以不用memsetint top;//队列实现ll N;struct point{    ll i;    int step;    point(ll a=0,int b=0){        i=a;        step=b;    }    friend bool operator <(point a,point b){        if(a.step==b.step)            return str[a.i]<str[b.i];//数值大的i优先        return a.step>b.step;//step小的优先    }};priority_queue<point> que;void bfs(){    int last=-1;    while(!que.empty()){        point tp=que.top();        que.pop();        //每一层都要清空下访问标记,用memset超时……        if(tp.step!=last){            last=tp.step;            while(top){                vis[ma[--top]]=0;            }        }        //三个剪枝        if(tp.step>=N)            continue;        if(mm[tp.step]>str[tp.i])            continue;        if(vis[tp.i])            continue;        vis[tp.i]=1;        ma[top++]=tp.i;        mm[tp.step]=str[tp.i];        que.push(point((tp.i*tp.i+1)%N,tp.step+1));            }}int main(){    int t;    scanf("%d",&t);    for(int qqq=1;qqq<=t;qqq++){        scanf("%lld",&N);        scanf("%s",str);        char ma=0;        for(ll i=0;i<N;i++)            ma=max(ma,str[i]);        for(ll i=0;i<N;i++)            if(ma==str[i])                que.push(point(i,0));        bfs();        printf("Case #%d: ",qqq);        for(ll i=0;i<N;i++)            putchar(mm[i]);        puts("");        for(ll i=0;i<N;i++)            mm[i]=0;    }    return 0;}




原创粉丝点击