http://codeforces.com/contest/878/problem/Bc++
This time the Berland Team Olympiad in Informatics is held in a remote city that can only be reached by one small bus. Bus has n passenger seats, seat i can be occupied only by a participant from the city ai.spa
Today the bus has completed m trips, each time bringing n participants. The participants were then aligned in one line in the order they arrived, with people from the same bus standing in the order of their seats (i. e. if we write down the cities where the participants came from, we get the sequence a1, a2, ..., an repeated m times).code
After that some teams were formed, each consisting of k participants form the same city standing next to each other in the line. Once formed, teams left the line. The teams were formed until there were no k neighboring participants from the same city.orm
Help the organizers determine how many participants have left in the line after that process ended. We can prove that answer doesn't depend on the order in which teams were selected.three
The first line contains three integers n, k and m (1 ≤ n ≤ 105, 2 ≤ k ≤ 109, 1 ≤ m ≤ 109).ip
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 105), where ai is the number of city, person from which must take seat i in the bus.ci
Output the number of remaining participants in the line.rem
4 2 5
1 2 3 1get
12input
1 9 10
1
1
3 2 10
1 2 1
0
In the second example, the line consists of ten participants from the same city. Nine of them will form a team. At the end, only one participant will stay in the line.
给你长度为n的序列,如今把这个序列重复写m次,而后消去长度为k的相同序列,消去若干次以后,问最后剩下什么。
首先预先先把能消除的消除了。而后咱们把这个序列切成三块,l+mid+r,其中l和r是可以互相消去的。
假设l和r拼在一块儿,最后消成了p。
那么最后答案必定是这样的构成 l+mid+p+mid+p+....+p+mid+r这样的。
最后分p是否消除完来讨论便可。
若是p的长度为0,那么咱们还得考虑多个mid合在一块儿的状况。
个人代码写的很丑。。。由于一开始我写的时候少考虑不少种状况,后面修修改改才过去的。
#include<bits/stdc++.h> using namespace std; const int maxn = 1e6+7; int n,k,m,x,p,tot; int val[maxn],cnt[maxn],val2[maxn],cnt2[maxn]; int a[maxn]; int main(){ scanf("%d%d%d",&n,&k,&m); for(int i=0;i<n;i++){ scanf("%d",&x); a[tot]=x; cnt[tot]=1; if(tot>0&&a[tot]==a[tot-1])cnt[tot]=cnt[tot-1]+1; if(cnt[tot]>=k){ tot-=k; } tot++; } memset(cnt,0,sizeof(cnt)); for(int i=0;i<tot;i++){ x=a[i]; if(p>0&&x==val[p-1]){ cnt[p-1]++; }else{ val[p]=x; cnt[p]=1; p++; } } tot=0; for(int i=0;i<p;i++){ cnt[i]%=k; val2[i]=val[i]; cnt2[i]=cnt[i]; } long long ans = 0; for(int i=0;i<p;i++){ ans+=cnt[i]; } if(p==1){ cout<<1ll*ans*m%k<<endl; return 0; } if(m==1){ cout<<1ll*ans<<endl; return 0; } for(int i=0;i<p;i++){ if(cnt2[i]==0)continue; val[tot]=val2[i]; cnt[tot]=cnt2[i]; tot++; } if(val[0]!=val[tot-1]){ cout<<1ll*ans*m<<endl; return 0; } int l = 0,r = tot-1; int he = 0; long long solve = 0; long long deal = 0; long long cntl=0,cntr=0; while(he==0&&l<r){ if(val[l]==val[r]){ cntl+=cnt[l]; cntr+=cnt[r]; deal+=(cnt[l]+cnt[r]); if((cnt[l]+cnt[r])%k){ solve+=(cnt[l]+cnt[r])%k; he = 1; break; } }else{ break; } l++; r--; } long long mid = ans-deal; //cout<<cntl<<" "<<cntr<<" "<<mid<<" "<<solve<<" "<<he<<endl; if(val[0]==val[tot-1]&&he){ cout<<cntl+cntr+mid*m+solve*(m-1)<<endl; return 0; } // cout<<r<<" "<<l<<endl; int flag = r>l?0:1; if(flag==0){ cout<<cntl+cntr+mid*m<<endl; return 0; }else if(mid*m%k==0){ cout<<(cntl+cntr)%k<<endl; }else{ cout<<(cntl+cntr+mid*m%k)<<endl; } }