Submission #588696

# Submission time Handle Problem Language Result Execution time Memory
588696 2022-07-03T21:32:49 Z MohammadAghil Swap (BOI16_swap) C++17
68 / 100
1000 ms 120148 KB
      #include <bits/stdc++.h>
  #pragma GCC optimize ("Ofast,unroll-loops")
#pragma GCC target ("avx2")
    using namespace std;
  typedef long long ll;
   typedef pair<int, int> pp;
    #define er(args ...) cerr << __LINE__ << ": ", err(new istringstream(string(#args)), args), cerr << endl
      #define per(i,r,l) for(int i = (r); i >= (l); i--)
        #define rep(i,l,r) for(int i = (l); i < (r); i++)
           #define all(x) begin(x), end(x)
              #define sz(x) (int)(x).size()
                  #define pb push_back
                      #define ss second
                           #define ff first
                                   void err(istringstream *iss){}template<typename T,typename ...Args> void err(istringstream *iss,const T &_val, const Args&...args){string _name;*iss>>_name;if(_name.back()==',')_name.pop_back();cerr<<_name<<" = "<<_val<<", ",err(iss,args...);}
void IOS(){
     cin.tie(0) -> sync_with_stdio(0);
     // #ifndef ONLINE_JUDGE
     //      freopen("in.in", "r", stdin);
     //      freopen("out.out", "w", stdout);
     // #endif
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll mod = 998244353, maxn = 2e5 + 5, lg = 35, inf = ll(1e9) + 5;
ll pw(ll a,ll b,ll md=mod){if(!b)return 1;ll k=pw(a,b>>1ll);return k*k%md*(b&1ll?a:1)%md;}

int a[maxn], n, cnt[maxn];
vector<int> cand[maxn];
pair<int, pp> nxt[maxn][lg];

int get(int u, int x){
     return lower_bound(all(cand[u]), x) - begin(cand[u]);
}

// vector<int> get(int r, int p, int lx, int rx){
//      vector<int> cand, resl = dp[r<<1][get(r<<1, lx)], resr = dp[r<<1|1][get(r<<1|1, rx)];
//      cand.reserve(sz(dp[r<<1][0]) + sz(dp[r<<1|1][0]) + 1); cand.pb(a[p]);
//      int ptr1 = 0, ptr2 = 0, st = 0;
//      while(ptr1 < sz(resl) || ptr2 < sz(resr)){
//           int k = 1<<st;
//           while(k && ptr1 < sz(resl)) cand.pb(resl[ptr1++]), k--;
//           k = 1<<st;
//           while(k && ptr2 < sz(resr)) cand.pb(resr[ptr2++]), k--;
//           st++;
//      }
//      return cand;
// }

vector<int> res;

void dfs(int r, int vl, int id){
     // if(r > n) return;
     res[id-1] = a[nxt[r][vl].ff];
     if(r<<1 <= n) dfs(r<<1, get(r<<1, nxt[r][vl].ss.ff), id<<1);
     if(r<<1 < n) dfs(r<<1|1, get(r<<1|1, nxt[r][vl].ss.ss), id<<1|1);
}

vector<int> get(int r, int p, int lx, int rx){
     res.clear();
     res.resize(cnt[r]);
     res[0] = a[p];
     dfs(r<<1, get(r<<1, lx), 2), dfs(r<<1|1, get(r<<1|1, rx), 3);
     return res;
}

void dfs(int r){
     int cnt = 0;
     if(r<<1 <= n) dfs(r<<1), cnt++;
     if(r<<1 < n) dfs(r<<1|1), cnt++;
     rep(i,0,sz(cand[r])){
          int x = cand[r][i];
          if(cnt == 0) nxt[r][i] = {x, {-1, -1}};
          else if(cnt == 1){
               nxt[r][i] = {x, {r<<1, -1}};
               if(a[x] > a[r<<1]) swap(nxt[r][i].ff, nxt[r][i].ss.ff); 
          }else{
               int lx = r<<1, rx = r<<1|1;
               if(a[x] < a[lx] && a[x] < a[rx]) nxt[r][i] = {x, {lx, rx}}; 
               else if(a[lx] < a[x] && a[x] < a[rx]) nxt[r][i] = {lx, {x, rx}};
               else if(a[lx] < a[rx] && a[rx] < a[x]) nxt[r][i] = {lx, {x, rx}};
               else {
                    nxt[r][i] = {rx, {x, lx}};
                    if(get(r, rx, x, lx) > get(r, rx, lx, x)) swap(nxt[r][i].ss.ff, nxt[r][i].ss.ss);
               }
          }
     }
}

int main(){ IOS();
     cin >> n;
     rep(i,1,n+1) cin >> a[i];
     rep(i,1,n+1){
          cand[i].pb(i);
          for(int j = i; j > 1; j >>= 1) {
               cand[i].pb(j>>1);
               if(j&1) cand[i].pb(j^1);
          }
          sort(all(cand[i]));
          while(sz(cand[i]) && cand[i].back() > n) cand[i].pop_back();
     }
     per(i,n,1) cnt[i] = 1 + (i<<1 <= n? cnt[i<<1]: 0) + (i<<1 < n? cnt[i<<1|1]: 0);
     dfs(1);
     // rep(i,1,n+1){
     //      rep(j,0,sz(cand[i])){
     //           er(i, cand[i][j]);
     //           for(int c:get(i, nxt[i][j].ff, nxt[i][j].ss.ff, nxt[i][j].ss.ss)) cerr << c << ' '; cerr << '\n';
     //      }
     // }
     for(int c: get(1, nxt[1][0].ff, nxt[1][0].ss.ff, nxt[1][0].ss.ss)) cout << c << ' '; 
     return 0;  
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 2 ms 4964 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 2 ms 4964 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 3 ms 5460 KB Output is correct
12 Correct 4 ms 5460 KB Output is correct
13 Correct 4 ms 5564 KB Output is correct
14 Correct 6 ms 5460 KB Output is correct
15 Correct 5 ms 5460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 2 ms 4964 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 3 ms 5460 KB Output is correct
12 Correct 4 ms 5460 KB Output is correct
13 Correct 4 ms 5564 KB Output is correct
14 Correct 6 ms 5460 KB Output is correct
15 Correct 5 ms 5460 KB Output is correct
16 Correct 128 ms 33588 KB Output is correct
17 Correct 124 ms 33592 KB Output is correct
18 Correct 156 ms 33792 KB Output is correct
19 Correct 283 ms 33804 KB Output is correct
20 Correct 283 ms 33628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 2 ms 4964 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 3 ms 5460 KB Output is correct
12 Correct 4 ms 5460 KB Output is correct
13 Correct 4 ms 5564 KB Output is correct
14 Correct 6 ms 5460 KB Output is correct
15 Correct 5 ms 5460 KB Output is correct
16 Correct 128 ms 33588 KB Output is correct
17 Correct 124 ms 33592 KB Output is correct
18 Correct 156 ms 33792 KB Output is correct
19 Correct 283 ms 33804 KB Output is correct
20 Correct 283 ms 33628 KB Output is correct
21 Correct 682 ms 120148 KB Output is correct
22 Correct 718 ms 119752 KB Output is correct
23 Correct 700 ms 119764 KB Output is correct
24 Execution timed out 1094 ms 94064 KB Time limit exceeded
25 Halted 0 ms 0 KB -