http://codeforces.com/contest/879/problem/D

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 kneighboring 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

Copy
4 2 5
1 2 3 1
output
12
input

Copy
1 9 10
1
output
1
input

Copy
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遍得到一个长度为n×m的新序列,然后消掉新序列中连续k个相同的元素,不断重复这一过程,求最后剩下的序列的长度

先消内部的,再处理交界处的

#include <bits/stdc++.h>
#define ll long long
using namespace std; int n, k, m;
int s[];
int a[];
bool flag = true;
int cnt[]; int main() {
// freopen("trans.in","r",stdin);
// freopen("trans.out","w",stdout);
ios::sync_with_stdio(false);
cin >> n >> k >> m;
for (int i = ; i <= n; i++)
cin >> a[i];
for (int i = ; i <= n; i++) //判断是否全相等
if (a[i] != a[i - ]) {
flag = false;
break;
}
if (flag) {
cout << (ll)n * m % k;
return ;
}
int top = ;
for (int i = ; i <= n; i++) {
s[++top] = a[i];
if (s[top] == s[top - ])
cnt[top] = cnt[top - ] + ;
else
cnt[top] = ;
if (cnt[top] == k)//将每相同k段扔掉,并调整top位置
top -= k;
}
int L = , R = top;
int t = ;
while (s[L] == s[R] && L < R) {//处理边界,开l,r两头走
int l = L, r = R;
int sum = ;
while (s[L] == s[l] && l < r && sum < k)
sum++, l++;
while (s[L] == s[r] && l < r && sum < k)
sum++, r--;
if (sum == k)
L = l, R = r, t += k;
else
break;//不满足k段就没必要继续了,直接跳出
}
flag = true;//跟上面相同操作
for (int i = L + ; i <= R; i++)
if (s[i] != s[i - ]) {
flag = false;
break;
}
if (flag) {
ll mid = (ll)(R - L + ) * m % k;//注意范围,爆int
if (mid)
cout << mid + t;
else
cout << ;
} else
cout << (ll)(R - L + ) * m + t; return ;
}

最新文章

  1. Echarts学习笔记之饼图
  2. javascript在调试bug的奇淫技巧(Chrome, Firebug, Filddle 调试)
  3. iOS YSDropdownMagnify 下拉放大,向上导航显示
  4. ACCESS作为网站数据库的弊端
  5. 转载 Javascript继承两种形式详解
  6. [置顶] Guava学习之ArrayListMultimap
  7. ora-00600笔记
  8. 解决IOS移动端 Safari流浪器 onclick无法触发的问题
  9. 非vue-cli的花括号闪现问题
  10. Python os.path.basename
  11. 『TensorFlow &#215; MXNet』SSD项目复现经验
  12. SpringBoot SpringSession redis SESSION
  13. Django实战(22):处理登录和注销
  14. android android.mk中:= ?= +=之间的区别
  15. HDFS编程
  16. Echarts实现隐藏x轴,y轴,刻度线,网格
  17. django自带过滤器大全
  18. GlobalAlloc()和malloc()、HeapAlloc()
  19. PHPExcel如何把该列的值设置为文本无科学计数?
  20. 转:Intent与PendingIntent

热门文章

  1. python中GIL和线程与进程
  2. thinkPHP的几个系统常量
  3. HTML 5 &lt;input&gt; placeholder 属性 实现搜索框提示文字点击输入后消失
  4. PowerDesigner 常用配置修改
  5. 云数据库HBase助力物联网,免费申请中
  6. 例如android:layout_marginBottom的值为负数
  7. linux_kernel_uaf漏洞利用实战
  8. linux 权限管理命令chown、chgrp、umask、linux新建文件或目录的默认权限755
  9. 微信小程序接入腾讯云IM即时通讯(会话列表)
  10. .hivehistory