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. animation 秒简单笔记

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. <Android 基础(二十九)> Fragment (2) ~ DialogFragment

    简介 上一篇简单的介绍了下Fragment的使用方法,这一篇主要看下DialogFragment. 在android 3.0时被引入.是一种特殊的Fragment,用于在Activity的内容之上展示 ...

  3. JSP用户登录页面

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  4. App 图标设计 - 圆角透明效果(0 基础使用 PS)

    App 图标设计 - 圆角透明效果(0 基础使用 PS) 方法: 如果你有些基础,就不必看图文教程了: 1.使用圆角矩形工具选中,设置圆角尺寸[例如:1024*1024 px(圆角:160 px)] ...

  5. mysql常用语句备忘

    1.连接本地数据库 mysql -h localhost -u root -p123 2.连接远程数据库 mysql -h 192.168.0.201 -P 3306 -u root -p123 3. ...

  6. DROP语句总结

    一.基本语句 通过使用 DROP 语句,可以轻松地删除索引.表和数据库.  1.DROP INDEX 语句 DROP INDEX 语句用于删除表中的索引. <!--用于 MS Access 的 ...

  7. [UI] 精美UI界面欣赏[1]

    精美UI界面欣赏[1]

  8. 北美IT求职攻略

    http://www.followmedoit.com/bbs/forum.php?mod=viewthread&tid=19&extra=page%3D1 身在北美,想留下来并能过得 ...

  9. 汽车学习---汽车知识大全【all】

    汽车驱动/发动机/大灯 侧方位停车和倒车入库技巧 手动挡车型 换档8大技巧 常见的掉头情况注意事项 最全的左转,右转,待转,掉头方法 开车技巧大汇总,尤其适合新手司机 汽车常识扫盲,不做“汽车小白”! ...

  10. Linux chown命令详解

    chown将指定文件的拥有者改为指定的用户或组,用户可以是用户名或者用户ID:组可以是组名或者组ID:文件是以空格分开的要改变权限的文件列表,支持通配符. chown常见命令参数 Usage: cho ...