제출 #590499

#제출 시각아이디문제언어결과실행 시간메모리
590499notaXylophone (JOI18_xylophone)C++14
100 / 100
68 ms436 KiB
#include<bits/stdc++.h>
#include <xylophone.h>
using namespace std;
const int maxN = 5001;
int a[maxN];
int chk[maxN];

void solve(int n)
{
    int l = 1, r = n;
    while(l <= r)
    {
        int mid = (l+r)/2;
        if(query(mid, n) == n-1)    l = mid + 1;
        else r = mid - 1;
    }
    answer(r, 1);
    a[r] = 1;
    chk[1] = true;
    for(int i = r - 1; i >= 1; i--)
    {
        int que = query(i, i+1);
        if(a[i+1] - que < 1)
        {
            a[i] = a[i+1] + que;
            answer(i,a[i]);
        }
        else if(a[i+1] + que > n)
        {
            a[i] = a[i+1] - que;
            answer(i,a[i]);
        }
        else
        {
            if(chk[a[i+1]-que])
            {
                a[i] = a[i+1] + que;
                answer(i, a[i]);
            }
            else if(chk[a[i+1] + que])
            {
                a[i] = a[i+1] - que;
                answer(i,a[i]);
            }
            else
            {
                int c = a[i+1] - que;
                int maxx,minn;
                maxx = max(max(c,a[i+1]),a[i+2]);
                minn = min(min(c,a[i+1]),a[i+2]);
                if(query(i,i+2) == maxx - minn)
                {
                    a[i] = c;
                    answer(i,c);
                }
                else
                {
                    a[i] = a[i+1] + que;
                    answer(i,a[i+1] + que);
                }
            }
        }
        chk[a[i]] = true;
    }
    for(int i = r + 1; i <= n; i++)
    {
        int que = query(i-1,i);
        if(a[i-1] - que < 1)
        {
            a[i] = a[i-1] + que;
            answer(i,a[i]);
        }
        else if(a[i-1] + que > n)
        {
            a[i] = a[i-1] - que;
            answer(i,a[i]);
        }
        else
        {
            if(chk[a[i-1]-que])
            {
                a[i] = a[i-1] + que;
                answer(i,a[i]);
            }
            else if(chk[a[i-1]+que])
            {
                a[i] = a[i-1] - que;
                answer(i,a[i]);
            }
            else
            {
                int c = a[i-1] - que;
                int maxx,minn;
                maxx = max(max(c,a[i-1]),a[i-2]);
                minn = min(min(c,a[i-1]),a[i-2]);
                if(query(i-2,i) == maxx - minn)
                {
                    a[i] = c;
                    answer(i,c);
                }
                else
                {
                    a[i] = a[i-1] + que;
                    answer(i,a[i-1] + que);
                }
            }
        }
        chk[a[i]] = true;
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...