답안 #894577

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
894577 2023-12-28T13:11:31 Z lunaTu Xylophone (JOI18_xylophone) C++17
0 / 100
1 ms 344 KB
#include <bits/stdc++.h>
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
#include "xylophone.h"
typedef long long ll;
using namespace std;

void solve(int N) {

    vector<int> a(N + 1), u(2 * N + 1);

    int l = 1, r = N + 1;
    while (r - l > 1) {
        int m = (l + r) / 2;

        int get = 0;
        if (m < N) get = query(m, N);

        if (get != N - 1) r = m;
        else l = m;
    }

    answer(l, 1);
    a[l] = 1;
    u[1] = 1;
    if (l != N) {
        a[l + 1] = 1 + query(l, l + 1);
        answer(l + 1, a[l + 1]);
        u[a[l + 1]] = 1;
    }

    for (int i = l + 2; i <= N; i++) {
        int x = query(i - 2, i);

        int r = abs(a[i - 1] - a[i - 2]) - x;

        if (a[i - 1] < a[i - 2]) {
            if (r == 0) {
                int y = -1;
                for (int j = a[i - 1] + 1; j < a[i - 2]; j++) {
                    if (!u[j]) {
                        if (y != -1) {
                            y = -1;
                            break;
                        }
                        else y = j;
                    }
                }

                if (y == -1) y = a[i - 1] + query(i, i + 1);
                a[i] = y;
            }
            else {
                if (u[a[i - 2] - x]) a[i] = a[i - 1] + x;
                else if (u[a[i - 1] + x]) a[i] = a[i - 2] - x;
                else {
                    int y = query(i - 1, i);
                    if (y == x) a[i] = a[i - 1] + x;
                    else a[i] = a[i - 2] - x;
                }
            }
        }
        else {
            if (r == 0) {
                int y = -1;
                for (int j = a[i - 2] + 1; j < a[i - 1]; j++) {
                    if (!u[j]) {
                        if (y != -1) {
                            y = -1;
                            break;
                        }
                        else y = j;
                    }
                }

                if (y == -1) y = a[i - 1] - query(i - 1, i);
                a[i] = y;
            }
            else {
                if (u[a[i - 1] - x]) a[i] = a[i - 2] + x;
                else if (u[a[i - 2] + x]) a[i] = a[i - 1] - x;
                else {
                    int y = query(i - 1, i);
                    if (y == x) a[i] = a[i - 1] - x;
                    else a[i] = a[i - 2] + x;
                }
            }
        }

        answer(i, a[i]);
        u[a[i]] = 1;
    }

    if (l != 1) {
        a[l - 1] = 1 + query(l - 1, l);
        answer(l - 1, a[l - 1]);
		u[a[l - 1]] = 1;
    }

    for (int i = l - 2; i >= 1; i--) {
        int x = query(i, i + 2);
        int r = abs(a[i + 1] - a[i + 2]) - x;

        if (a[i + 1] < a[i + 2]) {
            if (r == 0) {

                int y = -1;
                for (int j = a[i + 1] + 1; j < a[i + 2]; j++) {
                    if (!u[j]) {
                        if (y != -1) {
                            y = -1;
                            break;
                        }
                        else y = j;
                    }
                }

                if (y == -1) y = a[i + 1] + query(i, i + 1);
                a[i] = y;
            }
            else {
                if (u[a[i + 2] - x]) a[i] = a[i + 1] + x;
                else if (u[a[i + 1] + x]) a[i] = a[i + 2] - x;
                else {
                    int y = query(i, i + 1);
                    if (y == x) a[i] = a[i + 1] + x;
                    else a[i] = a[i + 2] - x;
                }
            }
        }
        else {
            if (r == 0) {
                int y = -1;
                for (int j = a[i + 2] + 1; j < a[i + 1]; j++) {
                    if (!u[j]) {
                        if (y != -1) {
                            y = -1;
                            break;
                        }
                        else y = j;
                    }
                }

                if (y == -1) y = a[i + 1] - query(i, i + 1);
                a[i] = y;
            }
            else {
                if (u[a[i + 1] - x]) a[i] = a[i + 2] + x;
                else if (u[a[i + 2] + x]) a[i] = a[i + 1] - x;
                else {
                    int y = query(i, i + 1);
                    if (y == x) a[i] = a[i + 1] - x;
                    else a[i] = a[i + 2] + x;
                }
            }
        }

        answer(i, a[i]);
        u[a[i]] = 1;
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Incorrect 1 ms 344 KB Wrong Answer [7]
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Incorrect 1 ms 344 KB Wrong Answer [7]
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Incorrect 1 ms 344 KB Wrong Answer [7]
4 Halted 0 ms 0 KB -