Submission #668521

# Submission time Handle Problem Language Result Execution time Memory
668521 2022-12-04T03:20:15 Z BlackJack Swap (BOI16_swap) C++14
68 / 100
393 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]};
            }
        }

//        if((i<<1|1) <= N) {
//            F0R(k, LG<<1) {
//                if(need[i<<1][k]) vi().swap(dp[i<<1][k]);
//                if(need[i<<1|1][k]) vi().swap(dp[i<<1|1][k]);
//            }
//        }
    }
    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);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 80 ms 169308 KB Output is correct
2 Correct 79 ms 169292 KB Output is correct
3 Correct 87 ms 169280 KB Output is correct
4 Correct 75 ms 169396 KB Output is correct
5 Correct 76 ms 169272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 80 ms 169308 KB Output is correct
2 Correct 79 ms 169292 KB Output is correct
3 Correct 87 ms 169280 KB Output is correct
4 Correct 75 ms 169396 KB Output is correct
5 Correct 76 ms 169272 KB Output is correct
6 Correct 76 ms 169368 KB Output is correct
7 Correct 86 ms 169304 KB Output is correct
8 Correct 93 ms 169292 KB Output is correct
9 Correct 80 ms 169460 KB Output is correct
10 Correct 78 ms 169376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 80 ms 169308 KB Output is correct
2 Correct 79 ms 169292 KB Output is correct
3 Correct 87 ms 169280 KB Output is correct
4 Correct 75 ms 169396 KB Output is correct
5 Correct 76 ms 169272 KB Output is correct
6 Correct 76 ms 169368 KB Output is correct
7 Correct 86 ms 169304 KB Output is correct
8 Correct 93 ms 169292 KB Output is correct
9 Correct 80 ms 169460 KB Output is correct
10 Correct 78 ms 169376 KB Output is correct
11 Correct 77 ms 169620 KB Output is correct
12 Correct 78 ms 169644 KB Output is correct
13 Correct 78 ms 169584 KB Output is correct
14 Correct 96 ms 169728 KB Output is correct
15 Correct 79 ms 169832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 80 ms 169308 KB Output is correct
2 Correct 79 ms 169292 KB Output is correct
3 Correct 87 ms 169280 KB Output is correct
4 Correct 75 ms 169396 KB Output is correct
5 Correct 76 ms 169272 KB Output is correct
6 Correct 76 ms 169368 KB Output is correct
7 Correct 86 ms 169304 KB Output is correct
8 Correct 93 ms 169292 KB Output is correct
9 Correct 80 ms 169460 KB Output is correct
10 Correct 78 ms 169376 KB Output is correct
11 Correct 77 ms 169620 KB Output is correct
12 Correct 78 ms 169644 KB Output is correct
13 Correct 78 ms 169584 KB Output is correct
14 Correct 96 ms 169728 KB Output is correct
15 Correct 79 ms 169832 KB Output is correct
16 Correct 129 ms 183804 KB Output is correct
17 Correct 131 ms 183520 KB Output is correct
18 Correct 130 ms 184000 KB Output is correct
19 Correct 259 ms 211300 KB Output is correct
20 Correct 276 ms 210980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 80 ms 169308 KB Output is correct
2 Correct 79 ms 169292 KB Output is correct
3 Correct 87 ms 169280 KB Output is correct
4 Correct 75 ms 169396 KB Output is correct
5 Correct 76 ms 169272 KB Output is correct
6 Correct 76 ms 169368 KB Output is correct
7 Correct 86 ms 169304 KB Output is correct
8 Correct 93 ms 169292 KB Output is correct
9 Correct 80 ms 169460 KB Output is correct
10 Correct 78 ms 169376 KB Output is correct
11 Correct 77 ms 169620 KB Output is correct
12 Correct 78 ms 169644 KB Output is correct
13 Correct 78 ms 169584 KB Output is correct
14 Correct 96 ms 169728 KB Output is correct
15 Correct 79 ms 169832 KB Output is correct
16 Correct 129 ms 183804 KB Output is correct
17 Correct 131 ms 183520 KB Output is correct
18 Correct 130 ms 184000 KB Output is correct
19 Correct 259 ms 211300 KB Output is correct
20 Correct 276 ms 210980 KB Output is correct
21 Correct 311 ms 232604 KB Output is correct
22 Correct 314 ms 233760 KB Output is correct
23 Correct 321 ms 234284 KB Output is correct
24 Runtime error 393 ms 262144 KB Execution killed with signal 9
25 Halted 0 ms 0 KB -