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 ;
}

443 D. Teams Formation的更多相关文章

  1. cf 443 D. Teams Formation](细节模拟题)

    cf 443 D. Teams Formation(细节模拟题) 题意: 给出一个长为\(n\)的序列,重复\(m\)次形成一个新的序列,动态消除所有k个连续相同的数字,问最后会剩下多少个数(题目保证 ...

  2. Codeforces Round #443 (Div. 1) B. Teams Formation

    B. Teams Formation link http://codeforces.com/contest/878/problem/B describe This time the Berland T ...

  3. CodeForces 879D Teams Formation

    题意 将一个长度为\(n\)的数组重复\(m\)遍得到一个长度为\(n \times m\)的新序列,然后消掉新序列中连续\(k\)个相同的元素,不断重复这一过程,求最后剩下的序列的长度 分析 首先可 ...

  4. Teams Formation

    题意: 给定一长度为 n 的整数序列 $a$,将其复制m次,并接成一条链,每相邻K个相同的整数会消除,然后其他的整数继续结成一条链,直到不能消除为止,求问最终剩余多少个整数. 解法: 首先将长度为n的 ...

  5. 【Codeforces】879D. Teams Formation 思维+模拟

    题意 给定$n$个数,重复拼接$m$次,相邻$k$个重复的可消除,问最后序列中有多少个数 首先可以发现当$k>=n$时,如果要使$n$个数可以被消除,那么$n$个数必须一样,否则$n$个数不能被 ...

  6. codeforces 879 D. Teams Formation(思维)

    题目链接:http://codeforces.com/contest/879/problem/D 题意:这题题意我反正是看了很久,可能是我的理解能力有点差,就是将一个数组倍增m倍然后将连续的相同的k个 ...

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

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

  8. zoj The 12th Zhejiang Provincial Collegiate Programming Contest Team Formation

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5494 The 12th Zhejiang Provincial ...

  9. 第十二届浙江省大学生程序设计大赛-Team Formation 分类: 比赛 2015-06-26 14:22 50人阅读 评论(0) 收藏

    Team Formation Time Limit: 3 Seconds Memory Limit: 131072 KB For an upcoming programming contest, Ed ...

随机推荐

  1. android之在view中内嵌浏览器的方法

    我要做的一个东西是在一个页面的中间嵌入浏览器,一开始不知道从哪里开始,因为以前用的都是Textveiw或者editVeiw之类的控件,而它们并不能用来显示网页的内容,怎么办呢? 首先想到的是:是不是有 ...

  2. iview里select组件搜索后选中的数据和展示内容不一样

    原因:option上的key设置的不唯一 保证key的值唯一

  3. zookeeper应用 - 配置服务

    一端不停的更新配置,另一端监听这个配置的变化.     需要注意的是:监听端不一定读取到所有的变化.在zk服务器发送通知到客户端,客户端读取数据注册监听之间可能发生了多次数据变化,这些数据变化是得不到 ...

  4. asp.net(C#)常用时间日期处理类

    using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Secu ...

  5. CSS 小结笔记之清除浮动

    浮动是一个非常好用的属性,但是有时会出现一些问题,需要进行清除浮动.例如 <!DOCTYPE html> <html lang="en"> <head ...

  6. 7 Recursive AutoEncoder结构递归自编码器(tensorflow)不能调用GPU进行计算的问题(非机器配置,而是网络结构的问题)

    一.源代码下载 代码最初来源于Github:https://github.com/vijayvee/Recursive-neural-networks-TensorFlow,代码介绍如下:“This ...

  7. winform listbox增加鼠标双击事件

    在Form.Designer.cs文件中对于listBox处理: listBox.MouseDoubleClick += new system.Windows.Forms.MouseEventHand ...

  8. javascript版format函数,方便实现复杂字串连接

    javascript版format函数,方便实现复杂字串连接 String.prototype.format = function () { var args = arguments; console ...

  9. SDWebImage动画加载图片

    SDWebImage动画加载图片 效果 源码 https://github.com/YouXianMing/Animations // // PictureCell.m // SDWebImageLo ...

  10. 静态代码分析工具sonarqube+sonar-runner的安装配置及使用

    配置成功后的代码分析页面: 可以看到对复杂度.语法使用.重复度等等都做了分析,具体到了每一个方法和每一句代码. 四种使用方式: sonarqube + sonar-runner sonarqube + ...