P2782 友好城市
1本题隐晦的说这个题目是一个最长上升子序列,先对南边或者北边排个序,然后再求最长上升子序列,我也不知道自己怎么想出来的,可能就是我最近一直在写dp有关的题目吧
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<string>
#include<vector>
#include<queue>
#include<algorithm>
#include<deque>
#include<map>
#include<stdlib.h>
#include<set>
#include<iomanip>
#include<stack>
#define ll long long
#define ms(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x & -x
#define fi first
#define ull unsigned long long
#define se second
#define lson (rt<<1)
#define rson (rt<<1|1)
#define endl "\n"
#define bug cout<<"----acac----"<<endl
#define IOS ios::sync_with_stdio(false), cin.tie(0),cout.tie(0)
using namespace std;
const int maxn = 2e5+10;
const int maxm = 1.5e5 + 50;
const double eps = 1e-10;
const double inf = 0x3f3f3f3f;
const ll lnf = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e4 + 7;
const double pi = 3.141592653589;
int n;
struct node
{
int x, y;
}a[maxn];
bool cmp1(node A, node B)
{
return A.x < B.x;
}
bool cmp2(node A, node B)
{
return A.x > B.x;
}
int b[maxn];
int ans = 0, cnt = 0;
void f()
{
cnt = 0;
ms(b, 0);
b[0] = -1;
for (int i = 1; i <= n; i++)
{
if (a[i].y > b[cnt])
{
b[++cnt] = a[i].y;
}
else
{
int pos = upper_bound(b + 1, b + 1 + cnt, a[i].y) - b;
b[pos] = a[i].y;
}
}
ans = max(ans, cnt);
}
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
scanf("%d %d", &a[i].x, &a[i].y);
}
sort(a + 1, a + 1 + n, cmp1);
f();
sort(a + 1, a + 1 + n, cmp2);
f();
printf("%d", ans);
return 0;
}