P1020 [NOIP 1999 提高组] 导弹拦截(dilworth)


这道题真的做的我鬼火冒,尤其是这个第二问要用到dilworth但是我看讲解完全不知道他们在讲什么,我看了好久才理解,一个数组至少可以由几个不增子序列覆盖就等于严格单调递增的最长子序列的长度,如果是至少可以由几个严格递减子序列覆盖就等于最长单调不减子序列的长度,然后对于第一个问题求最长不增子序列我们将数组中的元素乘以-1,这样就可以求最长不减子序列的长度
要注意的是如果是求严格单调递增子序列用lower_bound,求非严格递增子序列要用upper_bound并且判断该位置是否等于-x如果等于则将该位置后面的一个数变为-x

#include<iostream>
#include<set>
#include<map>
#include<algorithm>
#include<vector>
#include<cmath>
#include<climits>
#include<cstring>
#define int long long
const int N = 1e6+5;
using namespace std;
char* p1, * p2, buf[100000];
#define nc() (p1==p2 && (p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
int read()
{
	int x = 0, f = 1;
	char ch = nc();
	while (ch < 48 || ch>57)
	{
		if (ch == '-')
			f = -1;
		ch = nc();
	}
	while (ch >= 48 && ch <= 57)
		x = x * 10 + ch - 48, ch = nc();
	return x * f;
}
int a[N],b[N];
vector<int>v[N];
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int cnt1 = 0;
	int cnt2 = 0;
	int cnt3 = 0;
	int x;
	while (cin >> x) {
		if (!cnt1)a[++cnt1] = -x;
		else if (-x>=a[cnt1])a[++cnt1] = -x;
		else {
			int it = upper_bound(a + 1, a + 1 + cnt1, -x) - a;
			if (a[it] != -x)a[it] = -x;
			else a[it + 1] = -x;
		
		}
		if (!cnt2)b[++cnt2] = x;
		else if (x > b[cnt2])b[++cnt2] = x;
		else {
			int it = lower_bound(b + 1, b + 1 + cnt2, x) - b;
			b[it] = x;
		}
		
	}

	cout << cnt1 << endl<<cnt2;
	return 0;
}
posted @   郭轩均  阅读(2)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示