Submission #160798

# Submission time Handle Problem Language Result Execution time Memory
160798 2019-10-30T02:16:51 Z Leonardo_Paes Xylophone (JOI18_xylophone) C++11
100 / 100
97 ms 548 KB
#include <bits/stdc++.h>
#include "xylophone.h"

using namespace std;

const int maxn = 5e3+10;

int n, res[maxn], pos[maxn];

inline void find_1(){
    int ini=1, fim=n, meio, ans=-1;

    while(ini<=fim){
        meio = (ini+fim) >> 1;
        if(query(meio, n)>=(n-1)){
            ans = meio;
            ini = meio+1;
        }
        else{
            fim = meio-1;
        }
    }

    pos[1] = ans;
    res[ans] = 1;
}

inline void find_n(){
    int ini=pos[1]+1, fim=n, meio, ans=-1;

    while(ini<=fim){
        meio = (ini+fim) >> 1;
        if(query(pos[1], meio)>=(n-1)){
            ans = meio;
            fim = meio-1;
        }
        else{
            ini = meio+1;
        }
    }

    pos[n] = ans;
    res[ans] = n;
}

inline void solve_left(int j, int k){
    if(j<=1 or res[j-1]!=0) return;

    int i = j-1;
    int diff1 = query(i, j);

    if(pos[res[j] + diff1]!=0){
        res[i] = res[j] - diff1;
        pos[res[i]] = i;
    }
    else if(pos[res[j] - diff1]!=0){
        res[i] = res[j] + diff1;
        pos[res[i]] = i;
    }
    else{
        int diff2 = query(i, k);

        if(diff1 + abs(res[j]-res[k]) == diff2){
            if(res[k]<res[j]){
                res[i] = res[j] + diff1;
                pos[res[i]] = i;
            }
            else{
                res[i] = res[j] - diff1;
                pos[res[i]] = i;
            }
        }
        else{
            if(res[k]<res[j]){
                res[i] = res[j] - diff1;
                pos[res[i]] = i;
            }
            else{
                res[i] = res[j] + diff1;
                pos[res[i]] = i;
            }
        }

    }
    solve_left(i, j);
}

inline void solve_right(int j, int k){
    if(k>=n or res[k+1]!=0) return;

    int l = k+1;
    int diff1 = query(k, l);

    if((res[k] + diff1) > n or pos[res[k] + diff1]!=0){
        res[l] = res[k] - diff1;
        pos[res[l]] = l;
    }
    else if((res[k] - diff1) < 1 or pos[res[k] - diff1]!=0){
        res[l] = res[k] + diff1;
        pos[res[l]] = l;
    }
    else{
        int diff2 = query(j, l);

        if(diff1 + abs(res[j]-res[k]) == diff2){
            if(res[k]>res[j]){
                res[l] = res[k] + diff1;
                pos[res[l]] = l;
            }
            else{
                res[l] = res[k] - diff1;
                pos[res[l]] = l;
            }
        }
        else{
            if(res[k]>res[j]){
                res[l] = res[k] - diff1;
                pos[res[l]] = l;
            }
            else{
                res[l] = res[k] + diff1;
                pos[res[l]] = l;
            }
        }

    }
    solve_right(k, l);
}

