Submission #48071

# Submission time Handle Problem Language Result Execution time Memory
48071 2018-05-10T02:42:26 Z lovemathboy Library (JOI18_library) C++14
100 / 100
550 ms 844 KB
#include <bits/stdc++.h>
#include "library.h"
using namespace std;

vector<vector<int> > adj;
int n;

void recur(vector<int> options, int num) {
    if ((int)options.size() == 1) {
        vector<int> m;
        m.resize(n, 0);
        m[options[0]] = 1;
        m[num] = 1;
        if (Query(m) != 1) return;
        adj[num].push_back(options[0]);
        adj[options[0]].push_back(num);
        return;
    }
    vector<int> cur;
    for (int i = 0; i < options.size()/2; i++) {
        cur.push_back(options[i]);
    }
    vector<int> m;
    m.resize(n, 0);
    for (int i = 0; i < cur.size(); i++) m[cur[i]] = 1;
    int q1 = Query(m);
    m[num] = 1;
    int q2 = Query(m);
    if (q1 == q2) recur(cur, num);
    else {
        cur.clear();
        for (int i = options.size()/2; i < options.size(); i++) {
            cur.push_back(options[i]);
        }
        recur(cur, num);
    }
}

void recur1(vector<int> options, int num) {
    if ((int)options.size() == 1) {
        vector<int> m;
        m.resize(n, 0);
        m[options[0]] = 1;
        m[num] = 1;
        if (Query(m) != 1) return;
        adj[num].push_back(options[0]);
        adj[options[0]].push_back(num);
        return;
    }
    vector<int> cur;
    for (int i = 0; i < options.size()/2; i++) {
        cur.push_back(options[i]);
    }
    vector<int> m;
    m.resize(n, 0);
    for (int i = 0; i < cur.size(); i++) m[cur[i]] = 1;
    int q1 = Query(m);
    m[num] = 1;
    int q2 = Query(m);
    vector<int> temp;// = cur;
    for (int i = 0; i < cur.size(); i++) temp.push_back(cur[i]);
    cur.clear();
    for (int i = options.size()/2; i < options.size(); i++) {
        cur.push_back(options[i]);
    }
    m.clear(); m.resize(n, 0);
    for (int i = 0; i < cur.size(); i++) m[cur[i]] = 1;
    int q3 = Query(m);
    m[num] = 1;
    int q4 = Query(m);
    //printf("%d %d %d %d\n", q1, q2, q3, q4);
    if (q1 == q2 && q3 == q4) {
        recur(cur, num);
        recur(temp, num);
    }
    else if (q1 == q2) recur(temp, num);
    else if (q3 == q4) recur(cur, num);
    else if (q2 < q1) recur1(temp, num);
    else if (q4 < q3) recur1(cur, num);
    else assert(false);
}

void Solve(int N) {
    srand(492);
    n = N;
    if (n == 1) {
        vector<int> res;
        res.resize(1, 1);
        Answer(res);
        return;
    }
    adj.resize(n);

	vector<int> options;
	vector<int> cur;
	deque<int> ans;
	vector<bool> vis;
	vis.resize(n, false);
    int num = rand()%n;
    int orig = num;
    vis[num] = true;
    for (int i = 0; i < n; i++) {
        if (!vis[i]) options.push_back(i);
    }
    recur1(options, num);
    //printf("%d\n", adj[num].size());
    ans.push_back(num);
    for (int i = 0; i < adj[orig].size(); i++) {
        num = orig;
        int prev = num;
        num = adj[num][i];
        if (i == 0) ans.push_back(num);
        else ans.push_front(num);
        vis[num] = true;
        options.clear();
        for (int j = 0; j < n; j++) if (!vis[j]) options.push_back(j);
        if (options.size() == 0) break;
        recur(options, num);
        //printf("%d %d\n", num, adj[num].size());
        while (adj[num].size() == 2) {
            int newprev = num;
            if (adj[num][0] == prev) num = adj[num][1];
            else num = adj[num][0];
            if (i == 0) ans.push_back(num);
            else ans.push_front(num);
            prev = newprev;
            vis[num] = true;
            options.clear();
            for (int j = 0; j < n; j++) if (!vis[j]) options.push_back(j);
            if (options.size() == 0) break;
            recur(options, num);
            //printf("%d %d\n", num, adj[num].size());
        }
    }
    vector<int> res;
    while (!ans.empty()) {
        res.push_back(ans.front()+1);
        ans.pop_front();
    }
	Answer(res);
	return;
}

