hdu 1257 最少拦截系统

#include <bits/stdc++.h>
using namespace std;
int a[10010],c,n,h,i;
int main() {
    while (scanf("%d",&n)==1) {
        c = 1;
        memset(a, 0, sizeof(a));
        scanf("%d",&h);
        a[1] = h;
        for ( i = 2; i <= n; ++i) {
            scanf("%d",&h);
            if (h > a[c]) a[++c] = h;
            else *lower_bound(a+1,a+c+1,h) = h;
        }
        printf("%d\n",c);
    }
    return 0;
}
贪心算法+二分查找
#include<bits/stdc++.h> 
using namespace std;
#define M 100010
int f[M];
int main()
{
    int i,a,n,t=0;
    int l,r,mid;
    while(~scanf("%d",&n))
    {
        t=1;
        while(n--)
        {
            scanf("%d",&a);
            l=0,r=t;
            while(l<=r)     
            {
                mid=(l+r)/2;    
                if(f[mid]==a)
                {
                    l=mid; break;
                }
                else if(f[mid]<=a) l=mid+1;
                else r=mid-1;
            }
            if(l<t) f[l]=a;
            else  f[t++]=a;
        }
        printf("%d\n",t-1);
    }
    return 0;
}
LIS
#include<bits/stdc++.h>
using namespace std;
const int M = 10005;
int n,h[M];
int LIS(){
	int ans = 1;int dp[M];dp[1] = 1;
	for(int i = 2;i<=n;i++){
		int max = 0;
		for(int j = 1;j<i;j++)
		if(dp[j]>max&&h[j]<h[i]) max = dp[j];
		dp[i] = max+1;
		if(ans<dp[i]) ans = dp[i];
	}
	return ans;
}
int main (){
	while(cin>>n){
		for(int i = 1;i<=n;i++) cin>>h[i];
		cout<<LIS()<<endl;
	}
	return 0;
}

```[添加链接描述](http://acm.hdu.edu.cn/showproblem.php?pid=1257)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值