zoukankan      html  css  js  c++  java
  • 团体程序设计天梯赛PTA L2-020功夫传人

     

     分析:题意不难理解,看上去就是不太难的题,然后WA了一天qaq

    用bfs和并查集应该都能做,然后我没压缩好,一会超时一会超内存。最后用模拟stack存储辈分数降低了递归的时间复杂度。

     1 #include <iostream>
     2 #include<cstdio>
     3 #include<vector>
     4 #include<map>
     5 #include<stack>
     6 using namespace std;
     7 const int N = 1e5+10;
     8 
     9 vector<int>ans;
    10 map<int,int>mp;
    11 int cnt=0;
    12 int fa[N];
    13 int gen[N];
    14 stack<int>q;
    15 void fstack(int n)
    16 {
    17     while(!q.empty())
    18         q.pop();
    19     for(int i = 1; i < n; i ++)
    20     {
    21         if(fa[i]==0)
    22             gen[i]=1;
    23         else
    24         {
    25             q.push(i);
    26             while(gen[fa[q.top()]]!=0)
    27             {
    28                 gen[q.top()] = gen[fa[q.top()]]+1;
    29                 q.pop();
    30                 if(q.empty())
    31                     break;
    32             }
    33         }
    34     }
    35 }
    36 int main()
    37 {
    38     int n,m,a,flag;
    39     double z,r,bei[N]= {0},sum=0;
    40     scanf("%d%lf%lf",&n,&z,&r);
    41     fa[0] = 0;
    42     for(int  i = 0; i < n; i ++)
    43     {
    44         scanf("%d",&m);
    45         if(m==0)
    46         {
    47             scanf("%d",&a);
    48             ans.push_back(i);
    49             mp[i]=a;
    50         }
    51         else
    52         {
    53             for(int j = 0; j < m; j ++)
    54             {
    55                 scanf("%d",&a);
    56                 fa[a]=i;
    57             }
    58         }
    59     }
    60     fstack(n);
    61     bei[0] = z;
    62     double k = (100-r)/(double)100;
    63     for(int i = 1; i < n; i ++)
    64     {
    65         bei[i] = bei[i-1]*k;
    66     }
    67     vector<int>::iterator it;
    68     for(it = ans.begin(); it!=ans.end(); it++)
    69     {
    70         flag = *it;
    71         sum += bei[gen[flag]]*mp[flag];
    72     }
    73     int as = sum;
    74     printf("%d
    ",as);
    75     return 0;
    76 }
  • 相关阅读:
    JS的运行机制
    Vue路由
    javascript的逼格
    Vue开发中遇到的问题及解决方案
    模块模式
    2019年终总结
    http知识总结
    小议函数的节流和防抖
    nodejs初探一二
    Object是个什么鬼
  • 原文地址:https://www.cnblogs.com/dark-ming/p/13870960.html
Copyright © 2011-2022 走看看