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

D. Teams Formation

Problem Statement

    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

Example 1
    Input
        4 2 5
        1 2 3 1
    Output
        12
Example 2
    Input
        1 9 10
        1
    Output
        1
Example 3
    Input
         3 2 10
         1 2 1
    Output
         0

Note

    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遍接在该数组后面(总共m遍)。之后我们可以像玩祖玛那样,如果有相同的连着的k个数字,那就可以直接删去这k个并且将他们的两端合并起来,如果有多于k个的,那也只能删k个,问你最后剩下的无法消去的数字个数是多少。

思路

    这题的话,对于一个长度为n的序列,先将这个序列压缩一下,就是如果相同的数连在一起那我们就记一个并记下他连续的个数,之后我们先考虑他的内部是否已经存在连续k个一样的数字,如果已经存在,那么先将这些消去,否则我们用两个指针,一个指向序列开头,另一个指向尾部,表示当相邻两个串合并起来的时候是否有连续k个相同的数字,如果有那么删掉,最后就直接暴力做下去,直到不存在满足要求的数。最后统计一下答案就行了(至于怎么统计就看代码吧)。

Code

#pragma GCC optimize(3)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
bool Finish_read;
template<class T>
inline void read(T &x) {
    Finish_read=0;x=0;int f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;if(ch==EOF)return;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    x*=f;Finish_read=1;
}
template<class T>
inline void print(T x) {
    if(x/10!=0)
        print(x/10);
    putchar(x%10+'0');
}
template<class T>
inline void writeln(T x) {
    if(x<0)
        putchar('-');
    x=abs(x);
    print(x);
    putchar('\n');
}
template<class T>
inline void write(T x) {
    if(x<0)
        putchar('-');
    x=abs(x);
    print(x);
}
/*================Header Template==============*/
ll tot,n,m,k,x,s[100010][2],cnt;
int main() {
    read(n);
    read(k);
    read(m);
    for(int i=1;i<=n;i++) {
        read(x);
        if(!cnt||s[cnt][0]!=x) {
            s[++cnt][0]=x;
            s[cnt][1]=1;
        }
        else
            s[cnt][1]++;
        if(s[cnt][1]==k) {
            s[cnt][1]=0;
            cnt--;
        }
    }
    for(int i=1;i<=cnt;i++)
        tot+=s[i][1];
    int head=1,tail=cnt;
    while(head<tail&&s[head][0]==s[tail][0]) {
        if((s[head][1]+s[tail][1])%k==0) {
            head++;
            tail--;
        }
        else {
            s[head][1]=(s[head][1]+s[tail][1])%k;
            s[tail][1]=0;
            break;
        }
    }
    ll ans=0;
    if(head<tail) {
        for(int i=head;i<=tail;i++)
            ans+=s[i][1];
        ans*=(m-1);
        ans+=tot;
    }
    else if(head==tail){
        if((s[head][1]*m)%k==0)
            ans=0;
        else {
            ans=tot+s[head][1]*(m-1);
            ans-=s[head][1]*m-s[head][1]*m%k;
        }
    }
    writeln(ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值