제출 #199047

#제출 시각아이디문제언어결과실행 시간메모리
199047stefdascaSwap (BOI16_swap)C++14
100 / 100
67 ms5752 KiB
#include<bits/stdc++.h>
#define god dimasi5eks
#pragma GCC optimize("O3")
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define mod 1000000007
#define dancila 3.14159265359
#define eps 1e-9

// #define fisier 1

using namespace std;

typedef long long ll;

int n, v[400002], t[400002], ans[400002];

int get_value(int poz)
{
    int sol = (1<<30);
    for(; poz; poz >>= 1)
    {
        if(t[poz] != 1)
            sol = min(sol, v[poz]);
        if(t[poz] == 0)
            break;
        if((poz & 1) && t[poz ^ 1] != 0)
        {
            sol = min(sol, v[poz ^ 1]);
            if(t[poz ^ 1] == 1)
                break;
        }
    }
    return sol;
}
void set_value(int poz, int val)
{
    for(; poz; poz >>= 1)
    {
        if(v[poz] == val)
        {
            t[poz] = 0;
            break;
        }
        if(poz & 1)
        {
			if(v[poz ^ 1] == val)
			{
				t[poz] = t[poz ^ 1] = 1;
				break;
			}
            else
                t[poz ^ 1] = 0;
		}
		t[poz] = 1;
    }
}
int main()
{

    #ifdef fisier
        ifstream f("input.in");
        ofstream g("output.out");
    #endif

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n;
    for(int i = 1; i <= n; ++i)
        cin >> v[i], t[i] = 2;
    t[1] = 0;
    for(int i = n+1; i <= 2*n+1; ++i)
        v[i] = (1<<30);
    for(int i = 1; i <= n; ++i)
    {
        int val = get_value(i);
        ans[i] = min(val, min(v[i << 1], v[i << 1|1]));
        if(ans[i] == val)
        {
            t[i << 1] = t[i << 1|1] = 0;
            set_value(i, ans[i]);
        }
        if(ans[i] == v[i << 1])
            t[i << 1] = 1, t[i << 1|1] = 0;
        if(ans[i] == v[i << 1|1])
            t[i << 1|1] = 1;
    }
    for(int i = 1; i <= n; ++i)
        cout << ans[i] << " ";
    return 0;
}
#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...