Submission #1866019
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
#define null NULL
#define mp make_pair
#define pb(a) push_back(a)
#define sz(a) ((int)(a).size())
#define all(a) a.begin() , a.end()
#define fi first
#define se second
#define relaxMin(a , b) (a) = min((a),(b))
#define relaxMax(a , b) (a) = max((a),(b))
#define SQR(a) ((a)*(a))
#define PI 3.14159265358979323846
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;
const int MAXN = 262144;
const int oo = 1E9;
int n, in[MAXN], rpos[MAXN];
pii tree[2 * MAXN]; // even, odd
void Init(){
for(int i = 0;i < n;++i){
tree[MAXN + i] = mp(oo, oo);
if(i & 1) tree[MAXN + i].se = in[i];
else tree[MAXN + i].fi = in[i];
}
for(int i = MAXN - 1;i > 0;--i){
tree[i].fi = min(tree[2 * i].fi, tree[2 * i + 1].fi);
tree[i].se = min(tree[2 * i].se, tree[2 * i + 1].se);
}
}
pii Get(int l, int r){
l += MAXN, r += MAXN;
pii res = tree[l];
relaxMin(res.fi, tree[r].fi);
relaxMin(res.se, tree[r].se);
while(l != r){
int pl = l >> 1, pr = r >> 1;
if(pl == pr) break;
relaxMin(res.fi, tree[l + 1].fi);
relaxMin(res.se, tree[l + 1].se);
relaxMin(res.fi, tree[r - 1].fi);
relaxMin(res.se, tree[r - 1].se);
l = pl, r = pr;
}
return res;
}
pii Solve(const pii& u){
int x, y;
if(u.fi & 1){
x = Get(u.fi, u.se).se;
y = Get(rpos[x] + 1, u.se).fi;
} else {
x = Get(u.fi, u.se).fi;
y = Get(rpos[x] + 1, u.se).se;
}
return {x, y};
}
int main(){
scanf("%d", &n);
for(int i = 0;i < n;++i){
scanf("%d", &in[i]);
rpos[in[i]] = i;
}
Init();
set<pair<pii, pii>> ival;
ival.insert(mp(Solve(mp(0, n - 1)), mp(0, n - 1)));
vi res;
while(!ival.empty()){
auto cur = *ival.begin();
ival.erase(cur);
//cout << cur.fi.fi << " : " << cur.fi.se << " | ";
//cout << cur.se.fi << " : " << cur.se.se << endl;
//system("pause");
res.pb(cur.fi.fi);
res.pb(cur.fi.se);
int x = cur.fi.fi;
int y = cur.fi.se;
int px = rpos[x];
int py = rpos[y];
if(cur.se.fi < px){
int a = cur.se.fi, b = px - 1;
ival.insert(mp(Solve(mp(a, b)), mp(a, b)));
}
if(px + 1 <= py - 1){
int a = px + 1, b = py - 1;
ival.insert(mp(Solve(mp(a, b)), mp(a, b)));
}
if(py < cur.se.se){
int a = py + 1, b = cur.se.se;
ival.insert(mp(Solve(mp(a, b)), mp(a, b)));
}
}
for(int i = 0;i < sz(res);++i)
printf("%d%c", res[i], i + 1 == sz(res) ? '\n' : ' ');
return 0;
}
Submission Info
Submission Time |
|
Task |
E - Young Maids |
User |
v_haralampiev |
Language |
C++14 (GCC 5.4.1) |
Score |
0 |
Code Size |
2617 Byte |
Status |
TLE |
Exec Time |
2104 ms |
Memory |
269912 KB |
Compile Error
./Main.cpp: In function ‘int main()’:
./Main.cpp:69:18: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
^
./Main.cpp:71:24: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &in[i]);
^
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
0 / 800 |
Status |
|
|
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 |
3 ms |
6400 KB |
0_01.txt |
AC |
3 ms |
6400 KB |
0_02.txt |
AC |
5 ms |
6400 KB |
1_00.txt |
AC |
3 ms |
6400 KB |
1_01.txt |
AC |
3 ms |
6400 KB |
1_02.txt |
TLE |
2104 ms |
139484 KB |
1_03.txt |
AC |
52 ms |
8564 KB |
1_04.txt |
TLE |
2104 ms |
139228 KB |
1_05.txt |
TLE |
2104 ms |
138460 KB |
1_06.txt |
TLE |
2104 ms |
138076 KB |
1_07.txt |
TLE |
2104 ms |
269144 KB |
1_08.txt |
TLE |
2104 ms |
138972 KB |
1_09.txt |
TLE |
2104 ms |
269528 KB |
1_10.txt |
TLE |
2104 ms |
269784 KB |
1_11.txt |
TLE |
2104 ms |
269912 KB |
1_12.txt |
TLE |
2104 ms |
268632 KB |
1_13.txt |
TLE |
2103 ms |
72160 KB |
1_14.txt |
TLE |
2104 ms |
73824 KB |
1_15.txt |
TLE |
2104 ms |
139100 KB |
1_16.txt |
TLE |
2103 ms |
72288 KB |
1_17.txt |
TLE |
2103 ms |
73696 KB |
1_18.txt |
TLE |
2104 ms |
73952 KB |
1_19.txt |
TLE |
2104 ms |
138076 KB |