Submission #565631

#TimeUsernameProblemLanguageResultExecution timeMemory
565631RealSnakeXylophone (JOI18_xylophone)C++14
100 / 100
1439 ms468 KiB
#include "bits/stdc++.h"
using namespace std;
#include "xylophone.h"

int a[5001], b1[5001], b2[5001];

bool sol(int n, int j) {
    a[j] = 1;
    int b[n + 1];
    for(int i = j - 1; i >= 1; i--) {
        int x = b1[i];
        b[i] = x;
        if(i == j - 1) {
            a[i] = x + 1;
            continue;
        }
        int y = b2[i];
        if(a[i + 1] < a[i + 2]) {
            if(a[i + 1] + x < a[i + 2]) {
                if(y == b[i + 1])
                    a[i] = a[i + 1] + x;
                else
                    a[i] = a[i + 1] - x;
            }
            else {
                if(y == a[i + 2] - (a[i + 1] - x))
                    a[i] = a[i + 1] - x;
                else
                    a[i] = a[i + 1] + x;
            }
        }
        else {
            if(a[i + 1] - x > a[i + 2]) {
                if(y == b[i + 1])
                    a[i] = a[i + 1] - x;
                else
                    a[i] = a[i + 1] + x;
            }
            else {
                if(y == (a[i + 1] + x) - a[i + 2])
                    a[i] = a[i + 1] + x;
                else
                    a[i] = a[i + 1] - x;
            }
        }
    }
    for(int i = j + 1; i <= n; i++) {
        int x = b1[i - 1];
        b[i] = x;
        if(i == j + 1) {
            a[i] = x + 1;
            continue;
        }
        int y = b2[i - 2];
        if(a[i - 1] < a[i - 2]) {
            if(a[i - 1] + x < a[i - 2]) {
                if(y == b[i - 1])
                    a[i] = a[i - 1] + x;
                else
                    a[i] = a[i - 1] - x;
            }
            else {
                if(y == a[i - 2] - (a[i - 1] - x))
                    a[i] = a[i - 1] - x;
                else
                    a[i] = a[i - 1] + x;
            }
        }
        else {
            if(a[i - 1] - x > a[i - 2]) {
                if(b[i - 1] == y)
                    a[i] = a[i - 1] - x;
                else
                    a[i] = a[i - 1] + x;
            }
            else {
                if(y == (a[i - 1] + x) - a[i - 2])
                    a[i] = a[i - 1] + x;
                else
                    a[i] = a[i - 1] - x;
            }
        }
    }
    vector<int> ve;
    for(int i = 1; i <= n; i++)
        ve.push_back(a[i]);
    sort(ve.begin(), ve.end());
    for(int i = 1; i <= n; i++) {
        if(ve[i - 1] != i)
            return 0;
    }
    return 1;
}

void solve(int n) {
    for(int i = 1; i <= n; i++) {
        a[i] = 0;
        if(i + 1 <= n)
            b1[i] = query(i, i + 1);
        if(i + 2 <= n)
            b2[i] = query(i, i + 2);
    }
    for(int i = 1; i <= n; i++) {
        if(sol(n, i))
            break;
    }
    for(int i = 1; i <= n; i++)
        answer(i, a[i]);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...