#include<bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef pair<pair<int,int>,pair<int,int>>kwad;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const int LIM=8007, INF=1e9+7;
int P[LIM], tr[4*LIM], N=1, n;
pair<int,int>K[LIM];
void upd(int v, int x) {
v+=N;
while(v) {
tr[v]=min(tr[v], x);
v/=2;
}
}
int cnt(int l, int r) {
if(l>r) return INF;
l+=N; r+=N;
int ans=min(tr[l], tr[r]);
while(l/2!=r/2) {
if(l%2==0) ans=min(ans, tr[l+1]);
if(r%2==1) ans=min(ans, tr[r-1]);
l/=2; r/=2;
}
return ans;
}
pair<int,int>obr(pair<int,int>x) {
return {n-1-x.nd, x.st};
}
kwad obrk(kwad x) {
x={obr(x.st), obr(x.nd)};
if(x.st.st>x.nd.st) swap(x.st.st, x.nd.st);
if(x.st.nd>x.nd.nd) swap(x.st.nd, x.nd.nd);
return x;
}
vector<kwad>solve(vector<kwad>T) {
vector<kwad>V;
rep(i, n) K[i]={i, P[i]};
rep(xd, 4) {
vector<int>mi(n, INF);
vector<pair<int,int>>S;
rep(i, n) S.pb({K[i].st-K[i].nd, -i-1});
rep(i, T.size()) S.pb({T[i].st.st-T[i].st.nd, i+1});
sort(all(S));
reverse(all(S));
rep(i, 2*N) tr[i]=INF;
for(auto i : S) {
int p=abs(i.nd)-1;
if(i.nd<0) mi[p]=min(mi[p], cnt(K[p].nd+1, n-1)-K[p].st);
else upd(T[p].st.nd, T[p].nd.st);
}
reverse(all(S));
rep(i, 2*N) tr[i]=INF;
for(auto i : S) {
int p=abs(i.nd)-1;
if(i.nd<0) mi[p]=min(mi[p], cnt(K[p].st+1, n-1)-K[p].nd);
else upd(T[p].st.st, T[p].nd.nd);
}
rep(i, n) if(K[i].st+mi[i]<n && K[i].nd+mi[i]<n) V.pb({K[i], {K[i].st+mi[i], K[i].nd+mi[i]}});
rep(i, n) K[i]=obr(K[i]);
rep(i, T.size()) T[i]=obrk(T[i]);
rep(i, V.size()) V[i]=obrk(V[i]);
}
return V;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n;
while(N<n) N*=2;
rep(i, n) {
cin >> P[i];
--P[i];
}
int ans=n;
vector<kwad>T;
rep(i, n) T.pb({{i, P[i]}, {i, P[i]}});
while(T.size()) {
--ans;
T=solve(T);
}
cout << ans << '\n';
}
Compilation message
giraffes.cpp: In function 'std::vector<std::pair<std::pair<int, int>, std::pair<int, int> > > solve(std::vector<std::pair<std::pair<int, int>, std::pair<int, int> > >)':
giraffes.cpp:6:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
6 | #define rep(a, b) for(int a = 0; a < (b); ++a)
| ^
giraffes.cpp:48:5: note: in expansion of macro 'rep'
48 | rep(i, T.size()) S.pb({T[i].st.st-T[i].st.nd, i+1});
| ^~~
giraffes.cpp:6:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
6 | #define rep(a, b) for(int a = 0; a < (b); ++a)
| ^
giraffes.cpp:66:5: note: in expansion of macro 'rep'
66 | rep(i, T.size()) T[i]=obrk(T[i]);
| ^~~
giraffes.cpp:6:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
6 | #define rep(a, b) for(int a = 0; a < (b); ++a)
| ^
giraffes.cpp:67:5: note: in expansion of macro 'rep'
67 | rep(i, V.size()) V[i]=obrk(V[i]);
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
352 KB |
Output is correct |
2 |
Correct |
1 ms |
360 KB |
Output is correct |
3 |
Correct |
1 ms |
360 KB |
Output is correct |
4 |
Correct |
0 ms |
360 KB |
Output is correct |
5 |
Correct |
0 ms |
360 KB |
Output is correct |
6 |
Correct |
0 ms |
360 KB |
Output is correct |
7 |
Correct |
1 ms |
360 KB |
Output is correct |
8 |
Correct |
0 ms |
356 KB |
Output is correct |
9 |
Correct |
0 ms |
360 KB |
Output is correct |
10 |
Correct |
1 ms |
360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
352 KB |
Output is correct |
2 |
Correct |
1 ms |
360 KB |
Output is correct |
3 |
Correct |
1 ms |
360 KB |
Output is correct |
4 |
Correct |
0 ms |
360 KB |
Output is correct |
5 |
Correct |
0 ms |
360 KB |
Output is correct |
6 |
Correct |
0 ms |
360 KB |
Output is correct |
7 |
Correct |
1 ms |
360 KB |
Output is correct |
8 |
Correct |
0 ms |
356 KB |
Output is correct |
9 |
Correct |
0 ms |
360 KB |
Output is correct |
10 |
Correct |
1 ms |
360 KB |
Output is correct |
11 |
Correct |
0 ms |
360 KB |
Output is correct |
12 |
Correct |
1 ms |
360 KB |
Output is correct |
13 |
Correct |
1 ms |
472 KB |
Output is correct |
14 |
Correct |
1 ms |
360 KB |
Output is correct |
15 |
Correct |
1 ms |
360 KB |
Output is correct |
16 |
Correct |
0 ms |
360 KB |
Output is correct |
17 |
Correct |
1 ms |
360 KB |
Output is correct |
18 |
Correct |
0 ms |
360 KB |
Output is correct |
19 |
Correct |
1 ms |
360 KB |
Output is correct |
20 |
Correct |
1 ms |
360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
352 KB |
Output is correct |
2 |
Correct |
1 ms |
360 KB |
Output is correct |
3 |
Correct |
1 ms |
360 KB |
Output is correct |
4 |
Correct |
0 ms |
360 KB |
Output is correct |
5 |
Correct |
0 ms |
360 KB |
Output is correct |
6 |
Correct |
0 ms |
360 KB |
Output is correct |
7 |
Correct |
1 ms |
360 KB |
Output is correct |
8 |
Correct |
0 ms |
356 KB |
Output is correct |
9 |
Correct |
0 ms |
360 KB |
Output is correct |
10 |
Correct |
1 ms |
360 KB |
Output is correct |
11 |
Correct |
0 ms |
360 KB |
Output is correct |
12 |
Correct |
1 ms |
360 KB |
Output is correct |
13 |
Correct |
1 ms |
472 KB |
Output is correct |
14 |
Correct |
1 ms |
360 KB |
Output is correct |
15 |
Correct |
1 ms |
360 KB |
Output is correct |
16 |
Correct |
0 ms |
360 KB |
Output is correct |
17 |
Correct |
1 ms |
360 KB |
Output is correct |
18 |
Correct |
0 ms |
360 KB |
Output is correct |
19 |
Correct |
1 ms |
360 KB |
Output is correct |
20 |
Correct |
1 ms |
360 KB |
Output is correct |
21 |
Correct |
1 ms |
360 KB |
Output is correct |
22 |
Correct |
2 ms |
344 KB |
Output is correct |
23 |
Correct |
7 ms |
344 KB |
Output is correct |
24 |
Correct |
12 ms |
348 KB |
Output is correct |
25 |
Correct |
14 ms |
344 KB |
Output is correct |
26 |
Correct |
15 ms |
348 KB |
Output is correct |
27 |
Correct |
15 ms |
576 KB |
Output is correct |
28 |
Correct |
15 ms |
348 KB |
Output is correct |
29 |
Correct |
15 ms |
344 KB |
Output is correct |
30 |
Correct |
16 ms |
564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
352 KB |
Output is correct |
2 |
Correct |
1 ms |
360 KB |
Output is correct |
3 |
Correct |
1 ms |
360 KB |
Output is correct |
4 |
Correct |
0 ms |
360 KB |
Output is correct |
5 |
Correct |
0 ms |
360 KB |
Output is correct |
6 |
Correct |
0 ms |
360 KB |
Output is correct |
7 |
Correct |
1 ms |
360 KB |
Output is correct |
8 |
Correct |
0 ms |
356 KB |
Output is correct |
9 |
Correct |
0 ms |
360 KB |
Output is correct |
10 |
Correct |
1 ms |
360 KB |
Output is correct |
11 |
Correct |
0 ms |
360 KB |
Output is correct |
12 |
Correct |
1 ms |
360 KB |
Output is correct |
13 |
Correct |
1 ms |
472 KB |
Output is correct |
14 |
Correct |
1 ms |
360 KB |
Output is correct |
15 |
Correct |
1 ms |
360 KB |
Output is correct |
16 |
Correct |
0 ms |
360 KB |
Output is correct |
17 |
Correct |
1 ms |
360 KB |
Output is correct |
18 |
Correct |
0 ms |
360 KB |
Output is correct |
19 |
Correct |
1 ms |
360 KB |
Output is correct |
20 |
Correct |
1 ms |
360 KB |
Output is correct |
21 |
Correct |
1 ms |
360 KB |
Output is correct |
22 |
Correct |
2 ms |
344 KB |
Output is correct |
23 |
Correct |
7 ms |
344 KB |
Output is correct |
24 |
Correct |
12 ms |
348 KB |
Output is correct |
25 |
Correct |
14 ms |
344 KB |
Output is correct |
26 |
Correct |
15 ms |
348 KB |
Output is correct |
27 |
Correct |
15 ms |
576 KB |
Output is correct |
28 |
Correct |
15 ms |
348 KB |
Output is correct |
29 |
Correct |
15 ms |
344 KB |
Output is correct |
30 |
Correct |
16 ms |
564 KB |
Output is correct |
31 |
Correct |
446 ms |
1448 KB |
Output is correct |
32 |
Correct |
1993 ms |
2632 KB |
Output is correct |
33 |
Correct |
3156 ms |
3692 KB |
Output is correct |
34 |
Correct |
3128 ms |
3364 KB |
Output is correct |
35 |
Correct |
3136 ms |
3416 KB |
Output is correct |
36 |
Correct |
3156 ms |
3504 KB |
Output is correct |
37 |
Correct |
3081 ms |
3332 KB |
Output is correct |
38 |
Correct |
3179 ms |
3496 KB |
Output is correct |
39 |
Correct |
3118 ms |
3504 KB |
Output is correct |
40 |
Correct |
3061 ms |
3480 KB |
Output is correct |