443 D. Teams Formation

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[100005];
int a[100005];
bool flag = true;
int cnt[100005];

int main() {
  // freopen("trans.in","r",stdin);
  // freopen("trans.out","w",stdout);
  ios::sync_with_stdio(false);
  cin >> n >> k >> m;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 2; i <= n; i++) //判断是否全相等
    if (a[i] != a[i - 1]) {
      flag = false;
      break;
    }
  if (flag) {
    cout << (ll)n * m % k;
    return 0;
  }
  int top = 0;
  for (int i = 1; i <= n; i++) {
    s[++top] = a[i];
    if (s[top] == s[top - 1])
      cnt[top] = cnt[top - 1] + 1;
    else
      cnt[top] = 1;
    if (cnt[top] == k)//将每相同k段扔掉,并调整top位置
      top -= k;
  }
  int L = 1, R = top;
  int t = 0;
  while (s[L] == s[R] && L < R) {//处理边界,开l,r两头走
    int l = L, r = R;
    int sum = 0;
    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 + 1; i <= R; i++)
    if (s[i] != s[i - 1]) {
      flag = false;
      break;
    }
  if (flag) {
    ll mid = (ll)(R - L + 1) * m % k;//注意范围,爆int
    if (mid)
      cout << mid + t;
    else
      cout << 0;
  } else
    cout << (ll)(R - L + 1) * m + t;

  return 0;
}

 

转载于:https://www.cnblogs.com/planche/p/8506541.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值