CSES - Putka Open 2020 – 1/5 - Results
Submission details
Task:Vaihdot
Sender:mango_lassi
Submission time:2020-09-04 23:56:56 +0300
Language:C++11
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.01 s2, 3details
#14ACCEPTED0.01 s2, 3details
#15ACCEPTED0.01 s2, 3details
#16ACCEPTED0.01 s2, 3details
#17ACCEPTED0.25 s3details
#18ACCEPTED0.25 s3details
#19ACCEPTED0.26 s3details
#20ACCEPTED0.26 s3details
#21ACCEPTED0.26 s3details
#22ACCEPTED0.28 s3details
#23ACCEPTED0.34 s3details
#24ACCEPTED0.34 s3details

Compiler report

input/code.cpp: In constructor 'SegTree::SegTree(const std::vector<int>&, const std::vector<int>&)':
input/code.cpp:47:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    while(h < vals.size()) h *= 2;
          ~~^~~~~~~~~~~~~
input/code.cpp:50:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for (int i = 0; i < vals.size(); ++i) seg[i+h] = {vals[i], weights[i]};
                    ~~^~~~~~~~~~~~~

Code

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;
const int INF = (int)1e9 + 7;


// Segment tree for range minimum, count of minimum and range addition.
class SegTree {
	private:
		vector<pair<int, int>> seg;
		vector<int> tag;
		int h = 1;

		static pair<int, int> combine(int x, const pair<int, int>& a, const pair<int, int>& b) {
			auto res = min(a, b);
			res.first += x;
			if (a.first == b.first) res.second += max(a, b).second;
			return res;
		}

		void apply(int i, int v) {
			seg[i].first += v;
			if (i < h) tag[i] += v;
		}
		pair<int, int> recGet(int a, int b, int i, int ia, int ib) const {
			if (ib <= a || b <= ia) return {INF, 0};
			if (a <= ia && ib <= b) return seg[i];
			int im = (ia + ib) >> 1;
			return combine(tag[i],
				recGet(a, b, 2*i, ia, im),
				recGet(a, b, 2*i+1, im, ib));
		}
		void recApply(int a, int b, int v, int i, int ia, int ib) {
			if (ib <= a || b <= ia) return;
			if (a <= ia && ib <= b) apply(i, v);
			else {
				int im = (ia + ib) >> 1;
				recApply(a, b, v, 2*i, ia, im);
				recApply(a, b, v, 2*i+1, im, ib);
				seg[i] = combine(tag[i], seg[2*i], seg[2*i+1]);
			}
		}
	public:
		SegTree(const vector<int>& vals, const vector<int>& weights) {
			while(h < vals.size()) h *= 2;
			seg.resize(2*h, {0, 0});
			tag.resize(h, 0);
			for (int i = 0; i < vals.size(); ++i) seg[i+h] = {vals[i], weights[i]};
			for (int i = h-1; i > 0; --i) seg[i] = combine(0, seg[2*i], seg[2*i+1]);
		}
		pair<int, int> get(int a, int b) const { return recGet(a, b+1, 1, 0, h); }
		void add(int a, int b, int v) { recApply(a, b+1, v, 1, 0, h); }
};

void increment(pair<int, ll>& res, pair<int, int> add) {
	if (add.first < res.first) {
		res.first = add.first;
		res.second = add.second;
	} else if (add.first == res.first) res.second += add.second;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	int n;
	cin >> n;
	vector<int> perm(n), inv(n);
	for (int i = 0; i < n; ++i) {
		cin >> perm[i];
		--perm[i];
		inv[perm[i]] = i;
	}

	int base_cost = 1;
	vector<int> bases(n, 0);
	for (int i = 0; i < n; ++i) {
		if (i > 0) {
			if (inv[i-1] < inv[i]) ++bases[inv[i]];
			else ++base_cost;
		}
		if (i+1 < n && inv[i+1] < inv[i]) --bases[inv[i]];
	}

	for (int i = 0; i < n; ++i) bases[i] += base_cost;
	SegTree seg(bases, vector<int>(n, 1));
	
	pair<int, ll> res = {n, 0};
	for (int i = 0; i < n; ++i) {
		// Count swaps with j = perm[i] as the left element
		int j = perm[i];
		if (j > 0 && inv[j-1] > i) seg.add(inv[j-1]+1, n-1, -1);
		if (j+1 < n && inv[j+1] > i) seg.add(inv[j+1]+1, n-1, 1);

		increment(res, seg.get(i+1, n-1));
		/*
		cerr << i << ": ";
		for (int x = 0; x <= i; ++x) cerr << "_ ";
		for (int x = i+1; x < n; ++x) cerr << seg.get(x, x).first << ' '; cerr << '\n';
		*/

		// Undo changes to the segment tree, and handle events
		if (j > 0 && inv[j-1] > i) seg.add(inv[j-1], n-1, 1);
		if (j+1 < n && inv[j+1] > i) seg.add(inv[j+1], n-1, -1);
	}
	cout << res.first << ' ' << res.second << '\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