Submission #287843

# Submission time Handle Problem Language Result Execution time Memory
287843 2020-09-01T04:13:28 Z nandonathaniel Swap (BOI16_swap) C++14
100 / 100
970 ms 3832 KB
#include<bits/stdc++.h>
using namespace std;

int n,arr[200005];

int cari(int i,int a){
	if(i*2>n)return 0;
	int d=arr[2*i];
	if(i*2+1>n)return d<a;
	int e=arr[2*i+1];
	if(a<d && a<e)return 0;
	if(d<a && d<e)return 1+cari(2*i,a);
	if(a<d){
		return min(cari(2*i,a),cari(2*i+1,a))+1;
	}
	int l1=cari(2*i,d),l2=cari(2*i+1,d);
	if(l1<=l2)return 1+cari(2*i+1,a);
	else return 1+cari(2*i,a);
}

int main(){
	ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
	cin >> n;
	for(int i=1;i<=n;i++)cin >> arr[i];
	arr[n+1]=1e9;
	for(int i=1;2*i<=n;i++){
		int a=arr[i],b=arr[2*i],c=arr[2*i+1];
		if(a<b && a<c)continue;
		if(b<a && b<c){
			swap(arr[i],arr[2*i]);
			continue;
		}
		if(a>b)swap(a,b);
		if(cari(2*i,a)>cari(2*i+1,a))swap(a,b);
		arr[i]=c;
		arr[2*i]=a;
		arr[2*i+1]=b;
	}
	for(int i=1;i<=n;i++){
		cout << arr[i];
		if(i<n)cout << " ";
		else cout << '\n';
	}
	return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 ms 292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 ms 292 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 ms 292 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 10 ms 1152 KB Output is correct
17 Correct 10 ms 1152 KB Output is correct
18 Correct 11 ms 1152 KB Output is correct
19 Correct 114 ms 1148 KB Output is correct
20 Correct 111 ms 1144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 ms 292 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 10 ms 1152 KB Output is correct
17 Correct 10 ms 1152 KB Output is correct
18 Correct 11 ms 1152 KB Output is correct
19 Correct 114 ms 1148 KB Output is correct
20 Correct 111 ms 1144 KB Output is correct
21 Correct 42 ms 3664 KB Output is correct
22 Correct 43 ms 3704 KB Output is correct
23 Correct 41 ms 3704 KB Output is correct
24 Correct 970 ms 3832 KB Output is correct
25 Correct 966 ms 3832 KB Output is correct