답안 #668525

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
668525 2022-12-04T03:24:41 Z BlackJack Swap (BOI16_swap) C++14
100 / 100
908 ms 233956 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]};
                }
            }
        }
        if((i<<1|1) <= N) {
            F0R(j, LG<<1) {
                if(need[i<<1][j]) vi().swap(dp[i<<1][j]);
                if(need[i<<1|1][j]) vi().swap(dp[i<<1|1][j]);
            }
        }
    }
    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 84 ms 169380 KB Output is correct
2 Correct 74 ms 169272 KB Output is correct
3 Correct 86 ms 169292 KB Output is correct
4 Correct 73 ms 169348 KB Output is correct
5 Correct 75 ms 169324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 84 ms 169380 KB Output is correct
2 Correct 74 ms 169272 KB Output is correct
3 Correct 86 ms 169292 KB Output is correct
4 Correct 73 ms 169348 KB Output is correct
5 Correct 75 ms 169324 KB Output is correct
6 Correct 75 ms 169268 KB Output is correct
7 Correct 87 ms 169404 KB Output is correct
8 Correct 77 ms 169332 KB Output is correct
9 Correct 74 ms 169316 KB Output is correct
10 Correct 73 ms 169396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 84 ms 169380 KB Output is correct
2 Correct 74 ms 169272 KB Output is correct
3 Correct 86 ms 169292 KB Output is correct
4 Correct 73 ms 169348 KB Output is correct
5 Correct 75 ms 169324 KB Output is correct
6 Correct 75 ms 169268 KB Output is correct
7 Correct 87 ms 169404 KB Output is correct
8 Correct 77 ms 169332 KB Output is correct
9 Correct 74 ms 169316 KB Output is correct
10 Correct 73 ms 169396 KB Output is correct
11 Correct 78 ms 169500 KB Output is correct
12 Correct 83 ms 169532 KB Output is correct
13 Correct 81 ms 169532 KB Output is correct
14 Correct 81 ms 169600 KB Output is correct
15 Correct 80 ms 169672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 84 ms 169380 KB Output is correct
2 Correct 74 ms 169272 KB Output is correct
3 Correct 86 ms 169292 KB Output is correct
4 Correct 73 ms 169348 KB Output is correct
5 Correct 75 ms 169324 KB Output is correct
6 Correct 75 ms 169268 KB Output is correct
7 Correct 87 ms 169404 KB Output is correct
8 Correct 77 ms 169332 KB Output is correct
9 Correct 74 ms 169316 KB Output is correct
10 Correct 73 ms 169396 KB Output is correct
11 Correct 78 ms 169500 KB Output is correct
12 Correct 83 ms 169532 KB Output is correct
13 Correct 81 ms 169532 KB Output is correct
14 Correct 81 ms 169600 KB Output is correct
15 Correct 80 ms 169672 KB Output is correct
16 Correct 129 ms 174360 KB Output is correct
17 Correct 130 ms 174412 KB Output is correct
18 Correct 136 ms 174580 KB Output is correct
19 Correct 249 ms 183628 KB Output is correct
20 Correct 247 ms 183404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 84 ms 169380 KB Output is correct
2 Correct 74 ms 169272 KB Output is correct
3 Correct 86 ms 169292 KB Output is correct
4 Correct 73 ms 169348 KB Output is correct
5 Correct 75 ms 169324 KB Output is correct
6 Correct 75 ms 169268 KB Output is correct
7 Correct 87 ms 169404 KB Output is correct
8 Correct 77 ms 169332 KB Output is correct
9 Correct 74 ms 169316 KB Output is correct
10 Correct 73 ms 169396 KB Output is correct
11 Correct 78 ms 169500 KB Output is correct
12 Correct 83 ms 169532 KB Output is correct
13 Correct 81 ms 169532 KB Output is correct
14 Correct 81 ms 169600 KB Output is correct
15 Correct 80 ms 169672 KB Output is correct
16 Correct 129 ms 174360 KB Output is correct
17 Correct 130 ms 174412 KB Output is correct
18 Correct 136 ms 174580 KB Output is correct
19 Correct 249 ms 183628 KB Output is correct
20 Correct 247 ms 183404 KB Output is correct
21 Correct 327 ms 189572 KB Output is correct
22 Correct 312 ms 189660 KB Output is correct
23 Correct 336 ms 189880 KB Output is correct
24 Correct 899 ms 233812 KB Output is correct
25 Correct 908 ms 233956 KB Output is correct