Codeforces Round #443 (Div. 2) D. Teams Formation

D. Teams Formation
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.

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).

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.

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.

Input
The first line contains three integers n, k and m (1 ≤ n ≤ 105, 2 ≤ k ≤ 109, 1 ≤ m ≤ 109).

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.

Output
Output the number of remaining participants in the line.

Examples
input
4 2 5
1 2 3 1
output
12
input
1 9 10
1
output
1
input
3 2 10
1 2 1
output
0

这一题用到了回文的性质:对于一个串,如果它相邻的两个单位不同,对于把它复制两次得到的串,如果他自身是回文串,那么是可能全部消掉,不然是不可能全部消掉的。
所以,用两个点标记,每次加加减减,就可以得到最终的串,并且上届是不会比下届小的。
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+100;
int s[N],cnt[N];
int n,m,k;
int main(){
    cin >> n >> k>> m;
    int tot = 0;
    s[0] = -1;
    for(int i = 1;i <= n;i ++){
        int now;
        scanf("%d",&now);
        if(now == s[tot]) cnt[tot]++;
        else s[++tot] = now,cnt[tot] = 1;
        if(cnt[tot] == k) --tot;
    }
    long long u = 0;
    for(int i = 1;i <= tot;i ++) u += cnt[i];
    int t = 1,h = tot;
    while(t < h&&s[t] == s[h]){
        if((cnt[t]+cnt[h])%k == 0){
            ++t,--h;
        }
        else {
            cnt[h] = (cnt[t]+cnt[h])%k;
            cnt[t] = 0;
            break;
        }
    }
    long long ans = 0;
    //cout << t << ' ' << h << endl;
    if(t < h){
        long long now = 0;
        for(int i = t;i <= h;i ++) now += cnt[i];
        //cout <<"!!"<< now << ' '<<cnt[t] << ' ' << cnt[h] <<  endl;
        ans = 1ll*now*(m-1)+u;
    }
    else{
        if(1ll*cnt[h]*m%k ==0) ans = 0;
        else ans = u - cnt[h]+(1ll*cnt[h]*m)%k;
    }
    cout << ans << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值