void solve(int N){
    n = N;
    find_1();
    find_n();

    int diff;

    if(pos[1]+1 <= n and res[pos[1]+1] == 0){
        diff = query(pos[1], pos[1]+1);
        res[pos[1]+1] = 1 + diff;
        pos[1+diff] = pos[1]+1;
    }

    if(pos[1]-1 >= 1 and res[pos[1]-1] == 0){
        diff = query(pos[1]-1, pos[1]);
        res[pos[1]-1] = 1 + diff;
        pos[1+diff] = pos[1]-1;
    }

    if(pos[n]+1 <=n and res[pos[n]+1] == 0){
        diff = query(pos[n], pos[n]+1);
        res[pos[n]+1] = n - diff;
        pos[n-diff] = pos[n]+1;
    }

    if(pos[n]-1 >= 1 and res[pos[n]-1] == 0){
        diff = query(pos[n]-1, pos[n]);
        res[pos[n]-1] = n - diff;
        pos[n-diff] = pos[n] - 1;
    }

    solve_left(pos[1]-1, pos[1]);
    solve_left(pos[n]-1, pos[n]);
    solve_right(pos[n], pos[n]+1);

    for(int i=1; i<=n; i++){
        answer(i, res[i]);
    }
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 312 KB Output is correct
2 Correct 2 ms 248 KB Output is correct
3 Correct 3 ms 376 KB Output is correct
4 Correct 3 ms 248 KB Output is correct
5 Correct 3 ms 248 KB Output is correct
6 Correct 3 ms 376 KB Output is correct
7 Correct 3 ms 396 KB Output is correct
8 Correct 4 ms 248 KB Output is correct
9 Correct 3 ms 252 KB Output is correct
10 Correct 3 ms 248 KB Output is correct
11 Correct 3 ms 376 KB Output is correct
12 Correct 3 ms 248 KB Output is correct
13 Correct 9 ms 376 KB Output is correct
14 Correct 4 ms 248 KB Output is correct
15 Correct 3 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 312 KB Output is correct
2 Correct 2 ms 248 KB Output is correct
3 Correct 3 ms 376 KB Output is correct
4 Correct 3 ms 248 KB Output is correct
5 Correct 3 ms 248 KB Output is correct
6 Correct 3 ms 376 KB Output is correct
7 Correct 3 ms 396 KB Output is correct
8 Correct 4 ms 248 KB Output is correct
9 Correct 3 ms 252 KB Output is correct
10 Correct 3 ms 248 KB Output is correct
11 Correct 3 ms 376 KB Output is correct
12 Correct 3 ms 248 KB Output is correct
13 Correct 9 ms 376 KB Output is correct
14 Correct 4 ms 248 KB Output is correct
15 Correct 3 ms 376 KB Output is correct
16 Correct 6 ms 252 KB Output is correct
17 Correct 10 ms 412 KB Output is correct
18 Correct 21 ms 376 KB Output is correct
19 Correct 19 ms 248 KB Output is correct
20 Correct 16 ms 376 KB Output is correct
21 Correct 19 ms 248 KB Output is correct
22 Correct 18 ms 248 KB Output is correct
23 Correct 13 ms 248 KB Output is correct
24 Correct 20 ms 376 KB Output is correct
25 Correct 11 ms 320 KB Output is correct
26 Correct 16 ms 380 KB Output is correct
27 Correct 11 ms 316 KB Output is correct
28 Correct 17 ms 248 KB Output is correct
29 Correct 20 ms 376 KB Output is correct
30 Correct 10 ms 312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 312 KB Output is correct
2 Correct 2 ms 248 KB Output is correct
3 Correct 3 ms 376 KB Output is correct
4 Correct 3 ms 248 KB Output is correct
5 Correct 3 ms 248 KB Output is correct
6 Correct 3 ms 376 KB Output is correct
7 Correct 3 ms 396 KB Output is correct
8 Correct 4 ms 248 KB Output is correct
9 Correct 3 ms 252 KB Output is correct
10 Correct 3 ms 248 KB Output is correct
11 Correct 3 ms 376 KB Output is correct
12 Correct 3 ms 248 KB Output is correct
13 Correct 9 ms 376 KB Output is correct
14 Correct 4 ms 248 KB Output is correct
15 Correct 3 ms 376 KB Output is correct
16 Correct 6 ms 252 KB Output is correct
17 Correct 10 ms 412 KB Output is correct
18 Correct 21 ms 376 KB Output is correct
19 Correct 19 ms 248 KB Output is correct
20 Correct 16 ms 376 KB Output is correct
21 Correct 19 ms 248 KB Output is correct
22 Correct 18 ms 248 KB Output is correct
23 Correct 13 ms 248 KB Output is correct
24 Correct 20 ms 376 KB Output is correct
25 Correct 11 ms 320 KB Output is correct
26 Correct 16 ms 380 KB Output is correct
27 Correct 11 ms 316 KB Output is correct
28 Correct 17 ms 248 KB Output is correct
29 Correct 20 ms 376 KB Output is correct
30 Correct 10 ms 312 KB Output is correct
31 Correct 46 ms 336 KB Output is correct
32 Correct 34 ms 376 KB Output is correct
33 Correct 69 ms 376 KB Output is correct
34 Correct 93 ms 248 KB Output is correct
35 Correct 82 ms 424 KB Output is correct
36 Correct 43 ms 548 KB Output is correct
37 Correct 97 ms 412 KB Output is correct
38 Correct 45 ms 336 KB Output is correct
39 Correct 81 ms 376 KB Output is correct
40 Correct 75 ms 248 KB Output is correct
41 Correct 74 ms 380 KB Output is correct
42 Correct 57 ms 248 KB Output is correct
43 Correct 93 ms 504 KB Output is correct
44 Correct 37 ms 316 KB Output is correct
45 Correct 70 ms 248 KB Output is correct
46 Correct 80 ms 248 KB Output is correct
47 Correct 76 ms 504 KB Output is correct
48 Correct 41 ms 344 KB Output is correct
49 Correct 79 ms 248 KB Output is correct
50 Correct 51 ms 312 KB Output is correct
51 Correct 78 ms 248 KB Output is correct
52 Correct 76 ms 348 KB Output is correct
53 Correct 60 ms 248 KB Output is correct
54 Correct 74 ms 352 KB Output is correct
55 Correct 66 ms 376 KB Output is correct
56 Correct 58 ms 344 KB Output is correct
57 Correct 85 ms 344 KB Output is correct
58 Correct 86 ms 316 KB Output is correct
59 Correct 72 ms 344 KB Output is correct
60 Correct 87 ms 348 KB Output is correct