Submission #122993

# Submission time Handle Problem Language Result Execution time Memory
122993 2019-06-29T23:11:03 Z thebes Swap (BOI16_swap) C++14
100 / 100
751 ms 3704 KB
#include <bits/stdc++.h>
using namespace std;

const int MN = 2e5+5;
int arr[MN], n, i;
int rec(int p,int v){
    if(2*p>n) return p;
    else if(2*p+1>n) return v<arr[2*p]?p:2*p;
    if(v<arr[2*p]&&v<arr[2*p+1]) return p;
    if(arr[2*p]<v&&arr[2*p]<arr[2*p+1]) return rec(2*p,v);
    if(arr[2*p+1]<v&&arr[2*p+1]<arr[2*p]){
        if(v<arr[2*p]) return min(rec(2*p,v),rec(2*p+1,v));
        else if(rec(2*p,arr[2*p])<rec(2*p+1,arr[2*p])) return rec(2*p+1,v);
        else return rec(2*p,v);
    }
}
int main(){
    for(scanf("%d",&n),i=1;i<=n;i++)
        scanf("%d",&arr[i]);
    for(i=1;i<=n;i++){
        if(2*i>n) continue;
        if(2*i+1>n){
            if(arr[2*i]<arr[i]) swap(arr[2*i],arr[i]);
        }
        else{
            if(arr[2*i+1]<arr[i]&&arr[2*i+1]<arr[2*i]){
                int a = min(arr[i],arr[2*i]);
                int b = max(arr[i],arr[2*i]);
                arr[i] = arr[2*i+1];
                if(rec(2*i,a)<rec(2*i+1,a)) arr[2*i]=a,arr[2*i+1]=b;
                else arr[2*i+1]=a,arr[2*i]=b;
            }
            else if(arr[2*i]<arr[i]&&arr[2*i]<arr[2*i+1]) swap(arr[i],arr[2*i]);
        }
    }
    for(i=1;i<=n;i++)
        printf("%d ",arr[i]);
    return 0;
}

Compilation message

swap.cpp: In function 'int rec(int, int)':
swap.cpp:16:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
swap.cpp: In function 'int main()':
swap.cpp:18:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(scanf("%d",&n),i=1;i<=n;i++)
         ~~~~~~~~~~~~~~^~~~
swap.cpp:19:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d",&arr[i]);
         ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 256 KB Output is correct
14 Correct 3 ms 376 KB Output is correct
15 Correct 2 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 256 KB Output is correct
14 Correct 3 ms 376 KB Output is correct
15 Correct 2 ms 256 KB Output is correct
16 Correct 14 ms 1144 KB Output is correct
17 Correct 14 ms 1020 KB Output is correct
18 Correct 14 ms 1036 KB Output is correct
19 Correct 91 ms 1128 KB Output is correct
20 Correct 87 ms 1144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 256 KB Output is correct
14 Correct 3 ms 376 KB Output is correct
15 Correct 2 ms 256 KB Output is correct
16 Correct 14 ms 1144 KB Output is correct
17 Correct 14 ms 1020 KB Output is correct
18 Correct 14 ms 1036 KB Output is correct
19 Correct 91 ms 1128 KB Output is correct
20 Correct 87 ms 1144 KB Output is correct
21 Correct 55 ms 3676 KB Output is correct
22 Correct 54 ms 3576 KB Output is correct
23 Correct 55 ms 3664 KB Output is correct
24 Correct 751 ms 3704 KB Output is correct
25 Correct 749 ms 3704 KB Output is correct