제출 #233509

#제출 시각아이디문제언어결과실행 시간메모리
233509limabeansXylophone (JOI18_xylophone)C++17
11 / 100
90 ms416 KiB
#include "xylophone.h"
#include <bits/stdc++.h>
using namespace std;

template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl




typedef long long ll;
const ll mod = 1e9+7;
const int maxn = 1e6 + 5;
int n;
vector<int> a;

vector<int> p;


/*
//grader
int query(int s, int t) {
    int lo=1e9;
    int hi=-1e9;
    for (int i=s; i<=t; i++) {
	lo=min(lo,p[i]);
	hi=max(hi,p[i]);
    }
    return hi-lo;
}

void answer(int i, int a) {
    assert(i>=1 && i<=n);
    cout<<"a["<<i<<"]: "<<a<<endl;
    assert(p[i]==a);
}
//end grader
*/

int ask(int x, int y) {
    if (x>y) swap(x,y);
    return query(x,y);
}


void solveRange(int l, int r, int idx, bool hi=true) {
    if (l>=r) return;
    //cout<<"["<<l<<" "<<r<<"] : "<<idx<<" "<<hi<<endl;
    if (l+1==r) {
	int dx = query(l,r);
	if (idx==l) {
	    if (hi) a[r]=a[l]-dx; else a[r]=a[l]+dx;
	    answer(r,a[r]);
	} else {
	    if (hi) a[l]=a[r]-dx; else a[l]=a[r]+dx;
	    answer(l,a[l]);
	}
    } else {
	int d = query(l,r);
	if (idx==l) {
	    for (int i=r; i>=l; i--) {
		int dx=query(l,i);
		if (dx<d) {
		    if (hi) a[i+1]=a[l]-d; else a[i+1]=a[l]+d;
		    answer(i+1,a[i+1]);
		    //l..i+1..r
		    solveRange(l,i,l,hi);
		    solveRange(i+1,r,i+1,!hi);
		    return;
		}
	    }
	} else {
	    for (int i=l; i<=r; i++) {
		int dx=query(i,r);
		if (dx<d) {
		    if (hi) a[i-1]=a[r]-d; else a[i-1]=a[r]+d;
		    answer(i-1,a[i-1]);
		    //l..i-1...r
		    solveRange(l,i-1,i-1,!hi);
		    solveRange(i,r,r,hi);
		    return;
		}
	    }
	}
	assert(0);
    }
}

void solve(int N) {
    n = N;
    a.resize(n+1, -1);
    int nIdx=-1;
    for (int i=n; i>=1; i--) {
	if (ask(1,i)<n-1) {
	    assert(i<n);
	    answer(i+1,n);
	    a[i+1]=n;
	    nIdx=i+1;
	    break;
	}
    }
    solveRange(1,nIdx,nIdx);
    solveRange(nIdx,n,nIdx);
}

vector<int> rand_perm(int n, int index=1) {
    assert(n>=1);
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    vector<int> a(n);
    iota(a.begin(), a.end(), index);
    shuffle(a.begin(), a.end(), rng);
    return a;
}

// int main() {
//     ios_base::sync_with_stdio(false); cin.tie(0);  cout.tie(0);

//     int siz = 20;
//     p=rand_perm(siz);
//     //p={5,4,2,6,1,3};
//     p.insert(p.begin(), -1);
//     int lo=1,hi=1;
//     for (int i=1; i<=siz; i++) {
// 	if (p[i]>p[hi]) hi=i;
// 	if (p[i]<p[lo]) lo=i;
//     }
//     if (lo>hi) swap(p[lo],p[hi]);

//     for (int i: p) cout<<i<<" ";
//     cout<<endl;
    
//     solve(siz);

//     return 0;
// }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...