답안 #826272

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
826272 2023-08-15T11:58:21 Z fatemetmhr Monster Game (JOI21_monster) C++17
100 / 100
86 ms 4352 KB
//  ~ Be Name Khoda ~  //

#include "monster.h"
#include <bits/stdc++.h>
//#pragma GCC optimize ("O3")
//#pragma GCC target("avx2")
//#pragma GCC optimize("unroll-loops,Ofast")

using namespace std;

typedef long long ll;

#define pb       push_back
#define mp       make_pair
#define all(x)   x.begin(), x.end()
#define fi       first
#define se       second

const int maxn  =  1e6   + 10;
const int maxn5 =  1e3   + 10;
const int maxnt =  1.2e6 + 10;
const int maxn3 =  1e3   + 10;
const int mod   =  1e9   + 7;
const ll  inf   =  1e18;

namespace {

int a[maxn5], cnt[maxn5], done[maxn5][maxn5], mx[maxn5];
vector <int> av;

bool assk(int a, int b){
    if(done[a][b] == -1){
        done[a][b] = Query(a, b);
        done[b][a] = done[a][b] ^ 1;
    }
    return done[a][b];
}

void inser(int id, int val){
    vector <int> tmp;
    for(int i = 0; i < av.size(); i++){
        if(tmp.size() == id)
            tmp.pb(val);
        tmp.pb(av[i]);
    }
    if(tmp.size() == id)
        tmp.pb(val);
    av.clear();
    for(auto u : tmp)
        av.pb(u);
}

bool cmp(int i, int j){return cnt[i] < cnt[j] || (cnt[i] == cnt[j] && assk(i, j));}


}  // namespace

