2020 provincial election simulations Round # 13 solution (20/02/20)

[Link] game http://59.61.75.5:8018/contest/223

A. Euler path tree

【answer】

Consider dfs processes each sub-tree.

For each digital Obviously, one of the two adjacent sub-tree is the node between the same kind digital recursive process.

Now consider a case:. $ 1 2 \ dots 2 -1 -1 1 $ $ $ 3 assumed not occurred, there are two filling methods: $ 1 2 \ dots 2 1 3 1 $ and $ 1 2 \ dots 2 3 2 $ 1.

The two filling methods noted tree corresponding form of $ 3 to $ $ $ 2 and $ 3 $ child node to a child node $ $ 1. Analysis known drawing, both without any distinction to other nodes.

Therefore, we consider greedy, and more fill the current $ 1 $ as possible. Even if each of them sub-trees as small as possible, the number of sub-trees as much as possible.

Obviously first recursive process $ 2 \ 2 $ DOTS subtree tree, the recursive back compressed into a single node $ 2 $.

Considering now the processing of the problem type $ 21 $ 1-1-1. Depending on the nature of the tree, it should be used to maintain a stack.

If you encounter a non-number $ -1 $, and the current number occurred (probably $ -1 $ result), the next number is judged that the top of the stack are equal, equal to the top of the stack pop up, otherwise the stack.

For $ $ -1, there is a case that the number of appeared back must be completed: 1-1321 $ $ $ 2 must be completed at this time can get $ Analysis: When the $ 2 (sum_ {j-1} -sum_ { i}) = ji $ and $ i, j $ parity phase simultaneously, can fill $ a [j] $.

The above formula may be converted to $ 2 sum_ {i} -i = 2sum {j-1} -j $. Data structure ( The splay sleeve The splay Map jacket set) to maintain partial parity.

So for every $ -1 $ are: if we can find a place to fill in the fill and push; otherwise, if the stack is popped to empty the election a number of unused fill.

Efficiency $ O (n \ log n) $ or $ O (n \ log ^ 2 n) $ (The data structures may be used, there may be a linear approach) of the desired score: 100.

[Code]

 1 #include<bits/stdc++.h>
 2 inline int read ( void )
 3 {
 4     int x=0;char ch;bool f=true;
 5     while ( !isdigit(ch=getchar()) ) if ( ch=='-' ) f=false;
 6     for ( x=ch^48;isdigit(ch=getchar()); ) x=(x<<1)+(x<<3)+(ch^48);
 7     return f ? x : -x ;
 8 }
 9 const int maxn=1000000+10;
10 int a[maxn],st[maxn],tp,n,sum[maxn],vis[maxn],times;
11 std::map<int,std::set<int>> s[2];
12 std::queue<int> unused;
13 std::vector<int> pos[maxn];
14 inline void solve ( int l,int r,int x )
15 {
16     std::vector<int> G;
17     for ( int i=l;i<=r;i++ )
18         if ( ~a[i] )
19         {
20             for ( int j=0;j<(int)pos[a[i]].size()-1;j++ ) solve(pos[a[i]][j]+1,pos[a[i]][j+1]-1,a[i]);
21             G.push_back(a[i]);i=pos[a[i]].back();
22         }
23         else G.push_back(-i);
24     st[tp=1]=x;sum[0]=(G[0]>0);s[0].clear();s[1].clear();++times;
25     for ( int i=1;i<(int)G.size();i++ )
26         if ( G[i]>0 ) sum[i]=sum[i-1]+1,s[i&1][i-2*sum[i-1]].insert(i);
27         else sum[i]=sum[i-1];
28     for ( int i=0;i<(int)G.size();i++ )
29         if ( G[i]>0 )
30         {
31             s[i&1][i-2*sum[i-1]].erase(i);
32             if ( tp>1 and st[tp-1]==G[i] ) tp--;
33             else st[++tp]=G[i];
34         }
35         else
36         {
37             if ( !s[i&1][i-2*sum[i]].empty() )
38             {
39                 int j=*s[i&1][i-2*sum[i]].begin();
40                 a[-G[i]]=G[j];
41                 if ( vis[G[j]]==times ) while ( st[tp]!=G[j] ) tp--;
42                 else vis[G[j]]=times,st[++tp]=G[j];
43             }
44             else
45             {
46                 if ( tp>1 ) a[-G[i]]=st[--tp];
47                 else st[++tp]=a[-G[i]]=unused.front(),unused.pop();
48             }
49         }
50 }
51 signed main()
52 {
53     for ( int T=read();T--; )
54     {
55         n=read();
56         for ( int i=1;i<(n<<1);i++ ) a[i]=read();
57         a[1]=a[2*n-1]=1;
58         for ( int i=1;i<(n<<1);i++ ) if ( ~a[i] ) pos[a[i]].push_back(i);
59         for ( int i=1;i<=n;i++ ) if ( pos[i].empty() ) unused.push(i);
60         for ( int i=0;i<(int)pos[1].size()-1;i++ ) solve(pos[1][i]+1,pos[1][i+1]-1,1);
61         for ( int i=1;i<(n<<1);i++ ) printf("%d%c",a[i]," \n"[i==2*n-1]);
62         for ( int i=1;i<=n;i++ ) pos[i].clear();
63     }
64     return 0;
65 }
DTOJ4724

