답안 #668516

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
668516 2022-12-04T03:15:49 Z BlackJack Swap (BOI16_swap) C++14
68 / 100
389 ms 262144 KB
#include<bits/stdc++.h>
using namespace std;

void DBG() { cerr << "]\n"; }
template<class H, class... T> void DBG(H h, T... t) {
    cerr << h; if(sizeof...(t)) cerr << ", ";
    DBG(t...);
}
#ifdef LOCAL
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif // LOCAL

#define FOR(i,a,b) for(int i = (a) ; i<(b) ; i++)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for(int i = (b)-1 ; i>=(a) ; i--)
#define R0F(i,a) ROF(i,0,a)
#define each(e,a) for(auto &e : (a))
#define sz(v) (int)(v).size()
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define pb push_back
#define tcT template<class T
#define nl "\n"
#define fi first
#define se second

using ll = long long;
using vi = vector<int>;
using pi = pair<int,int>;
using str = string;
using db = long double;
tcT> using V = vector<T>;
tcT> using pqg = priority_queue<T,vector<T>,greater<T>>;

void setIO(string NAME = "") {
    cin.tie(0)->sync_with_stdio(0);
    if(sz(NAME)) {
        freopen((NAME + ".inp").c_str(),"r",stdin);
        freopen((NAME + ".out").c_str(),"w",stdout);
    }
}

tcT> bool ckmin(T&a, const T&b) {
    return b < a ? a=b,1 : 0; }
tcT> bool ckmax(T&a, const T&b) {
    return b > a ? a=b,1 : 0; }

const int MOD = 1e9 + 7;
const ll INF = 1e18;

const int LG = 18;

const int MX = 2e5+5;

int N;

int A[MX];

vi dp[MX][LG<<1];

bool need[MX][LG<<1];

void mrg(vi& res, const vi&a, const vi&b) {
    for(int i = 0, j = 0, step = 1; i < sz(a) || j < sz(b); i += step, j += step, step <<= 1) {
        for(int k = 0; k < step && i + k < sz(a); k++)
            res.pb(a[i+k]);
        for(int k = 0; k < step && j + k < sz(b); k++)
            res.pb(b[j+k]);
    }
}

void solve() {
    cin >> N;
    FOR(i,1,N+1) cin >> A[i];
    need[1][0] = 1;
    for(int i = 1; 2*i + 1 <= N; i++) {
        F0R(j, LG<<1) if(need[i][j]) {
            int b = (i >> (j>>1)) ^ (j&1);
            if(A[i<<1|1] < A[i<<1] && A[i<<1|1] < A[b]) {
                need[i<<1][0] = need[i<<1|1][1] = true;
                if(j + 2 < (LG<<1)) need[i<<1|1][j+2] = need[i<<1][j+2] = true;
            }
            else {
                need[i<<1][0] = need[i<<1|1][0] = 1;
                if(j + 2 < (LG<<1)) need[i<<1][j+2] = 1;
            }
        }
    }

    ROF(i,1,N+1) F0R(j, LG<<1) if(need[i][j]) {
        int b = (i >> (j>>1)) ^ (j&1);
        if((i<<1|1) <= N) {
            if(A[i<<1|1] < A[i<<1] && A[i<<1|1] < A[b]) {
                vi opt1 = {A[i<<1|1]}, opt2 = opt1;
                mrg(opt1, dp[i<<1][0], dp[i<<1|1][j+2]);
                mrg(opt2, dp[i<<1][j+2], dp[i<<1|1][1]);
                dp[i][j] = min(opt1, opt2);
            }
            else {
                if(A[i<<1] < A[b] && A[i<<1] < A[i<<1|1]) {
                    dp[i][j] = {A[i<<1]};
                    mrg(dp[i][j], dp[i<<1][j+2], dp[i<<1|1][0]);
                }
                else {
                    dp[i][j] = {A[b]};
                    mrg(dp[i][j], dp[i<<1][0], dp[i<<1|1][0]);
                }
            }
        }
        else {
            if((i<<1) <= N) {
                dp[i][j] = {min(A[b], A[i<<1]), max(A[b], A[i<<1])};
            }
            else {
                dp[i][j] = {A[b]};
            }
        }
    }
    each(e, dp[1][0]) cout << e << " ";
    cout << nl;
}