std::vector<int> Solve(int n) {
    std::vector<int> ret(n);

    memset(done, -1, sizeof done);

    if(n < 10){
        for(int i = 1; i < n; i++){
            int lo = -1, hi = i;
            while(hi - lo > 1){
                int mid = (lo + hi) >> 1;
                if(assk(a[mid], i))
                    hi = mid;
                else
                    lo = mid;
            }
            cnt[i] = lo + 1;
            for(int j = hi; j < i; j++)
                cnt[a[j]]++;
            for(int j = max(0, lo - 3); j < lo; j++) if(!assk(i, a[j])){
                cnt[a[j]]++;
                cnt[i]--;
                ////cout << "ha? " << i << ' ' << j << ' ' << a[j] << endl;
            }
            for(int j = hi + 1; j < min(i, hi + 4); j++) if(assk(i, a[j])){
                cnt[a[j]]--;
                cnt[i]++;
            }
            ////cout << "* " << i << ' ' << lo << ' ' << hi << ' ' << cnt[i] << ' ' << cnt[0] << endl;
            a[i] = i;
            sort(a, a + i + 1, cmp);
        }
        /*
        for(int i = 0; i < n; i++)
            //cout << a[i] << ' ' << cnt[a[i]] << endl;
        //cout << endl;
        //*/
     
        for (int i = 0; i < n; i++)
            ret[a[i]] = i;
     
        return ret;
    }

    av.pb(0);
    for(int i = 1; i < n; i++){
        int lo = -1, hi = av.size();
        while(hi - lo > 1){
            int mid = (lo + hi) >> 1;
            if(assk(av[mid], i))
                hi = mid;
            else
                lo = mid;
            ////cout << "hmm " << i << ' ' << lo << ' ' << hi << endl;
        }
        inser(hi, i);
    }
    /*
    for(auto u : av)
        cout << u << ' ';
    cout << endl;
    //*/
    for(int i = 1; i < n; i++)
        assert(assk(av[i], av[i - 1]));

    int id = 2;
    while(id < n && assk(av[0], av[id]))
        id++;
    bool was2 = false, was3 = false;
    int mxid = 0;
    mx[0] = true;
    if(id < n){
        if(id >= 4){
            if(!assk(av[1], av[id - 1]))
                id--;
        }
        if(id == 2){
            was2 = true;
        }
        else if(id == 3){
            was3 = true;
            mx[3] = true;
        }
    }
    int st = 0;
    if(was3){
        mxid = 3;
        was3 = false;
        id = 5;
        while(id < n && assk(av[3], av[id]))
            id++;
        //cout << "hmmm " << id << endl;
        if(id >= 7){
            if(!assk(av[4], av[id - 1])){
                id--;
            }
            //cout << "then " << id << endl;
            if(id > 6){
                if(assk(av[1], av[id - 1]))
                    mx[0] = mx[1] = true;
                else if(assk(av[2], av[id - 1]))
                    mx[0] = mx[2] = true;
            }
        }
        if(id == 5){
            st = 3;
            was2 = true;
            was3 = true;
        }
        else if(id == 6){
            int st = 3;
            //cout << "it was " << endl;
            if(assk(av[0], av[st + 0])){
                mx[0] = mx[3] = mx[4] = true;
                mxid = 4;
            }
            else if(assk(av[0], av[st + 1])){
                mx[0] = mx[3] = mx[5] = true;
                mxid = 5;
            }
            else if(assk(av[0], av[st + 2])){
                mx[0] = mx[3] = true;
                mxid = 3;
            }
            else if(assk(av[1], av[st + 0])){
                mx[0] = mx[1] = mx[3] = mx[4] = true;
                mxid = 4;
            }
            else if(assk(av[1], av[st + 1])){
                mx[0] = mx[1] = mx[3] = mx[5] = true;
                mxid = 5;
            }
            else if(assk(av[1], av[st + 2])){
                mx[0] = mx[1] = mx[3] = true;
                mxid = 3;
            }
            else if(assk(av[2], av[st + 0])){
                mx[0] = mx[2] = mx[3] = mx[4] = true;
                mxid = 4;
            }
            else if(assk(av[2], av[st + 1])){
                mx[0] = mx[2] = mx[3] = mx[5] = true;
                mxid = 5;
            }
            else{ // if(assk(av[1], av[st + 2]))
                mx[0] = mx[2] = mx[3] = true;
                mxid = 3;
            }
            //cout << mx[0] << ' ' << mx[1] << ' ' << mx[2] << ' ' << mx[3] << ' ' << mx[4] << ' ' << mx[5] << endl;
        }
    }
    mx[id] = true;
    int last = id;
    while(id < n){
        //cout << "**** " << id << ' ' << mxid << ' ' << was2 << ' ' << was3 << endl;
        if(assk(av[mxid], av[id])){
            if(was2){
                ////cout << "here " << was3 << ' ' << st << endl;
                if(assk(av[1 + st], av[id])){ // 1 x ..
                    mx[st] = mx[1 + st] = true;
                    mx[st + 2] = false;
                    mxid = st + 1;
                }
                else{ // 2 1 x ..
                    mx[st] = mx[2 + st] = true;
                    mx[st + 1] = false;
                    mxid = st + 2;
                }
                ////cout << mx[st] << ' ' << mx[st + 1] << ' ' << mx[st + 2] << endl;
                was2 = false;
                if(was3){
                    if(mx[st + 1]){
                        if(assk(av[0], av[st]))
                            mx[0] = true;
                        else{
                            mx[0] = mx[1] = true;
                        }
                    }
                    else{
                        if(assk(av[0], av[4]))
                            mx[0] = true;
                        else if(assk(av[1], av[4]))
                            mx[0] = mx[1] = true;
                        else
                            mx[0] = mx[2] = true;
                    }
                    was3 = false;
                }
            }
            else{
                mxid = last;
            }
            ////cout << "ha? " << id << ' ' << last << endl;
            last = id + 1;
            mx[id + 1] = true;
            id++;
            continue;
        }
        id++;
    }
    last = 0;
    for(int i = 1; i < n; i++){
        if(mx[i]){
            //cout << i << ' ' << mx[i] << endl;
            reverse(av.begin() + last, av.begin() + i);
            last = i;
        }
    }
    reverse(av.begin() + last, av.end());
    for(int i = 0; i < n; i++)
        ret[av[i]] = i;
    return ret;
}













Compilation message