Compilation message

library.cpp: In function 'void recur(std::vector<int>, int)':
library.cpp:20:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < options.size()/2; i++) {
                     ~~^~~~~~~~~~~~~~~~~~
library.cpp:25:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < cur.size(); i++) m[cur[i]] = 1;
                     ~~^~~~~~~~~~~~
library.cpp:32:42: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int i = options.size()/2; i < options.size(); i++) {
                                        ~~^~~~~~~~~~~~~~~~
library.cpp: In function 'void recur1(std::vector<int>, int)':
library.cpp:51:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < options.size()/2; i++) {
                     ~~^~~~~~~~~~~~~~~~~~
library.cpp:56:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < cur.size(); i++) m[cur[i]] = 1;
                     ~~^~~~~~~~~~~~
library.cpp:61:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < cur.size(); i++) temp.push_back(cur[i]);
                     ~~^~~~~~~~~~~~
library.cpp:63:38: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = options.size()/2; i < options.size(); i++) {
                                    ~~^~~~~~~~~~~~~~~~
library.cpp:67:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < cur.size(); i++) m[cur[i]] = 1;
                     ~~^~~~~~~~~~~~
library.cpp: In function 'void Solve(int)':
library.cpp:108:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < adj[orig].size(); i++) {
                     ~~^~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 41 ms 248 KB Output is correct
2 Correct 55 ms 312 KB Output is correct
3 Correct 44 ms 384 KB Output is correct
4 Correct 33 ms 476 KB Output is correct
5 Correct 46 ms 484 KB Output is correct
6 Correct 31 ms 636 KB Output is correct
7 Correct 36 ms 636 KB Output is correct
8 Correct 41 ms 636 KB Output is correct
9 Correct 57 ms 636 KB Output is correct
10 Correct 24 ms 636 KB Output is correct
11 Correct 2 ms 636 KB Output is correct
12 Correct 2 ms 636 KB Output is correct
13 Correct 2 ms 636 KB Output is correct
14 Correct 2 ms 636 KB Output is correct
15 Correct 3 ms 636 KB Output is correct
16 Correct 4 ms 636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 248 KB Output is correct
2 Correct 55 ms 312 KB Output is correct
3 Correct 44 ms 384 KB Output is correct
4 Correct 33 ms 476 KB Output is correct
5 Correct 46 ms 484 KB Output is correct
6 Correct 31 ms 636 KB Output is correct
7 Correct 36 ms 636 KB Output is correct
8 Correct 41 ms 636 KB Output is correct
9 Correct 57 ms 636 KB Output is correct
10 Correct 24 ms 636 KB Output is correct
11 Correct 2 ms 636 KB Output is correct
12 Correct 2 ms 636 KB Output is correct
13 Correct 2 ms 636 KB Output is correct
14 Correct 2 ms 636 KB Output is correct
15 Correct 3 ms 636 KB Output is correct
16 Correct 4 ms 636 KB Output is correct
17 Correct 531 ms 660 KB Output is correct
18 Correct 523 ms 660 KB Output is correct
19 Correct 550 ms 796 KB Output is correct
20 Correct 456 ms 796 KB Output is correct
21 Correct 484 ms 796 KB Output is correct
22 Correct 496 ms 796 KB Output is correct
23 Correct 539 ms 796 KB Output is correct
24 Correct 170 ms 796 KB Output is correct
25 Correct 500 ms 796 KB Output is correct
26 Correct 450 ms 844 KB Output is correct
27 Correct 186 ms 844 KB Output is correct
28 Correct 483 ms 844 KB Output is correct
29 Correct 488 ms 844 KB Output is correct
30 Correct 485 ms 844 KB Output is correct