CSES - Putka Open 2020 – 1/5 - Results
Submission details
Task:Vaihdot
Sender:Metabolix
Submission time:2020-09-06 12:50:27 +0300
Language:C++ (C++11)
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
#30
Test results
testverdicttimegroup
#10.01 s1, 2, 3details
#2ACCEPTED0.01 s1, 2, 3details
#3ACCEPTED0.01 s1, 2, 3details
#4ACCEPTED0.01 s1, 2, 3details
#5ACCEPTED0.01 s1, 2, 3details
#6ACCEPTED0.01 s1, 2, 3details
#7ACCEPTED0.01 s1, 2, 3details
#8ACCEPTED0.01 s1, 2, 3details
#90.03 s2, 3details
#10ACCEPTED0.03 s2, 3details
#11ACCEPTED0.03 s2, 3details
#12ACCEPTED0.03 s2, 3details
#13ACCEPTED0.03 s2, 3details
#14ACCEPTED0.03 s2, 3details
#15ACCEPTED0.03 s2, 3details
#16ACCEPTED0.04 s2, 3details
#170.67 s3details
#180.66 s3details
#19ACCEPTED0.71 s3details
#20ACCEPTED0.72 s3details
#21ACCEPTED0.76 s3details
#22ACCEPTED0.98 s3details
#23--3details
#24--3details

Code

#include <iostream>
#include <unordered_set>

#define MAX 200000
#define MAX_POW2 262144
int n, L[MAX+4], Li[MAX+4], oikea[MAX+4];

int matka_4(int a, int b, int c, int d) {
	if (a < b && b < c && c < d) return 1;
	if ((a < b && b < c) || (b < c && c < d) || (a < b && c < d)) return 2;
	if (a < b || b < c || c < d) return 3;
	return 4;
}

int vaihtamalla_paranee(int a, int b1, int b2, int c) {
	if (a < b1 && b1 < c) {
		if (a < b2 && b2 < c) return 0;
		if (a < b2 || b2 < c) return -1;
		return -2;
	}
	if (a < b1 || b1 < c) {
		if (a < b2 && b2 < c) return 1;
		if (a < b2 || b2 < c) return 0;
		return -1;
	}
	if (a < b2 && b2 < c) return 2;
	if (a < b2 || b2 < c) return 1;
	return 0;
}

struct tulos {
	int kierroksia0 = 1, parannus = -3, tapoja = 0;
	void uusi(int p, int n = 1) {
		if (n <= 0) {
			return;
		} else if (p > parannus) {
			parannus = p;
			tapoja = n;
		} else if (p == parannus) {
			tapoja += n;
		}
	}
};

struct magic {
	std::unordered_set<int> data;
	int count_tree[MAX_POW2 * 2] = {0};
	void insert(int i) {
		if (data.insert(i).second) {
			update_count(Li[i], 1);
		}
	}
	int erase(int i) {
		if (data.erase(i)) {
			update_count(Li[i], -1);
			return 1;
		}
		return 0;
	}
	int count(int i) {
		return data.count(i);
	}
	void update_count(int i, int val) {
		i |= MAX_POW2;
		while (i) {
			count_tree[i] += val;
			i >>= 1;
		}
	}
	int count_interval(int a, int b) {
		return count_interval_(a, b+1, 0, MAX_POW2, 1);
	}
	int count_interval_(int a, int b, int a0, int b0, int node) {
		if (b0 <= a || b <= a0) {
			return 0;
		}
		if (a <= a0 && b0 <= b) {
			return count_tree[node];
		}
		int c0 = (a0 + b0) / 2;
		return count_interval_(a, b, a0, c0, (node << 1)) + count_interval_(a, b, c0, b0, (node << 1) | 1);
	}
};

std::unordered_set<int> oikeat;
magic paranee, sama, huononee;

void aseta_oikea(int i) {
	oikeat.insert(i);
	if (oikea[i]) {
		paranee.erase(i);
		sama.insert(i);
	} else {
		paranee.insert(i);
		sama.erase(i);
	}
	huononee.erase(i);
}

