HDU 6223 Infinite Fraction Path

来源:互联网 发布:axure7.0mac中文版下载 编辑:程序博客网 时间:2024/06/05 21:16

链接

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

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

4
3
149
5
12345
7
3214567
9
261025520

Sample Output

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

思路

bfs + 剪枝
复杂度为:O(n * L),L为环的大小

剪枝:

  • 值小于当前层最大值的点移出队列
  • 同一层在相同位置的移出队列
#include <bits/stdc++.h>using namespace std;const int MAXN = 150000 + 50;int a[MAXN], D[MAXN], cur[MAXN], vis[MAXN];char s[MAXN];struct Node{    int v, pos, cur;    Node() {}    Node(int v, int pos, int cur) :v(v), pos(pos), cur(cur) {}};struct compare{    bool operator()(const Node &a, const Node &b) const    {        if (a.cur != b.cur) return a.cur > b.cur;        else if (a.v != b.v) return a.v < b.v;        return a.pos > b.pos;    }};priority_queue<Node, vector<Node>, compare> pq;int main(){    int t, cas = 1;    scanf("%d", &t);    while (t--)    {        int n;        scanf("%d", &n);        scanf("%s", s);        printf("Case #%d: ", cas++);        int maxx = 0;        for (int i = 0; i < n; ++i)        {            cur[i] = -1;            vis[i] = -1;            a[i] = s[i] - '0';            maxx = max(maxx, a[i]);        }        for (int i = 0; i < n; ++i)        {            D[i] = int(((long long)i * (long long)i + 1) % (long long)n);        }        for (int i = 0; i < n; ++i)        {            if (a[i] == maxx) pq.push(Node(maxx, i, 0));        }        while (!pq.empty())        {            Node top = pq.top();            pq.pop();            if (cur[top.cur] == -1) cur[top.cur] = top.v;            if (cur[top.cur] > top.v) continue;            if (vis[top.pos] < top.cur) vis[top.pos] = top.cur;            else continue;            if (top.cur == n - 1) continue;            pq.push(Node(a[D[top.pos]], D[top.pos], top.cur + 1));        }        for (int i = 0; i < n; ++i)        {            printf("%d", cur[i]);        }        printf("\n");    }    return 0;}
原创粉丝点击