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

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

And A.

[Title] Italy

$ $ A n-point of the tree has $ n-1 $ edges, respectively, $ (u_1, v_1), (u_2, v_2), \ cdots, (u_ {n-1}, v_ {n-1} ) $ Initially, U $ $ for each point, there is set $ S_u = \ {u \} $.

There $ m $ operations, operations of $ I $ $ given $ P_i, the $ S_ {u_ {p_i}} $ and $ S_ {v_ {p_i}} $ assigned to them and.

After completion of all operations, for all $ i \ in [1, n] $, $ I $ request contains the number of sets.

Data range [] $ n, m \ le 5 \ times 10 ^ 5 $.

【answer】

Consider each point when it will be counted. Obviously each block comprising affect its communication operation. The operation in reverse order processing, statistical click.

Efficiency $ O (m + n) $. Expectations score: 100.

[Code]

 1 #include<bits/stdc++.h>
 2 int n,m,u[500010],v[500010],s[500010],val[500010],q[500010];
 3 signed main()
 4 {
 5     scanf("%d%d",&n,&m);
 6     for ( int i=1;i<n;i++ ) scanf("%d%d",&u[i],&v[i]);
 7     for ( int i=1;i<=n;i++ ) s[i]=1;
 8     for ( int i=1;i<=m;i++ ) scanf("%d",&q[i]);
 9     for ( int i=m,x;i;i-- ) x=q[i],s[u[x]]=s[v[x]]=s[u[x]]+s[v[x]]-val[x],val[x]=s[u[x]];
10     for ( int i=1;i<=n;i++ ) printf("%d%c",s[i]," \n"[i==n]);
11     return 0;
12 }
DTOJ4710

C. Management

[Title] Italy

$ $ N-th row of articles, a first article $ I $ $ right value of $ a_i, break it into exactly $ k $ non-empty section, and in the same segment so a_i $ $ $ same tuple (I , j) $ as little as possible. That is, it is assumed in the right segment of the value $ $ $ I $ J article has $ c (i, j) $ th, please minimized $ \ sum \ limits_ {i = 1} ^ k \ sum \ limits_ { j} \ binom {c (i, j)} {2} $.

数据 范围】 【$ 2 \ n \ 10 ^ 5 \ 1 \ and k \ and \ min \ {n, 20 \}, \ 1 \ and a_i \ n $.

【answer】

Consider $ dp $. Provided $ f [i] [j] $ $ I $ represents the front is divided into the minimum number of segments $ k $. There are: $ f [i] [j] = \ min \ limits_ {0 \ leq k <i} (f [k] [j-1] + w (k + 1, i)) $.

For $ k $, $ k $ since small, it will be apparent to slicing, i.e. the transfer of each layer of $ j $.

Play table found to have decision-making monotonic. We can not maintain the specific situation with a pointer or monotonous queue, consider the decision-making process with the monotony of divide and conquer.

Consider how to calculate $ w (l, r) $. Will be apparent from the $ w (l, r) $ pushed to $ w (l-1, r) $ position and the like. Therefore, to maintain a similar MO team approach.

Efficiency $ O (nk \ log n) $. Expectations score: 100.

 1 #include<bits/stdc++.h>
 2 const long long inf=1LL<<60;
 3 int n,k,cnt[100010],a[100010],pl,pr;
 4 long long f[100010][22],res;
 5 inline void Ins ( int x,int v ) { res-=1LL*cnt[x]*(cnt[x]-1)/2;cnt[x]+=v;res+=1LL*cnt[x]*(cnt[x]-1)/2; }
 6 inline void solve ( int l,int r,int L,int R,int k )
 7 {
 8     int mid=(l+r)>>1;
 9     while ( pl>L+1 ) Ins(a[--pl],1);
10     while ( pr<mid ) Ins(a[++pr],1);
11     while ( pl<L+1 ) Ins(a[pl++],-1);
12     while ( pr>mid ) Ins(a[pr--],-1);
13     int p=L;long long ans=f[L][k-1]+res;
14     while ( pl<=mid and pl<=R+1 )
15     {
16         if ( f[pl-1][k-1]+res<=ans ) ans=f[pl-1][k-1]+res,p=pl-1;
17         Ins(a[pl++],-1);
18     }
19     f[mid][k]=ans;
20     if ( l<=mid-1 ) solve(l,mid-1,L,p,k);
21     if ( mid+1<=r ) solve(mid+1,r,p,R,k);
22 }
23 signed main()
24 {
25     scanf("%d%d",&n,&k);
26     for ( int i=1;i<=n;i++ ) scanf("%d",&a[i]);
27     for ( int i=0;i<=n;i++ ) for ( int j=0;j<=k;j++ ) f[i][j]=inf;
28     f[0][0]=0;
29     for ( int j=1;j<=k;j++ )
30     {
31         res=0;pl=1;pr=n;
32         for ( int i=1;i<=n;i++ ) cnt[i]=0;
33         for ( int i=1;i<=n;i++ ) Ins(a[i],1);
34         solve(1,n,0,n-1,j);
35     }
36     return !printf("%lld\n",f[n][k]);
37 }
DTOJ4718

Guess you like

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