CSES - Putka Open 2020 – 1/5 - Results
Submission details
Task:Vaihdot
Sender:hltk
Submission time:2020-09-05 12:12:18 +0300
Language:C++ (C++17)
Status:READY
Result:100
Feedback
groupverdictscore
#1ACCEPTED13
#2ACCEPTED23
#3ACCEPTED64
Test results
testverdicttimegroup
#1ACCEPTED0.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
#9ACCEPTED0.01 s2, 3details
#10ACCEPTED0.01 s2, 3details
#11ACCEPTED0.01 s2, 3details
#12ACCEPTED0.01 s2, 3details
#13ACCEPTED0.02 s2, 3details
#14ACCEPTED0.02 s2, 3details
#15ACCEPTED0.02 s2, 3details
#16ACCEPTED0.02 s2, 3details
#17ACCEPTED0.26 s3details
#18ACCEPTED0.26 s3details
#19ACCEPTED0.26 s3details
#20ACCEPTED0.26 s3details
#21ACCEPTED0.27 s3details
#22ACCEPTED0.34 s3details
#23ACCEPTED0.77 s3details
#24ACCEPTED0.90 s3details

Code

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct Tree {
	int n;
	vector<int> p;
	void init(int z) {
		n = 1;
		while (n <= z)
			n *= 2;
		p.resize(n * 2);
	}
	void add(int k, int x) {
		k += n;
		p[k] += x;
		while (k /= 2)
			p[k] = p[k*2] + p[k*2+1];
	}
	int sum(int a, int b) {
		int r = 0;
		for (a += n, b += n; a <= b; a /= 2, b /= 2) {
			if (a % 2 == 1)
				r += p[a++];
			if (b % 2 == 0)
				r += p[b--];
		}
		return r;
	}
};
int main() {
	ios::sync_with_stdio(false);
 	cin.tie(nullptr);
	int n;
	cin >> n;
	vector<int> v(n);
	for (auto& x : v)
		cin >> x, --x;
	vector<int> pos(n);
	for (int i = 0; i < n; ++i)
		pos[v[i]] = i;
	auto calc_pos = [&](int x) {
		return (x < 0 || x+1 >= n) ? 0 : int(pos[x] > pos[x+1]);
	};
	int current_moves = 1;
	for (int i = 0; i < n; ++i)
		current_moves += calc_pos(i);
	auto test_swap = [&](int a, int b) {
		vector<int> L{v[a], v[a]-1, v[b], v[b]-1};
		sort(begin(L), end(L));
		L.erase(unique(begin(L), end(L)), end(L));
		int moves = 0;
		for (int u : L) moves -= calc_pos(u);
		swap(pos[v[a]], pos[v[b]]);
		for (int u : L) moves += calc_pos(u);
		swap(pos[v[a]], pos[v[b]]);
		return moves;
	};
	auto test_swap_A = [&](int a, int b) {
		vector<int> L{v[a], v[a]-1};
		int moves = 0;
		for (int u : L) moves -= calc_pos(u);
		swap(pos[v[a]], pos[v[b]]);
		for (int u : L) moves += calc_pos(u);
		swap(pos[v[a]], pos[v[b]]);
		return moves;
	};
	map<int, ll> scores;
	map<int, Tree> amt;
	amt[-1].init(n);
	amt[0].init(n);
	amt[1].init(n);
	vector<vector<array<int, 2>>> TA(n);
	vector<vector<array<int, 2>>> TR(n+1);
	for (int i = 0; i < n; ++i) {
		for (auto [j, p] : TA[i]) {
			amt[p].add(j, 1);
		}
		for (auto [j, p] : TR[i]) {
			amt[p].add(j, -1);
		}
		int c = test_swap_A(i, i);
		vector<int> points{-1, n, i};
		if (v[i] > 0) {
			points.push_back(pos[v[i]-1]);
			scores[test_swap(i, pos[v[i]-1])-c]++;
		}
		if (v[i]+1 < n)
			points.push_back(pos[v[i]+1]);
		sort(begin(points), end(points));
		for (int j = 0; j+1 < int(points.size()); ++j) {
			int l = points[j]+1, r = points[j+1]-1;
			if (r < l)
				continue;
			int p = test_swap_A(i, l) - c;
			TA[l].push_back({i, p});
			TR[r+1].push_back({i, p});
			for (int k = -1; k <= 1; ++k) {
				int z = amt[k].sum(l, r);
				if (z) {
					scores[k+p] += z;
				}
			}
		}
	}
	auto [x, y] = *scores.begin();
	cout << current_moves+x << ' ' << y << '\n';
}

Test details

Test 1

Group: 1, 2, 3

Verdict: ACCEPTED

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

correct output
2 99

user output
2 99

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: ACCEPTED

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

correct output
2 4999

user output
2 4999

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: ACCEPTED

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

correct output
2 199999

user output
2 199999

Test 18

Group: 3

Verdict: ACCEPTED

input
200000
200000 199999 199998 199997 19...

correct output
199998 19999700001

user output
199998 19999700001

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: ACCEPTED

input
200000
151013 68675 119105 58292 3335...

correct output
86328 318722426

user output
86328 318722426

Test 24

Group: 3

Verdict: ACCEPTED

input
200000
11562 33356 106752 170825 2723...

correct output
99873 663048119

user output
99873 663048119