Submission #813218

# Submission time Handle Problem Language Result Execution time Memory
813218 2023-08-07T14:20:06 Z Hakiers Swap (BOI16_swap) C++17
0 / 100
2 ms 4424 KB
#include <iostream>
using namespace std;
const int MAXN = 1 << 19;
const int oo = 1e9;
int t[MAXN];
int res[MAXN];
int tajm;
int n;

void save(){
    bool s = false;
    for(int i = 1; i <= n; i++){

        if(t[i] < res[i]) s = true;
        if(s) res[i] = t[i];
    }
}

void solve(int v){

    if(tajm >= n-1) save();
    if(2*v > n) return;
    int l = 2*v, r = 2*v + 1;

    if(t[v] < t[l] && t[v] < t[r]){
        tajm++; solve(l);
        tajm++; solve(r);
        tajm -= 2;
    }
    else if(t[l] < t[v] && t[v] < t[r]){
        swap(t[v], t[l]);
        tajm++; solve(l);
        tajm++; solve(r);
        tajm -= 2;
        swap(t[v], t[l]);
    }
    else if(t[r] < t[l] && t[l] < t[v]){
        swap(t[v], t[r]);
        tajm++; solve(l);
        tajm++; solve(r);
        tajm -= 2;
        swap(t[v], t[r]);
    }
    else if(t[r] < t[v] && t[v] < t[l]){
        
        //case 1
        swap(t[v], t[l]);
        swap(t[v], t[r]);
        tajm++; solve(l);
        tajm++; solve(r);
        tajm -= 2;
        swap(t[v], t[l]);
        swap(t[v], t[r]);
        
        //case 2
        swap(t[v], t[r]);
        tajm++; solve(l);
        tajm++; solve(r);
        tajm -= 2;
        swap(t[v], t[r]);
        
    }

    
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    for(int i = 1; i < MAXN; i++) res[i] = t[i] = oo;

    for(int i = 1; i <= n; i++)
        cin >> t[i];

    solve(1);

    for(int i = 1; i <= n; i++)
        cout << res[i] << " ";
}
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 4424 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 4424 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 4424 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 4424 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 4424 KB Output isn't correct
2 Halted 0 ms 0 KB -