int main() {
    setIO();

    int t=1;
    //cin>>t;
    while(t-->0) {
        solve();
    }

    return 0;
}

Compilation message

swap.cpp: In function 'void setIO(std::string)':
swap.cpp:40:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 |         freopen((NAME + ".inp").c_str(),"r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
swap.cpp:41:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   41 |         freopen((NAME + ".out").c_str(),"w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 75 ms 169292 KB Output is correct
2 Correct 76 ms 169340 KB Output is correct
3 Correct 77 ms 169284 KB Output is correct
4 Correct 77 ms 169332 KB Output is correct
5 Correct 75 ms 169352 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 75 ms 169292 KB Output is correct
2 Correct 76 ms 169340 KB Output is correct
3 Correct 77 ms 169284 KB Output is correct
4 Correct 77 ms 169332 KB Output is correct
5 Correct 75 ms 169352 KB Output is correct
6 Correct 80 ms 169380 KB Output is correct
7 Correct 76 ms 169312 KB Output is correct
8 Correct 76 ms 169412 KB Output is correct
9 Correct 80 ms 169424 KB Output is correct
10 Correct 78 ms 169428 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 75 ms 169292 KB Output is correct
2 Correct 76 ms 169340 KB Output is correct
3 Correct 77 ms 169284 KB Output is correct
4 Correct 77 ms 169332 KB Output is correct
5 Correct 75 ms 169352 KB Output is correct
6 Correct 80 ms 169380 KB Output is correct
7 Correct 76 ms 169312 KB Output is correct
8 Correct 76 ms 169412 KB Output is correct
9 Correct 80 ms 169424 KB Output is correct
10 Correct 78 ms 169428 KB Output is correct
11 Correct 79 ms 169532 KB Output is correct
12 Correct 79 ms 169540 KB Output is correct
13 Correct 77 ms 169616 KB Output is correct
14 Correct 88 ms 169760 KB Output is correct
15 Correct 91 ms 169932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 75 ms 169292 KB Output is correct
2 Correct 76 ms 169340 KB Output is correct
3 Correct 77 ms 169284 KB Output is correct
4 Correct 77 ms 169332 KB Output is correct
5 Correct 75 ms 169352 KB Output is correct
6 Correct 80 ms 169380 KB Output is correct
7 Correct 76 ms 169312 KB Output is correct
8 Correct 76 ms 169412 KB Output is correct
9 Correct 80 ms 169424 KB Output is correct
10 Correct 78 ms 169428 KB Output is correct
11 Correct 79 ms 169532 KB Output is correct
12 Correct 79 ms 169540 KB Output is correct
13 Correct 77 ms 169616 KB Output is correct
14 Correct 88 ms 169760 KB Output is correct
15 Correct 91 ms 169932 KB Output is correct
16 Correct 129 ms 184076 KB Output is correct
17 Correct 130 ms 183884 KB Output is correct
18 Correct 130 ms 184228 KB Output is correct
19 Correct 264 ms 211544 KB Output is correct
20 Correct 256 ms 211248 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 75 ms 169292 KB Output is correct
2 Correct 76 ms 169340 KB Output is correct
3 Correct 77 ms 169284 KB Output is correct
4 Correct 77 ms 169332 KB Output is correct
5 Correct 75 ms 169352 KB Output is correct
6 Correct 80 ms 169380 KB Output is correct
7 Correct 76 ms 169312 KB Output is correct
8 Correct 76 ms 169412 KB Output is correct
9 Correct 80 ms 169424 KB Output is correct
10 Correct 78 ms 169428 KB Output is correct
11 Correct 79 ms 169532 KB Output is correct
12 Correct 79 ms 169540 KB Output is correct
13 Correct 77 ms 169616 KB Output is correct
14 Correct 88 ms 169760 KB Output is correct
15 Correct 91 ms 169932 KB Output is correct
16 Correct 129 ms 184076 KB Output is correct
17 Correct 130 ms 183884 KB Output is correct
18 Correct 130 ms 184228 KB Output is correct
19 Correct 264 ms 211544 KB Output is correct
20 Correct 256 ms 211248 KB Output is correct
21 Correct 327 ms 233832 KB Output is correct
22 Correct 314 ms 235076 KB Output is correct
23 Correct 320 ms 235564 KB Output is correct
24 Runtime error 389 ms 262144 KB Execution killed with signal 9
25 Halted 0 ms 0 KB -