Submission #429573

# Submission time Handle Problem Language Result Execution time Memory
429573 2021-06-16T06:56:11 Z lyc Park (JOI17_park) C++14
100 / 100
540 ms 808 KB
#include "park.h"

#include <bits/stdc++.h>
using namespace std;

#define TRACE(x) cerr << #x << " :: " << x << endl
#define _ << " " <<
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(),(x).end()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)

static int Place[1400];

const int mxN = 1405;
vector<int> solved, rem, stk, al[mxN];
int N, ban[mxN], vis[mxN];

void myans(int i, int j) {
    //TRACE("ANSWER" _ i _ j);
    if (i > j) swap(i,j);
    al[i].push_back(j);
    al[j].push_back(i);
    Answer(i,j);
}

bool myask(int i, int j, vector<int> v) {
    if (i > j) swap(i,j);
    v.push_back(i);
    v.push_back(j);

    for (int& x : v) Place[x] = 1;
    int ret = Ask(i,j,Place);
    for (int& x : v) Place[x] = 0;
    return ret;
}

void solveLine(int l, int r) {
    //TRACE("LINE" _ l _ r);
    vector<int> line;
    line.push_back(l);
    line.push_back(r);
    while (l != r) {
        int u = *(++find(ALL(line),l));
        while (true) {
            //TRACE("CHECK" _ l _ u);
            if (myask(l,u,solved)) {
                l = u;
                break;
            }

            int lo = -1, hi = SZ(rem);
            while (hi-lo > 1) {
                int mid = (lo+hi)/2;

                vector<int> v2(solved);
                FOR(i,0,mid) v2.push_back(rem[i]);
                if (myask(l,u,v2)) hi = mid;
                else lo = mid;
            }
            u = rem[hi];

            assert(find(ALL(rem),u) != rem.end());
            rem.erase(find(ALL(rem),u));
            line.insert(find(ALL(line),l)+1,u);
        }
    }
    //cerr << "line: "; for (int& x : line) { cerr << x << ' '; } cerr << endl;
    //cerr << "rem: "; for (int& x : rem) { cerr << x << ' '; } cerr << endl;
    line.pop_back();
    for (int& x : line) stk.push_back(x);
}

void dfs(int u, vector<int>& cc) {
    vis[u] = 1; cc.push_back(u);
    for (int& v : al[u]) if (!ban[v] && !vis[v]) dfs(v,cc);
}

void solveComp(int u, vector<int> cc) {
    //TRACE("COMP" _ u _ SZ(cc));
    //for (int& x : cc){ cout << x << ' '; } cout << endl;
    int lo = -1, hi = SZ(cc);
    while (hi-lo > 1) {
        int mid = (lo+hi)/2;

        vector<int> v2;
        FOR(i,0,mid) v2.push_back(cc[i]);
        if (myask(u,cc[0],v2)) hi = mid;
        else lo = mid;
    }
    int v = cc[hi];
    myans(u,v);
    ban[v] = 1;
    fill_n(vis,N,0);
    vector<vector<int>> vcc;
    for (int& x : cc) if (!ban[x] && !vis[x]) {
        vector<int> comp; dfs(x,comp);
        if (myask(u,comp[0],comp)) vcc.push_back(comp);
    }
    for (auto& comp : vcc) solveComp(u,comp);
}

void Detect(int T, int _N) {
    N = _N;
    solved.push_back(0);
    FOR(i,1,N-1) rem.push_back(i);
    while (SZ(solved) < N) {
        if (!SZ(stk)) stk.push_back(rem.back()), rem.pop_back();
        int u = stk.back(); stk.pop_back();
        //TRACE("CHECK" _ u _ solved[0]);
        //for (int& x : solved){ cout << x << ' '; } cout << endl;
        if (!myask(u,solved[0],solved)) {
            solveLine(u,solved[0]);
        } else {
            fill_n(ban,N,0);
            solveComp(u,solved);
            solved.push_back(u);
        }
        //TRACE("OK");
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 12 ms 472 KB Output is correct
3 Correct 11 ms 440 KB Output is correct
4 Correct 25 ms 480 KB Output is correct
5 Correct 53 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 425 ms 780 KB Output is correct
2 Correct 420 ms 760 KB Output is correct
3 Correct 397 ms 632 KB Output is correct
4 Correct 423 ms 652 KB Output is correct
5 Correct 419 ms 708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 350 ms 656 KB Output is correct
2 Correct 382 ms 616 KB Output is correct
3 Correct 388 ms 500 KB Output is correct
4 Correct 350 ms 528 KB Output is correct
5 Correct 395 ms 664 KB Output is correct
6 Correct 377 ms 684 KB Output is correct
7 Correct 356 ms 684 KB Output is correct
8 Correct 371 ms 752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 203 ms 628 KB Output is correct
2 Correct 433 ms 520 KB Output is correct
3 Correct 434 ms 568 KB Output is correct
4 Correct 464 ms 632 KB Output is correct
5 Correct 437 ms 580 KB Output is correct
6 Correct 343 ms 636 KB Output is correct
7 Correct 496 ms 736 KB Output is correct
8 Correct 426 ms 588 KB Output is correct
9 Correct 440 ms 592 KB Output is correct
10 Correct 440 ms 600 KB Output is correct
11 Correct 478 ms 624 KB Output is correct
12 Correct 414 ms 764 KB Output is correct
13 Correct 470 ms 688 KB Output is correct
14 Correct 471 ms 600 KB Output is correct
15 Correct 472 ms 572 KB Output is correct
16 Correct 377 ms 580 KB Output is correct
17 Correct 419 ms 580 KB Output is correct
18 Correct 450 ms 720 KB Output is correct
19 Correct 481 ms 580 KB Output is correct
20 Correct 453 ms 536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 483 ms 560 KB Output is correct
2 Correct 462 ms 616 KB Output is correct
3 Correct 395 ms 616 KB Output is correct
4 Correct 463 ms 580 KB Output is correct
5 Correct 452 ms 708 KB Output is correct
6 Correct 436 ms 748 KB Output is correct
7 Correct 540 ms 592 KB Output is correct
8 Correct 436 ms 632 KB Output is correct
9 Correct 424 ms 608 KB Output is correct
10 Correct 435 ms 768 KB Output is correct
11 Correct 432 ms 636 KB Output is correct
12 Correct 519 ms 808 KB Output is correct
13 Correct 426 ms 704 KB Output is correct
14 Correct 480 ms 792 KB Output is correct
15 Correct 367 ms 536 KB Output is correct
16 Correct 369 ms 532 KB Output is correct
17 Correct 435 ms 680 KB Output is correct
18 Correct 307 ms 716 KB Output is correct