Submission #1532928


Source Code Expand

#include <bits/stdc++.h>
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define FORR(i,a,b) for (int i=(a);i>=(b);i--)
#define pb push_back
#define pcnt __builtin_popcount
#define show(x) cout<<#x<<" = "<<x<<endl;
#define maxs(x,y) x = max(x,y)
#define mins(x,y) x = min(x,y)
#define fi first
#define se second
#define rng(a) a.begin(),a.end()
#define each(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define sz(x) (int)(x).size()

using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef set<int> si;
typedef pair<ll,ll> pll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<pll> vpll;
typedef set<ll> sl;
template<typename T>string join(vector<T>&v)
{stringstream s;FOR(i,0,sz(v))s<<' '<<v[i];return s.str().substr(1);}

const int iinf = 1111111111;
const ll linf = 4e18;
const int mod = 1e9+7;
const double eps = 1e-10;

struct segtree{
  typedef int T;
  T e = iinf;
  T merge(const T& a, const T& b){return min(a, b);}
  //###################################################
  int n;vector<T> d;
  void init(int _n){n=1; while(n<_n)n*=2;d.resize(2*n-1);FOR(i,0,2*n+1)d[i]=e;}
  void update(int k,T a){k+=n-1;d[k]=a;while(k>0){k=(k-1)/2;d[k]=merge(d[k*2+1],d[k*2+2]);}}
  T q(int a,int b,int k,int l,int r){return (r<=a||b<=l)?e:(a<=l&&r<=b)?d[k]:merge(q(a,b,k*2+1,l,(l+r)/2),q(a,b,k*2+2,(l+r)/2,r));}
  T query(int a,int b){return q(a,b,0,0,n);}
};

segtree seg[2];
int n, p[200005], ip[200005];
typedef pair<pii, pii> qi;
priority_queue<qi, vector<qi>, greater<qi> > que;
vi v;

qi to_qi(pii& p){
  int l = p.fi, r = p.se;
  int _l = seg[l%2].query(l/2, r/2);
  int _r = seg[1-l%2].query((ip[_l]+1)/2, (r+1)/2);
  return qi(pii(_l, _r), pii(l, r));
}

main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  FOR(i, 0, 2)seg[i].init(n/2);
  FOR(i, 0, n){
    cin >> p[i];
    ip[p[i]] = i;
    seg[i%2].update(i/2, p[i]);
  }
  pii t1 = pii(0, n), t2, t3;
  qi tmp;
  que.push(to_qi(t1));
  while(!que.empty()){
    tmp = que.top();
    que.pop();
    t1 = tmp.fi;
    t2 = tmp.se;
    v.pb(t1.fi);
    v.pb(t1.se);
    if(ip[t1.fi] > t2.fi){
      t3 = pii(t2.fi, ip[t1.fi]);
      que.push(to_qi(t3));
    }
    if(ip[t1.se] > ip[t1.fi] + 1){
      t3 = pii(ip[t1.fi] + 1, ip[t1.se]);
      que.push(to_qi(t3));
    }
    if(t2.se > ip[t1.se] + 1){
      t3 = pii(ip[t1.se] + 1, t2.se);
      que.push(to_qi(t3));
    }
  }
  cout << join(v) << endl;
}

Submission Info

Submission Time
Task E - Young Maids
User char134217728
Language C++14 (GCC 5.4.1)
Score 800
Code Size 2592 Byte
Status AC
Exec Time 97 ms
Memory 9208 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 800 / 800
Status
AC × 3
AC × 23
Set Name Test Cases
Sample 0_00.txt, 0_01.txt, 0_02.txt
All 0_00.txt, 0_01.txt, 0_02.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 1_18.txt, 1_19.txt
Case Name Status Exec Time Memory
0_00.txt AC 1 ms 256 KB
0_01.txt AC 1 ms 256 KB
0_02.txt AC 1 ms 256 KB
1_00.txt AC 1 ms 256 KB
1_01.txt AC 1 ms 256 KB
1_02.txt AC 76 ms 8568 KB
1_03.txt AC 68 ms 8568 KB
1_04.txt AC 82 ms 8568 KB
1_05.txt AC 97 ms 9080 KB
1_06.txt AC 96 ms 9076 KB
1_07.txt AC 96 ms 9208 KB
1_08.txt AC 96 ms 9036 KB
1_09.txt AC 72 ms 8568 KB
1_10.txt AC 83 ms 8568 KB
1_11.txt AC 82 ms 8572 KB
1_12.txt AC 93 ms 8824 KB
1_13.txt AC 95 ms 9080 KB
1_14.txt AC 96 ms 8952 KB
1_15.txt AC 95 ms 9080 KB
1_16.txt AC 95 ms 9080 KB
1_17.txt AC 96 ms 9080 KB
1_18.txt AC 95 ms 8952 KB
1_19.txt AC 96 ms 8952 KB