Submission #122993

#TimeUsernameProblemLanguageResultExecution timeMemory
122993thebesSwap (BOI16_swap)C++14
100 / 100
751 ms3704 KiB
#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 (stderr)

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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...