CSES - Putka Open 2020 – 1/5 - Results
Submission details
Task:Vaihdot
Sender:Gomhog
Submission time:2020-09-05 12:01:34 +0300
Language:C++ (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.13 s3details
#18ACCEPTED0.13 s3details
#19ACCEPTED0.14 s3details
#20ACCEPTED0.15 s3details
#21ACCEPTED0.15 s3details
#22ACCEPTED0.18 s3details
#23ACCEPTED0.34 s3details
#24ACCEPTED0.38 s3details

Code

#include <bits/stdc++.h>
#define F first
#define S second
#define X real()
#define Y imag()
using namespace std;
typedef long long ll;
typedef long double ld;

pair<int,int> re[201010];
int re2[201010];
int xx[201010];
int INF = 999999;
const int N = 1<<18;
ll sTr[3][2*N];
ll cntr[5];

void change(int ind, ll am, int a) {
	ind += N;
	while (ind > 0) {
		sTr[a][ind]+=am;
		ind /= 2;
	}
}

ll query(int a, int lo, int hi) {
	lo += N;
	hi += N;
	ll res = 0;
	while (lo <= hi) {
		if (lo%2 == 1) res += sTr[a][lo++];
		if (hi%2 == 0) res += sTr[a][hi--];
		lo /= 2;
		hi /= 2;
	}
	return res;
}

int main() {
	ios_base::sync_with_stdio(0);	
	cin.tie(0);
	int n;
	cin>>n;
	for (int i=0;i<n;i++) {
		int j;
		cin>>j;
		re[i]= {j,i};
		re2[i]=j;
	}
	sort(re,re+n);
	for (int i=0;i<n;i++) xx[i]=re[i].S;
	int parsa=INF;
	int paCnt=0;
	for (int i=0;i+1<n;i++) {
		if (xx[i]>xx[i+1]) paCnt++;
	}
	ll parsacnt=0;
	for (int i=0;i+2<n;i++) {
		int tst=paCnt;
		if (i>0) {
			if (xx[i-1]>xx[i] && xx[i-1]<xx[n-1]) tst--;
			if (xx[i-1]<xx[i] && xx[i-1]>xx[n-1]) tst++;
		}
		if (xx[i]>xx[i+1] && xx[n-1]<xx[i+1]) tst--;
		if (xx[i]<xx[i+1] && xx[n-1]>xx[i+1]) tst++;
		if (xx[n-2]>xx[n-1] && xx[n-2]<xx[i]) tst--;
		if (xx[n-2]<xx[n-1] && xx[n-2]>xx[i]) tst++;
		if (tst < parsa) {
			parsa = tst;
			parsacnt = 1;
		} else if (tst == parsa) parsacnt++;
	}
	for (int i=2;i+1<n;i++) {
		int tst=paCnt;
		if (xx[i]>xx[i+1] && xx[0]<xx[i+1]) tst--;
		if (xx[i]<xx[i+1] && xx[0]>xx[i+1]) tst++;
		if (xx[0]>xx[1] && xx[i]<xx[1]) tst--;
		if (xx[0]<xx[1] && xx[i]>xx[1]) tst++;
		if (xx[i-1]>xx[i] && xx[i-1]<xx[0]) tst--;
		if (xx[i-1]<xx[i] && xx[i-1]>xx[0]) tst++;
		if (tst < parsa) {
			parsa = tst;
			parsacnt = 1;
		} else if (tst == parsa) parsacnt++;
	}
	for (int i=0;i+1<n;i++) {
		int tst=paCnt;
		if (i>0) {
			if (xx[i-1]>xx[i] && xx[i-1]<xx[i+1]) tst--;
			if (xx[i-1]<xx[i] && xx[i-1]>xx[i+1]) tst++;
		}
		if (i+2<n) {
			if (xx[i+1]>xx[i+2] && xx[i]<xx[i+2]) tst--;
			if (xx[i+1]<xx[i+2] && xx[i]>xx[i+2]) tst++;
		}
		if (xx[i]>xx[i+1]) tst--;
		else tst++;
		if (tst < parsa) {
			parsa = tst;
			parsacnt = 1;
		} else if (tst == parsa) parsacnt++;
	}
	vector<pair<int,pair<int,int>>> act,deact;
	for (int j=1;j<n-1;j++) {
		if (xx[j-1]<xx[j]) {
			if (xx[j]<xx[j+1]) {
				if (0<xx[j-1]) {
					act.push_back({0,{2,j}});
					deact.push_back({xx[j-1],{2,j}});
				}
				if (xx[j-1]+1 < xx[j]) {
					act.push_back({xx[j-1]+1,{1,j}});
					deact.push_back({xx[j],{1,j}});
				}
			} else {
				if (xx[j-1]<xx[j+1]) {
					if (0<xx[j-1]) {
						act.push_back({0,{1,j}});
						deact.push_back({xx[j-1],{1,j}});
					}
					if (xx[j-1]+1<xx[j+1]) {
						act.push_back({xx[j-1]+1,{0,j}});
						deact.push_back({xx[j+1],{0,j}});
					}
					if (xx[j+1]+1<xx[j]) {
						act.push_back({xx[j+1]+1,{1,j}});
						deact.push_back({xx[j],{1,j}});
					}
				} else {
					if (0<xx[j+1]) {
						act.push_back({0,{1,j}});
						deact.push_back({xx[j+1],{1,j}});
					}
					if (xx[j+1]+1<xx[j-1]) {
						act.push_back({xx[j+1]+1,{2,j}});
						deact.push_back({xx[j-1],{2,j}});
					}
					if (xx[j-1]+1<xx[j]) {
						act.push_back({xx[j-1]+1,{1,j}});
						deact.push_back({xx[j],{1,j}});
					}
				}
			}
		} else {
			if (xx[j]>xx[j+1]) {
				if (0<xx[j+1]) {
					act.push_back({0,{0,j}});
					deact.push_back({xx[j+1],{0,j}});
				}
				if (xx[j+1]+1<xx[j]) {
					act.push_back({xx[j+1]+1,{1,j}});
					deact.push_back({xx[j],{1,j}});
				}
			} else {
				if (0<xx[j]) {
					act.push_back({0,{1,j}});
					deact.push_back({xx[j],{1,j}});
				}
			}
		}
	}
	sort(act.begin(),act.end());
	sort(deact.begin(),deact.end());
	int kk = act.size();
	int ptr1 = 0;
	int ptr2 = 0;
	for (int i = 0; i<n; i++) {
		while (ptr1 < kk && act[ptr1].F<=i) {
			change(xx[act[ptr1].S.S],1,act[ptr1].S.F);
			ptr1++;
		}
		while (ptr2 < kk && deact[ptr2].F<=i) {
			change(xx[deact[ptr2].S.S],-1,deact[ptr2].S.F);
			ptr2++;
		}
		int j = re2[i];
		j--;
		if (j==0 || j==n-1) continue;
		if (xx[j-1]<xx[j] && xx[j]<xx[j+1]) {
//			cerr<<"1: ";
			for (int a=0;a<=2;a++) {
				cntr[1+a]+=query(a,xx[j]+1,xx[j+1]-1);
				cntr[2+a]+=query(a,xx[j+1]+1,n-1);
			}
		} else if (xx[j-1]<xx[j+1] && xx[j+1]<xx[j]) {
//			cerr<<"2: ";
			for (int a=0;a<=2;a++) {
				cntr[1+a]+=query(a,xx[j]+1,n-1);
			}
		} else if (xx[j]<xx[j-1] && xx[j-1]<xx[j+1]) {
//			cerr<<"3: ";
			for (int a=0;a<=2;a++) {
				cntr[1+a]+=query(a,xx[j]+1,xx[j-1]-1);
				cntr[a]+=query(a,xx[j-1]+1,xx[j+1]-1);
				cntr[1+a]+=query(a,xx[j+1]+1,n-1);
			}
		} else if (xx[j]<xx[j+1] && xx[j+1]<xx[j-1]) {
//			cerr<<"4: ";
			for (int a=0;a<=2;a++) {
				cntr[1+a]+=query(a,xx[j]+1,xx[j+1]-1);
				cntr[2+a]+=query(a,xx[j+1]+1,xx[j-1]-1);
				cntr[1+a]+=query(a,xx[j-1]+1,n-1);
			}
		} else if (xx[j+1]<xx[j] && xx[j]<xx[j-1]) {
//			cerr<<"5: ";
			for (int a=0;a<=2;a++) {
				cntr[1+a]+=query(a,xx[j]+1,xx[j-1]-1);
				cntr[a]+=query(a,xx[j-1]+1,n-1);
			}
		} else {
//			cerr<<"6: ";
			for (int a=0;a<=2;a++) {
				cntr[1+a]+=query(a,xx[j]+1,n-1);
			}
		}
//		cerr<<j<<" "<<xx[j-1]<<" "<<xx[j]<<" "<<xx[j+1]<<endl;
//		for (int a = 0; a<=4;a++) cerr<<cntr[a]<<" ";
//		cerr<<endl;
	}
	for (int i=0;i<=4;i++) {
		if (cntr[i]>0 && paCnt-2+i < parsa) {
			cout<<paCnt-1+i<<" "<<cntr[i]<<"\n";
			return 0;
		} else if (paCnt-2+i == parsa) {
			cout << parsa+1 << " " <<parsacnt+cntr[i]<<"\n";
			return 0;
		}
	}
}

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