Submission #374835

# Submission time Handle Problem Language Result Execution time Memory
374835 2021-03-08T10:24:59 Z valerikk Xylophone (JOI18_xylophone) C++17
Compilation error
0 ms 0 KB
/*
    doing virtual contest
    start: 10:00
    finish: 14:00
*/
#include <bits/stdc++.h>

using namespace std;

#ifndef EVAL
const int N_ = 5007;

int n_;
int a_[N_], ans_[N_];

int query(int s, int t) {
    int mn = n_, mx = 1;
    for (int i = s; i <= t; ++i) {
        mn = min(mn, a_[i]);
        mx = max(mx, a_[i]);
    }
    return mx - mn;
}

void answer(int i, int a) {
    if (i < 1 || i > n_) {
        cout << "incorrect index " << i << endl;
        exit(0);
    }
    if (ans_[i] != -1) {
        cout << i << " used twice" << endl;
        exit(0);
    }
    ans_[i] = a;
}

void check_ans() {
    bool corr = 1;
    for (int i = 1; i <= n_; ++i) corr &= a_[i] == ans_[i];
    if (!corr) {
        cout << "incorrect" << endl;
        for (int i = 1; i <= n_; ++i) cout << a_[i] << " ";
        cout << endl;
        for (int i = 1; i <= n_; ++i) cout << ans_[i] << " ";
        cout << endl;
    } else {
        cout << "correct\n";
    }
}

void init(int n, vector<int> a) {
    n_ = n;
    for (int i = 1; i <= n; ++i) {
        a_[i] = a[i - 1];
        ans_[i] = -1;
    }
}
#endif

void solve(int n) {
    int l = 1, r = n;
    while (r - l > 1) {
        int mid = (l + r) / 2;
        if (query(1, mid) == n - 1) {
            r = mid;
        } else {
            l = mid;
        }
    }
    int posn = r;
    l = 1, r = n;
    while (r - l > 1) {
        int mid = (l + r) / 2;
        if (query(mid, n) == n - 1) {
            l = mid;
        } else {
            r = mid;
        }
    }
    int pos1 = l;
    vector<int> a(n + 1, -1);
    a[pos1] = 1;
    a[posn] = n;
    if (pos1 + 1 < n) a[pos1 + 1] = 1 + query(pos1, pos1 + 1);
    for (int i = pos1 - 1; i > 0; --i) {
        int x = a[i + 2], y = a[i + 1];
        int d = query(i, i + 1);
        if (abs(x - y) + d == query(i, i + 2)) {
            a[i] = y < x ? y - d : y + d;
        } else {
            a[i] = y < x ? y + d : y - d;
        }
    }
    for (int i = pos1 + 2; i < posn; ++i) {
        int x = a[i - 2], y = a[i - 1];
        int d = query(i - 1, i);
        if (abs(x - y) + d == query(i - 2, i)) {
            a[i] = y < x ? y - d : y + d;
        } else {
            a[i] = y < x ? y + d : y - d;
        }
    }
    for (int i = posn + 1; i <= n; ++i) {
        int x = a[i - 2], y = a[i - 1];
        int d = query(i - 1, i);
        if (abs(x - y) + d == query(i - 2, i)) {
            a[i] = y < x ? y - d : y + d;
        } else {
            a[i] = y < x ? y + d : y - d;
        }
    }
    for (int i = 1; i <= n; ++i) answer(i, a[i]);
}

#ifndef EVAL
int main() {
    freopen("input.txt", "r", stdin);
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; ++i) cin >> a[i];
    init(n, a);
    solve(n);
    check_ans();
    return 0;
}
#endif

Compilation message

xylophone.cpp: In function 'void solve(int)':
xylophone.cpp:64:13: error: 'query' was not declared in this scope
   64 |         if (query(1, mid) == n - 1) {
      |             ^~~~~
xylophone.cpp:74:13: error: 'query' was not declared in this scope
   74 |         if (query(mid, n) == n - 1) {
      |             ^~~~~
xylophone.cpp:84:41: error: 'query' was not declared in this scope
   84 |     if (pos1 + 1 < n) a[pos1 + 1] = 1 + query(pos1, pos1 + 1);
      |                                         ^~~~~
xylophone.cpp:87:17: error: 'query' was not declared in this scope
   87 |         int d = query(i, i + 1);
      |                 ^~~~~
xylophone.cpp:96:17: error: 'query' was not declared in this scope
   96 |         int d = query(i - 1, i);
      |                 ^~~~~
xylophone.cpp:105:17: error: 'query' was not declared in this scope
  105 |         int d = query(i - 1, i);
      |                 ^~~~~
xylophone.cpp:112:34: error: 'answer' was not declared in this scope
  112 |     for (int i = 1; i <= n; ++i) answer(i, a[i]);
      |                                  ^~~~~~