动态规划——导弹拦截

动态规划——导弹拦截

P1020 [NOIP1999 普及组] 导弹拦截

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2HztlIwo-1648433540368)(C:\Users\86180\AppData\Roaming\Typora\typora-user-images\image-20220328100945307.png)]

解题思路


首先这道题我们需要求出最长的上升序列 和最长的非上升序列

主要用到了lower_bound 和upper_bound函数 ,前者包括了大于等于,而后者仅含大于

代码实现

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define rep(i,s,n) for(long long i=s;i<n;i++)
#define reb(i,d,n) for(long long i=n;i>=d;i--)
using namespace std;
const int SIZE = 100001;
int res[SIZE];
int dp1[SIZE];
int dp2[SIZE];
inline bool read(int& x) {
	char c = getchar();
	if (c == EOF)return false;
	while (c > '9' || c < '0')c = getchar();
	while (c >= '0' && c <= '9') {
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return true;
}
signed main()
{
	ios::sync_with_stdio(false);
	int i = 0;
	cin >> res[0];

	int len1 = 1;
	int len2 = 1;
	dp1[1] = res[0];
	dp2[1] = res[0];
	int temp;
	while (cin>>temp)
	{
		
		if (temp <= dp1[len1])
		{
			dp1[++len1] = temp;
		}
		else
		{
			*upper_bound(dp1+1, dp1+1 + len1 , temp, greater<int>()) = temp;//dp1中第一个小于temp得数 是一个下降序列
		}
		if (temp > dp2[len2])
		{

			dp2[++len2] = temp;

		}
		else
		{
			*lower_bound(dp2+1, dp2 +1+ len2 , temp) = temp;//dp2中第一个大于等于temp得数 是一个上升序列
		}

	}
	cout << len1 << endl << len2;

	return 0;


}

注意

输入 cin>>temp 需要注意

upper_bound greater 是指第一个比该数小的位置

总的来说upper和lower需要 和前面是否有等于号匹配

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值