1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| #include <bits/stdc++.h> using namespace std; template <typename T> inline void read(T &a){ T w=1; a=0; char ch=getchar(); for(;ch < '0' || ch > '9';ch=getchar()) if(ch == '-') w=-1; for(;ch>='0'&&ch<='9';ch=getchar()) a=(a<<3)+(a<<1)+(ch^48); a*=w; } template <typename T> inline void ckmax(T &a, T b){a = a > b ? a : b;} template <typename T> inline void ckmin(T &a, T b){a = a < b ? a : b;} #define fi first #define se second #define pb push_back #define mp make_pair #define mii map<int, int> #define pii pair<int, int> #define vi vector<int> #define Debug(x) cout << #x << " = " << x << endl #define For(i,l,r) for (int i = l; i <= r; ++i) #define foR(i,l,r) for (int i = l; i >= r; --i) const int N = 5e5 + 10; struct Node { int x, y; Node (int X = 0, int Y = 0) { x = X, y = Y; } bool operator < (const Node &a) const { if ( x == a.x ) return y < a.y; return x < a.x; } } a[N];
vi dot[N]; int n; pii sta[N]; vector < pii > dp; inline int Dp (int op) { int cur = 0; dp.clear(); dp.pb(mp(0, 0)); For (i, 1, N - 10) { if ( (i ^ op) & 1 ) { int p = dp.size() - 1, tmp = dp[0].se; int top = 0, sz = dot[i].size() - 1; foR (j, sz, 0) { int x = dot[i][j]; while (p >= 0 && dp[p].fi >= x) ckmax(tmp, dp[p--].se); sta[++top] = mp(x, tmp + j + 1); } while ( p >= 0 ) ckmax(tmp, dp[p--].se); dp.clear(); dp.pb(mp(0, tmp)); reverse (sta + 1, sta + top + 1); For (j, 1, top) dp.pb(sta[j]); } else { int tmp = 0, p = 0, sz = dot[i].size() - 1; int top = 0; For (j, 0, sz) { int x = dot[i][j]; while (p < dp.size() && dp[p].fi <= x) ckmax(tmp, dp[p++].se); sta[++top] = mp(x, tmp + sz - j + 1); } while ( p < dp.size() ) ckmax(tmp, dp[p++].se); dp.clear(); dp.pb(mp(0, tmp)); For (j, 1, top) dp.pb(sta[j]); } } int res = 0; for (auto v : dp) ckmax(res, v.se); return res; }
int main() { read(n); For (i, 1, n) read(a[i].x), read(a[i].y); sort (a + 1, a + n + 1); For (i, 1, n) dot[a[i].y].pb(a[i].x); printf ("%d\n", n - (n - Dp(0) + n - Dp(1) )); return 0; }
|