CF878B Teams Formation

B. Teams Formation
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 npassenger 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 ≤ 1052 ≤ k ≤ 1091 ≤ 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.

两个要点,一个是状态压缩,用一个pair来压缩连续的点,第二个是情况的覆盖,即首位均回文了,中间一块可以重复m次。

#include <stdio.h>
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>

#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;

#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define ldb ldouble

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;

int IT_MAX = 1 << 17;
const ll MOD = 1000000009;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const db PI = acos(-1);
const db ERR = 1e-8;
#define szz(x) (int)(x).size()
#define rep(i, n) for(int i=0;i<n;i++)

vector <pll> Vu;
int main() {
	int N, K, M, i, j;
	scanf("%d %d %d", &N, &K, &M);
	for (i = 1; i <= N; i++) {
		int t;
		scanf("%d", &t);
		if (!Vu.empty() && Vu.back().first == t) {
			Vu.back().second++;
			if (Vu.back().second == K) Vu.pop_back();
		}
		else Vu.emplace_back(t, 1);
	}

	ll s = 0;
	for (auto it : Vu) s += it.second;
	if (M == 1 || s == 0) return !printf("%lld\n", s);

	s *= M;
	int st = 0, en = (int)Vu.size() - 1;
	while (st < en) {
		if (Vu[st].first != Vu[en].first) return !printf("%lld\n", s);
		if (Vu[st].second + Vu[en].second == K) {
			s -= (ll)(M - 1) * K;
			st++, en--;
			continue;
		}
		if (Vu[st].second + Vu[en].second > K) s -= (ll)(M - 1)*K;
		return !printf("%lld\n", s);
	}
	if (st > en) return !printf("0\n");
	if (st == en) {
		ll x = Vu[st].second * M;
		if (x % K == 0) {
           // cout<<"hi"<<endl;
            return !printf("0\n");

		}
		else{

            return !printf("%lld\n", s - (x / K)*K);
		}
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值