답안 #563442

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
563442 2022-05-17T08:13:43 Z nghiass001 Swap (BOI16_swap) C++17
100 / 100
97 ms 16484 KB
#include <bits/stdc++.h>
#define FOR(i,l,r) for(int i=(l); i<=(r); ++i)
#define REP(i,l,r) for(int i=(l); i<(r); ++i)
#define FORD(i,r,l) for(int i=(r); i>=(l); --i)
#define REPD(i,r,l) for(int i=(r)-1; i>=(l); --i)
using namespace std;
const int N = 2e5 + 5, INF = 0x3c3c3c3c;
int n, res, a[N], used[N];
vector<int> *Q[N];

void Enter() {
    cin >> n;
    FOR(i, 1, n) cin >> a[i];
}

int query(int u) {
    if (u < 0) return a[-u];
    int val = INF;
    for(int v : *Q[u]) {
        if (v == u) val = min(val, a[u]);
        else val = min(val, query(v));
    }
    return val;
}

void del(int u, int val) {
    REP(i, 0, Q[u]->size()) if (query(Q[u]->at(i)) == val) {
        if (Q[u]->at(i) > 0) del(Q[u]->at(i), val);
        swap(Q[u]->at(i), Q[u]->back()); Q[u]->pop_back();
        return;
    }
}

void Process() {
    FOR(i, 1, n) {
        Q[i] = new vector<int>();
        Q[i]->push_back(-i);
    }
    FOR(i, 1, n) {
        int vL = (i*2 <= n ? a[i*2] : INF);
        int vR = (i*2 + 1 <= n ? a[i*2 + 1] : INF);
        int vP = query(i);
        int vmin = min({vL, vR, vP});
        if (vmin == vP) {
            del(i, vP);
        }
        else if (vmin == vL) {
            Q[i*2] = Q[i];
        }
        else if (vmin == vR) {
            Q[i*2]->push_back(i);
            Q[i*2 + 1] = Q[i*2];
        }
        else {
            exit(1);
        }

        cout << vmin << ' ';
    }
}

int main()
{
    #define file "t"
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    if (fopen(file".inp","r")) {
        freopen(file".inp","r",stdin);
        //freopen(file".out","w",stdout);
    }
    Enter();
    Process();
}

Compilation message

swap.cpp: In function 'void del(int, int)':
swap.cpp:3:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    3 | #define REP(i,l,r) for(int i=(l); i<(r); ++i)
      |                                    ^
swap.cpp:27:5: note: in expansion of macro 'REP'
   27 |     REP(i, 0, Q[u]->size()) if (query(Q[u]->at(i)) == val) {
      |     ^~~
swap.cpp: In function 'int main()':
swap.cpp:67:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   67 |         freopen(file".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 14 ms 4324 KB Output is correct
17 Correct 15 ms 4232 KB Output is correct
18 Correct 13 ms 4308 KB Output is correct
19 Correct 23 ms 4324 KB Output is correct
20 Correct 26 ms 4308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 14 ms 4324 KB Output is correct
17 Correct 15 ms 4232 KB Output is correct
18 Correct 13 ms 4308 KB Output is correct
19 Correct 23 ms 4324 KB Output is correct
20 Correct 26 ms 4308 KB Output is correct
21 Correct 54 ms 16428 KB Output is correct
22 Correct 55 ms 16460 KB Output is correct
23 Correct 54 ms 16436 KB Output is correct
24 Correct 97 ms 16484 KB Output is correct
25 Correct 97 ms 16464 KB Output is correct