P2782 友好城市

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值