void aseta_vaara(int i) {
	oikeat.erase(i);
	paranee.erase(i);
	if (oikea[i]) {
		sama.erase(i);
		huononee.insert(i);
	} else {
		sama.insert(i);
		huononee.erase(i);
	}
}

void havaittu(int i) {
	if (i + 1 <= n) aseta_oikea(i + 1);
	if (i - 1 >= 1) aseta_vaara(i - 1);
}

bool skip(int i, int j) {
	if (i == j || L[i] == L[j]+1 || L[i] == L[j]-1) return true;
	return false;
}

int aseta_tauolle(int i) {
	return paranee.erase(i) ? 1 : sama.erase(i) ? 0 : huononee.erase(i) ? -1 : -2;
}
void aseta_takaisin(int i, int mihin) {
	switch (mihin) {
		case -1: huononee.insert(i); break;
		case 0: sama.insert(i); break;
		case 1: paranee.insert(i); break;
	}
}

int main() {
	std::cin >> n;
	L[0] = Li[0] = 0;
	L[n+1] = Li[n+1] = n+1;
	L[n+2] = Li[n+2] = n+2;
	oikea[0] = oikea[n+1] = oikea[n+2] = 1;

	for (int i = 1; i <= n; ++i) {
		std::cin >> L[i];
		Li[L[i]] = i;
	}

	// oikeat (set)
	for (int i = 1; i <= n; ++i) {
		havaittu(L[i]);
	}
	aseta_oikea(1);
	aseta_vaara(n);

	// oikea (array)
	for (int i = 1; i <= n; ++i) {
		oikea[L[i]] = oikeat.count(L[i]);
		havaittu(L[i]);
	}
	aseta_oikea(1);
	aseta_vaara(n);

	// paranee/huononee
	for (int i = 1; i <= n; ++i) {
		havaittu(L[i]);
	}
	aseta_oikea(1);
	aseta_vaara(n);

	// ratkaisu.
	tulos t;
	for (int i = 1; i <= n; ++i) {
		if (Li[i+1] < Li[i]) {
			t.kierroksia0 += 1;
		}
	}

	// vaihto i ~ i+1
	for (int i = 1; i < n; ++i) {
		int m0 = matka_4(Li[i - 1], Li[i], Li[i + 1], Li[i + 2]);
		int m1 = matka_4(Li[i - 1], Li[i + 1], Li[i], Li[i + 2]);
		t.uusi(m0 - m1);
	}
	t.tapoja *= 2;

	// muut vaihdot
	for (int i = 1; i <= n; ++i) {
		int i_luku = L[i];
		int a = std::min(Li[i_luku - 1], Li[i_luku + 1]);
		int b = std::max(Li[i_luku - 1], Li[i_luku + 1]);
		int muutos_0a = vaihtamalla_paranee(Li[i_luku - 1], i, a-1, Li[i_luku + 1]);
		int muutos_ab = vaihtamalla_paranee(Li[i_luku - 1], i, a+1, Li[i_luku + 1]);
		int muutos_bn = vaihtamalla_paranee(Li[i_luku - 1], i, b+1, Li[i_luku + 1]);

		std::pair<int, int> tauot[] = {
			{ a, aseta_tauolle(L[a]) },
			{ b, aseta_tauolle(L[b]) }
		};

		t.uusi(1 + muutos_0a, paranee.count_interval(1, a));
		t.uusi(1 + muutos_ab, paranee.count_interval(a, b));
		t.uusi(1 + muutos_bn, paranee.count_interval(b, n));

		t.uusi(0 + muutos_0a, sama.count_interval(1, a));
		t.uusi(0 + muutos_ab, sama.count_interval(a, b));
		t.uusi(0 + muutos_bn, sama.count_interval(b, n));

		t.uusi(-1 + muutos_0a, huononee.count_interval(1, a));
		t.uusi(-1 + muutos_ab, huononee.count_interval(a, b));
		t.uusi(-1 + muutos_bn, huononee.count_interval(b, n));

		for (auto x: tauot) {
			aseta_takaisin(L[x.first], x.second);
		}

		havaittu(L[i]);
	}

	printf("%d %d\n", t.kierroksia0 - t.parannus, t.tapoja / 2);
}

