B. Teams Formation

link

http://codeforces.com/contest/878/problem/B

describe

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

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次,然后消去长度为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;
}
}

Codeforces Round #443 (Div. 1) B. Teams Formation的更多相关文章

  1. Codeforces Round #443 (Div. 2) 【A、B、C、D】

    Codeforces Round #443 (Div. 2) codeforces 879 A. Borya's Diagnosis[水题] #include<cstdio> #inclu ...

  2. Codeforces Round #443 Div. 1

    A:考虑每一位的改变情况,分为强制变为1.强制变为0.不变.反转四种,得到这个之后and一发or一发xor一发就行了. #include<iostream> #include<cst ...

  3. Codeforces Round #443 (Div. 1) D. Magic Breeding 位运算

    D. Magic Breeding link http://codeforces.com/contest/878/problem/D description Nikita and Sasha play ...

  4. Codeforces Round #443 (Div. 1) A. Short Program

    A. Short Program link http://codeforces.com/contest/878/problem/A describe Petya learned a new progr ...

  5. Codeforces Round #443 (Div. 2) C. Short Program

    C. Short Program time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  6. Codeforces Round #443 (Div. 1) C. Tournament

    题解: 思路挺简单 但这个set的应用好厉害啊.. 我们把它看成图,如果a存在一门比b大,那么a就可以打败b,a——>b连边 然后求强联通分量之后最后顶层的强联通分量就是能赢的 但是因为是要动态 ...

  7. Codeforces Round #443 (Div. 2)

    C. Short Program Petya learned a new programming language CALPAS. A program in this language always ...

  8. Codeforces Round #443 (Div. 2) C 位运算

    C. Short Program time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  9. 【Codeforces Round #443 (Div. 2) A】Borya's Diagnosis

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟 [代码] #include <bits/stdc++.h> using namespace std; const ...

随机推荐

  1. SVG 图像入门教程

    http://www.ruanyifeng.com/blog/2018/08/svg.html 一.概述 SVG 是一种基于 XML 语法的图像格式,全称是可缩放矢量图(Scalable Vector ...

  2. zookeeper都有哪些使用场景

    分布式协调 这个其实是zk很经典的一个用法,比如,A系统发送个请求到mq,然后B拿到消息消费之后处理了.那A系统如何知道B系统的处理结果? 用zk就可以实现分布式系统之间的协调工作.A系统发送请求之后 ...

  3. 前后台交互经常使用的技术汇总(后台:Java技术,前台:Js或者Jquery)

    1:由于针对特定的前后台交互用到的知识总结,所以不大量贴代码,主要给出思路,方便自己以后脑补和技术总结,当然也希望可以帮助到别人. 后台Json和其他格式转化,之前总结过Json和对象,集合,字符串的 ...

  4. Zabbix监控——Zabbix自定义用户参数制作监控项

    https://blog.51cto.com/183530300/2087774 https://www.cnblogs.com/richardzgt/articles/7889404.html

  5. UIImageView的常用方法

    //初始化 init(image: UIImage!) @availability(iOS, introduced=3.0)//初始化,highlightedImage 高亮图片 init(image ...

  6. SQL Server中自定义函数:用指定的分隔符号分割字符串

    微软SQL Server数据库中包含了很多内置的函数,入下图: 它们用于处理日期.数学.元数据.字符串等. 其中最为常用的就是处理字符串,里面包含了CharIndex()等函数,非常方便使用. 但是对 ...

  7. 怎样把linux客户端用户禁止用 su命令来切换用户

    系统中有一个组叫做“wheel”,我们可以利用该组实现一些特殊的功能.我们可以将拥有su使用权限的用户加入到wheel组中并且对该组进行限制,那么只有在该组中的用户才有su的使用权限. 如要实现该功能 ...

  8. Hadoop| MapReduce01 概述

    概述 分布式运算程序: 优点:易于编程:良好扩展性:高容错性:适合PB级以上海量数据的离线处理: 缺点:不擅长实时计算:不擅长流式计算:不擅长DAG有向图计算: 核心思想: 1)分布式的运算程序往往需 ...

  9. 20165235 祁瑛 2018-4 《Java程序设计》第九周学习总结

    20165235 祁瑛 2018-4 <Java程序设计>第九周学习总结 教材学习内容总结 URL类 UR类是java.net包中的一个重要类,使用URL创建的对象的应用程序称作称作客户端 ...

  10. 5、Qt Project之键盘数据监控

    键盘数据监控: 同样的,键盘的检测和鼠标的情形很类似,都是以QWidget为基类的工程 Step1:在UI设计中添加该模块需要使用的相关组件,如下所示: <width>141</wi ...