C. Travel

【answer】

Recent tests have FJOI more and more of a style. . .

Consider $ k $ is very small, it can be used similar practices binary search.

Split ring halves so that force, dyed black half, the other half colored white. White on black, respectively, to solve the longest chain reprocessing.

Consider how apparently only using a random approach to all points stained. So consider random $ T $ times to solve.

For the processing after dyeing, it is clear to the maximum processing only where $ l = 5 $ ($ l $ is the number of points on the chain), and then:

$ L = 2 $, enumerate each edge.

$ L = 3 $, enumeration determines whether two sides with a common point, there are updated.

$ L = 4 $, a distance between the first pre-processing all sides two points, and then consider the two sides enumeration, if no common point of the two sides with a pretreatment wherein a distance between each node coupled to both sides of the right side .

$ L = 5 $, consider the edge $ (u, v), (x, y) $. Contribution apparently if $ (u, v, p, x, y) $ type contribution, then for $ v, x $ need to count $ 3 $ longest chain points. Be careful not to repeat, it is necessary to maintain the first three long chain.

The first round of two sides of the enumeration process three long-chain, the answer to the second count wheel enumerated two sides.

Finally, a numerical answer enumeration cross-color two sides considered the answer. Efficiency $ O (Tm ^ 2) $.

When the $ T = 1000 $ answer the probability of error is very small (wrong once again cross the line) , and the complexity of the outstanding expectations score: 0 to 100 (if non Emirates possible $ 0 $) .

[Code]

 1 #include<bits/stdc++.h>
 2 const long long inf=1LL<<60;
 3 int n,m,k,u[400],v[400],c[400],pos[400][400][3];
 4 long long w[400],dis[400][400],Dis[2][400][400]; 
 5 inline void solve2 ( int color )
 6 {
 7     for ( int i=1;i<=m;i++ ) if ( c[u[i]]==color and c[v[i]]==color ) Dis[color][u[i]][v[i]]=Dis[color][v[i]][u[i]]=w[i];
 8 }
 9 inline void solve3 ( int color )
