zoukankan      html  css  js  c++  java
  • 2017 ICPC/ACM 沈阳区域赛HDU6223

    Infinite Fraction Path

    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
    Total Submission(s): 1262    Accepted Submission(s): 224


    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
     
    分析   这道题写不出来 搜到大佬的题解 只有代码分析很少  只能看懂大佬代码  然后照着 敲一遍 不得不服大佬就是大佬写的代码很有感觉
     
    按照 i ——> ( i ^ 2 + 1 ) % n  建图    bfs+剪枝

    bfs + 剪枝 

    剪枝:

    • 值小于当前层最大值的点移出队列
    • 同一层在相同位置的移出队列
     
    AC代码
     1 #include <stdio.h>
     2 #include <math.h>
     3 #include <string.h>
     4 #include <stdlib.h>
     5 #include <iostream>
     6 #include <sstream>
     7 #include <algorithm>
     8 #include <string>
     9 #include <queue>
    10 #include <vector>
    11 using namespace std;
    12 const int maxn= 150000+10;
    13 const int maxm= 1e4+10;
    14 const int inf = 0x3f3f3f3f;
    15 typedef long long ll;
    16 ll ne[maxn];   //ne[i]为以i为起点的边的终点
    17 int v[maxn],ans[maxn],vis[maxn];  //权值、答案、i上一次在ans数组中出现的位置
    18 char s[maxn];
    19 int n,t;
    20 struct node
    21 {
    22     int v,pos,ans;   //表示当前节点的权值,下标i,在答案数组中的位置下标
    23     node(){}
    24     node(int v,int pos,int ans):v(v),pos(pos),ans(ans){}
    25 };
    26 struct compare
    27 {
    28     bool operator()(const node &a,const node &b) const   //ans最小值优先 权值最大值优先 
    29     {
    30         if(a.ans!=b.ans) return a.ans>b.ans;       
    31         else if(a.v!=b.v) return a.v<b.v;
    32         return a.pos>b.pos;
    33     }
    34 };
    35 priority_queue<node,vector<node>,compare> q;
    36 int main()
    37 {
    38     scanf("%d",&t);
    39     int kase=1;
    40     while(t--)
    41     {
    42         scanf("%d",&n);
    43         scanf("%s",s);
    44         memset(vis,-1,sizeof(vis));    //初始化
    45         memset(ans,-1,sizeof(ans));
    46         int ma=0;
    47         for(int i=0;i<n;i++)            //权值转化为整数  求出最大值  i的终点(i^2+1)%n
    48         { 
    49             v[i]=s[i]-'0';
    50             ma=max(ma,v[i]);      
    51             ne[i]=(((ll)i*(ll)i+1)%(ll)n);  
    52         }
    53 //        for(int i=0;i<n;i++)
    54 //        {
    55 //            printf("%d %d %d
    ",i,v[i],next[i]);
    56 //        }
    57 //        printf("%d
    ",ma);
    58         for(int i=0;i<n;i++)
    59         {
    60             if(v[i]==ma)
    61                 q.push(node(ma,i,0));       //最大值先压入队列
    62         }
    63         while(!q.empty())
    64         {
    65             node t=q.top();q.pop();
    66             if(ans[t.ans]==-1) ans[t.ans]=t.v;    //该位置初步确定一个值
    67             if(ans[t.ans]>t.v) continue;          //该节点的权值比以前小  直接跳过
    68             if(vis[t.pos]<t.ans) vis[t.pos]=t.ans;   //更新节点的 访问位置
    69             else continue;
    70             if(t.ans==n-1) continue;
    71             q.push(node(v[ne[t.pos]],ne[t.pos],t.ans+1));  //加入新节点
    72         }
    73         printf("Case #%d: ",kase++);
    74         for(int i=0;i<n;i++)
    75             printf("%d",ans[i]);
    76         printf("
    ");
    77     }
    78     return 0;
    79 }
     
  • 相关阅读:
    多线程-共享全局变量问题
    多线程-共享全局变量
    线程-注意点
    线程
    最短网络Agri-Net
    2455 繁忙的都市
    2597 团伙
    5929 亲戚
    5969 [AK]刻录光盘
    JavaEE Tutorials (6)
  • 原文地址:https://www.cnblogs.com/stranger-/p/7841085.html
Copyright © 2011-2022 走看看