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 ≤ 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
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 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#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  pii;
typedef pair  pll;
typedef pair  pli;
typedef pair  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 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"<



你可能感兴趣的:(CF878B Teams Formation)