10 {
11     for ( int i=1;i<=m;i++ ) if ( c[u[i]]==color and c[v[i]]==color )
12         for ( int j=1;j<=m;j++ ) if ( c[u[j]]==color and c[v[j]]==color ) if ( i!=j )
13         {
14             if ( u[i]==u[j] ) Dis[color][v[i]][v[j]]=std::max(Dis[color][v[i]][v[j]],w[i]+w[j]);
15     else    if ( u[i]==v[j] ) Dis[color][v[i]][u[j]]=std::max(Dis[color][v[i]][u[j]],w[i]+w[j]);
16     else    if ( v[i]==u[j] ) Dis[color][u[i]][v[j]]=std::max(Dis[color][u[i]][v[j]],w[i]+w[j]);
17     else    if ( v[i]==v[j] ) Dis[color][u[i]][u[j]]=std::max(Dis[color][u[i]][u[j]],w[i]+w[j]);
18         }
19 }
20 inline void solve4 ( int color )
21 {
22     for ( int i=1;i<=m;i++ ) if ( c[u[i]]==color and c[v[i]]==color )
23         for ( int j=1;j<=m;j++ ) if ( c[u[j]]==color and c[v[j]]==color )
24             if ( i!=j ) if ( u[i]!=u[j] and u[i]!=v[j] and v[i]!=u[j] and v[i]!=v[j] )
25                 Dis[color][u[i]][u[j]]=std::max(Dis[color][u[i]][u[j]],dis[v[i]][v[j]]+w[i]+w[j]),
26                 Dis[color][u[i]][v[j]]=std::max(Dis[color][u[i]][v[j]],dis[v[i]][u[j]]+w[i]+w[j]),
27                 Dis[color][v[i]][u[j]]=std::max(Dis[color][v[i]][u[j]],dis[u[i]][v[j]]+w[i]+w[j]),
28                 Dis[color][v[i]][v[j]]=std::max(Dis[color][v[i]][v[j]],dis[u[i]][u[j]]+w[i]+w[j]);
29 }
30 inline void solve5 ( int color )
31 {
32     std::function < void ( int,int,int ) > Addedge = [&] ( int x,int y,int z ) {
33         int rnk=0;
34         while ( rnk<3 )
35         {
36             int u=pos[x][y][rnk];
37             if ( dis[x][u]+dis[u][y]<dis[x][z]+dis[z][y] ) break;
38             rnk++;
39         }
40         if ( rnk<3 )
41         {
42             for ( int i=2;i>rnk;i-- ) pos[x][y][i]=pos[x][y][i-1];
43             pos[x][y][rnk]=z;
44         }
45     };
46     std::function < void ( int,int,int,int ) > work = [&] ( int x,int y,int u,int v ) {
47         int rnk = 0;
48         while ( pos[u][v][rnk]==x or pos[u][v][rnk]==y ) rnk++;
49         int z=pos[u][v][rnk];
50         Dis[color][x][y]=std::max(Dis[color][x][y],dis[x][u]+dis[u][z]+dis[z][v]+dis[v][y]);
51     };
52     for ( int i=1;i<=n;i++ ) if ( c[i]==color ) for ( int j=1;j<=n;j++ ) if ( c[j]==color ) pos[i][j][0]=pos[i][j][1]=pos[i][j][2]=0;
53     for ( int i=1;i<=m;i++ ) if ( c[u[i]]==color and c[v[i]]==color )
54         for ( int j=1;j<=m;j++ ) if ( c[u[j]]==color and c[v[j]]==color ) if ( i!=j )
55         {
56             if ( u[i]==u[j] ) Addedge(v[i],v[j],u[i]);
57     else    if ( u[i]==v[j] ) Addedge(v[i],u[j],u[i]);
58     else    if ( v[i]==u[j] ) Addedge(u[i],v[j],v[i]);
59     else    if ( v[i]==v[j] ) Addedge(u[i],u[j],v[i]);
60         }
61     for ( int i=1;i<=m;i++ ) if ( c[u[i]]==color and c[v[i]]==color )
62         for ( int j=1;j<=m;j++ ) if ( c[u[j]]==color and c[v[j]]==color )
63             if ( i!=j ) if ( u[i]!=u[j] and u[i]!=v[j] and v[i]!=u[j] and v[i]!=v[j] )
64                 work(u[i],u[j],v[i],v[j]),work(u[i],v[j],v[i],u[j]),work(v[i],u[j],u[i],v[j]),work(v[i],v[j],u[i],u[j]);
65 }
66 signed main()
67 {
68     scanf("%d%d%d",&n,&m,&k);
69     for ( int i=0;i<=n;i++ ) for ( int j=0;j<=n;j++ ) dis[i][j]=-inf;
70     for ( int i=1;i<=m;i++ ) scanf("%d%d%lld",&u[i],&v[i],&w[i]),dis[u[i]][v[i]]=dis[v[i]][u[i]]=w[i];
71     if ( k==3 )
72     {
73         long long ans=-1;
74         for ( int i=1;i<=n-2;i++ ) for ( int j=i+1;j<=n-1;j++ ) for ( int k=j+1;k<=n;k++ ) ans=std::max(ans,dis[i][j]+dis[j][k]+dis[k][i]);
75         if ( ~ans ) printf("%lld\n",ans);
76         else puts("impossible");
77         return 0;
78     }
79     srand(time(NULL));long long ans=-1;
80     for ( int Rnd=1;Rnd<=1000;Rnd++ )
81     {
82         for ( int i=1;i<=n;i++ ) c[i]=rand()&1;
83         for ( int i=1;i<=n;i++ ) for ( int j=1;j<=n;j++ ) Dis[0][i][j]=Dis[1][i][j]=-inf;
84         for ( int i=1;i<=m;i++ ) if ( c[u[i]] and !c[v[i]] ) std::swap(u[i],v[i]);
85         if ( k==4 ) solve2(0),solve2(1);
86         else if ( k==5 ) solve2(0),solve3(1);
87          else  if (k == 6 ) solve3 ( 0 ) solve3 ( 1 );
88          else  if (k == 7 ) solve3 ( 0 ) solve4 ( 1 );
89          else  if (k == 8 ) solve4 ( 0 ) solve4 ( 1 );
90          else  if (k == 9 ) solve4 ( 0 ) solve5 ( 1 );
91          else  if (k == 10 ) solve5 ( 0 ) solve5 ( 1 );
92         for ( int i=1;i<=m;i++ ) if ( c[u[i]]^c[v[i]] ) for ( int j=1;j<=m;j++ ) if ( c[u[j]]^c[v[j]] )
93             if ( i!=j ) ans=std::max(ans,Dis[0][u[i]][u[j]]+Dis[1][v[i]][v[j]]+w[i]+w[j]);
94     }
95     if ( ~ans ) printf("%lld\n",ans);
96     else puts("impossible");
97     return 0;
98 }
DTOJ4727

Guess you like

Origin www.cnblogs.com/RenSheYu/p/12341340.html