Test details

Test 1

Group: 1, 2, 3

Verdict:

input
100
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
2 99

user output
1 50

Test 2

Group: 1, 2, 3

Verdict: ACCEPTED

input
100
100 99 98 97 96 95 94 93 92 91...

correct output
98 4851

user output
98 4851

Test 3

Group: 1, 2, 3

Verdict: ACCEPTED

input
100
1 2 88 4 5 6 7 8 9 10 11 12 13...

correct output
16 5

user output
16 5

Test 4

Group: 1, 2, 3

Verdict: ACCEPTED

input
100
51 48 74 70 45 71 24 88 55 99 ...

correct output
49 131

user output
49 131

Test 5

Group: 1, 2, 3

Verdict: ACCEPTED

input
100
45 67 29 62 70 77 41 74 52 95 ...

correct output
52 268

user output
52 268

Test 6

Group: 1, 2, 3

Verdict: ACCEPTED

input
100
47 98 2 75 6 21 84 8 4 89 27 9...

correct output
48 149

user output
48 149

Test 7

Group: 1, 2, 3

Verdict: ACCEPTED

input
100
73 68 17 94 71 63 61 13 58 10 ...

correct output
47 116

user output
47 116

Test 8

Group: 1, 2, 3

Verdict: ACCEPTED

input
100
17 16 45 94 6 1 36 81 31 13 51...

correct output
45 95

user output
45 95

Test 9

Group: 2, 3

Verdict:

input
5000
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
2 4999

user output
1 2500

Test 10

Group: 2, 3

Verdict: ACCEPTED

input
5000
5000 4999 4998 4997 4996 4995 ...

correct output
4998 12492501

user output
4998 12492501

Test 11

Group: 2, 3

Verdict: ACCEPTED

input
5000
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
19 10

user output
19 10

Test 12

Group: 2, 3

Verdict: ACCEPTED

input
5000
1 2 3 4 5 6 264 8 9 10 11 12 1...

correct output
190 96

user output
190 96

Test 13

Group: 2, 3

Verdict: ACCEPTED

input
5000
1 2 3 4 5 6 7 8 9 2400 11 12 1...

correct output
1378 27938

user output
1378 27938

Test 14

Group: 2, 3

Verdict: ACCEPTED

input
5000
4012 2 4820 4208 1868 1728 362...

correct output
2511 436307

user output
2511 436307

Test 15

Group: 2, 3

Verdict: ACCEPTED

input
5000
3877 3972 1112 3669 1959 4640 ...

correct output
2497 417065

user output
2497 417065

Test 16

Group: 2, 3

Verdict: ACCEPTED

input
5000
2774 998 4525 2884 487 1995 41...

correct output
2518 426448

user output
2518 426448

Test 17

Group: 3

Verdict:

input
200000
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
2 199999

user output
1 100000

Test 18

Group: 3

Verdict:

input
200000
200000 199999 199998 199997 19...

correct output
199998 19999700001

user output
199998 672347169

Test 19

Group: 3

Verdict: ACCEPTED

input
200000
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
19 10

user output
19 10

Test 20

Group: 3

Verdict: ACCEPTED

input
200000
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
199 100

user output
199 100

Test 21

Group: 3

Verdict: ACCEPTED

input
200000
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
1979 1030

user output
1979 1030

Test 22

Group: 3

Verdict: ACCEPTED

input
200000
1 2 184153 4 5 6 7 8 164545 10...

correct output
18081 477187

user output
18081 477187

Test 23

Group: 3

Verdict:

input
200000
151013 68675 119105 58292 3335...

correct output
86328 318722426

user output
(empty)

Test 24

Group: 3

Verdict:

input
200000
11562 33356 106752 170825 2723...

correct output
99873 663048119

user output
(empty)