栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

51nod P3111 小明爱拦截【树状数组】【DP】

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

51nod P3111 小明爱拦截【树状数组】【DP】

题目

思路

这道题就是拦截导弹
然后可以用树状数组优化
拦截导弹的本质就是求最长不上升子序列。
然后把求最长不下降序列的数组倒过来求就好了。

代码
#include
#include
#include
using namespace std;
long long n,ans,c[2000020];
long long f[2000020];
pair  a[2000020];
void insert(long long x,long long y)
{
	while(x<=200020)
	 {
	    c[x]=max(c[x],y);
	    x+=(x&-x);
	 }
}
long long query(long long x)
{
	long long maxn=0;
	while(x)
     {
     	maxn=max(maxn,c[x]);
     	x-=(x&-x);
	 }
	return maxn;
}
int main()
{
	scanf("%lld",&n);
	for(int i=1; i<=n; i++)
	 {
	   scanf("%lld",&a[n-i+1].first);
	   a[n-i+1].second=n-i+1;
	 }
	sort(a+1,a+1+n);
	for(int i=1; i<=n; i++)
	 {
	   f[i]=query(a[i].second)+1;
	   insert(a[i].second,f[i]);
	 }
	for(int i=1; i<=n; i++)
	   ans=max(ans,f[i]);
	printf("%lld",ans);
	return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/702793.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号