monster.cpp: In function 'void {anonymous}::inser(int, int)':
monster.cpp:41:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   41 |     for(int i = 0; i < av.size(); i++){
      |                    ~~^~~~~~~~~~~
monster.cpp:42:23: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   42 |         if(tmp.size() == id)
      |            ~~~~~~~~~~~^~~~~
monster.cpp:46:19: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   46 |     if(tmp.size() == id)
      |        ~~~~~~~~~~~^~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4176 KB Output is correct
2 Correct 2 ms 4176 KB Output is correct
3 Correct 2 ms 4176 KB Output is correct
4 Correct 2 ms 4176 KB Output is correct
5 Correct 2 ms 4176 KB Output is correct
6 Correct 2 ms 4176 KB Output is correct
7 Correct 2 ms 4304 KB Output is correct
8 Correct 2 ms 4300 KB Output is correct
9 Correct 2 ms 4176 KB Output is correct
10 Correct 2 ms 4176 KB Output is correct
11 Correct 2 ms 4176 KB Output is correct
12 Correct 2 ms 4176 KB Output is correct
13 Correct 2 ms 4304 KB Output is correct
14 Correct 2 ms 4176 KB Output is correct
15 Correct 2 ms 4176 KB Output is correct
16 Correct 12 ms 4304 KB Output is correct
17 Correct 12 ms 4176 KB Output is correct
18 Correct 13 ms 4304 KB Output is correct
19 Correct 10 ms 4304 KB Output is correct
20 Correct 10 ms 4176 KB Output is correct
21 Correct 2 ms 4304 KB Output is correct
22 Correct 2 ms 4176 KB Output is correct
23 Correct 3 ms 4176 KB Output is correct
24 Correct 2 ms 4176 KB Output is correct
25 Correct 2 ms 4176 KB Output is correct
26 Correct 14 ms 4300 KB Output is correct
27 Correct 2 ms 4176 KB Output is correct
28 Correct 2 ms 4176 KB Output is correct
29 Correct 2 ms 4176 KB Output is correct
30 Correct 2 ms 4176 KB Output is correct
31 Correct 2 ms 4304 KB Output is correct
32 Correct 11 ms 4300 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4176 KB Output is correct
2 Correct 2 ms 4176 KB Output is correct
3 Correct 2 ms 4176 KB Output is correct
4 Correct 2 ms 4176 KB Output is correct
5 Correct 2 ms 4176 KB Output is correct
6 Correct 2 ms 4176 KB Output is correct
7 Correct 2 ms 4304 KB Output is correct
8 Correct 2 ms 4300 KB Output is correct
9 Correct 2 ms 4176 KB Output is correct
10 Correct 2 ms 4176 KB Output is correct
11 Correct 2 ms 4176 KB Output is correct
12 Correct 2 ms 4176 KB Output is correct
13 Correct 2 ms 4304 KB Output is correct
14 Correct 2 ms 4176 KB Output is correct
15 Correct 2 ms 4176 KB Output is correct
16 Correct 12 ms 4304 KB Output is correct
17 Correct 12 ms 4176 KB Output is correct
18 Correct 13 ms 4304 KB Output is correct
19 Correct 10 ms 4304 KB Output is correct
20 Correct 10 ms 4176 KB Output is correct
21 Correct 2 ms 4304 KB Output is correct
22 Correct 2 ms 4176 KB Output is correct
23 Correct 3 ms 4176 KB Output is correct
24 Correct 2 ms 4176 KB Output is correct
25 Correct 2 ms 4176 KB Output is correct
26 Correct 14 ms 4300 KB Output is correct
27 Correct 2 ms 4176 KB Output is correct
28 Correct 2 ms 4176 KB Output is correct
29 Correct 2 ms 4176 KB Output is correct
30 Correct 2 ms 4176 KB Output is correct
31 Correct 2 ms 4304 KB Output is correct
32 Correct 11 ms 4300 KB Output is correct
33 Correct 71 ms 4304 KB Output is correct
34 Correct 86 ms 4304 KB Output is correct
35 Correct 57 ms 4304 KB Output is correct
36 Correct 45 ms 4304 KB Output is correct
37 Correct 55 ms 4288 KB Output is correct
38 Correct 80 ms 4304 KB Output is correct
39 Correct 65 ms 4304 KB Output is correct
40 Correct 48 ms 4268 KB Output is correct
41 Correct 83 ms 4304 KB Output is correct
42 Correct 81 ms 4304 KB Output is correct
43 Correct 60 ms 4288 KB Output is correct
44 Correct 53 ms 4304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 4304 KB Output is correct
2 Correct 55 ms 4308 KB Output is correct
3 Correct 40 ms 4352 KB Output is correct
4 Correct 81 ms 4308 KB Output is correct
5 Correct 46 ms 4304 KB Output is correct
6 Correct 58 ms 4304 KB Output is correct
7 Correct 59 ms 4296 KB Output is correct
8 Correct 61 ms 4304 KB Output is correct
9 Correct 77 ms 4304 KB Output is correct
10 Correct 63 ms 4304 KB Output is correct
11 Correct 64 ms 4272 KB Output is correct
12 Correct 52 ms 4304 KB Output is correct
13 Correct 61 ms 4308 KB Output is correct
14 Correct 45 ms 4308 KB Output is correct
15 Correct 85 ms 4288 KB Output is correct
16 Correct 77 ms 4304 KB Output is correct
17 Correct 86 ms 4304 KB Output is correct
18 Correct 56 ms 4308 KB Output is correct