zoukankan      html  css  js  c++  java
  • 导弹拦截题解

    使用STL中的upper_bound和lower_bound代替二分操作

    对于问一求最长不上升子序列,问二求最长上升子序列(为什么问二是这样是我也不知道)
    巨坑的点,对于问一,f数组中的数据是下降(从大到小排序)的,必须添加greater(),二分的原则是排好序啊!!!
    终于AC代码

    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <vector>
    #include <algorithm>
    #include <cstring>
    
    using namespace std;
    int a[100005],p,f[100005],k=0;
    
    int main()
    {
    	
    	while(cin>>a[p])
    	{ 
    		p++;
    	}
    	f[0]=a[0];
    	for(int i=1;i<p;i++)
    	{
    		if(a[i]<=f[k])
    		{
    			f[++k]=a[i];
    		}
    		else
    		{
    			int x=upper_bound(f,f+k,a[i],greater<int>())-f;
    			f[x]=a[i];
    		}
    	}	
    	cout<<k+1<<endl;
    	k=0;
    	memset(f,0,sizeof(f));
    	f[0]=a[0];
    	for(int i=1;i<p;i++)
    	{
    		if(a[i]>f[k])
    		{
    			f[++k]=a[i];
    		}
    		else
    		{
    			int x=lower_bound(f,f+k,a[i])-f;
    			f[x]=a[i];
    		}
    	}	
    	cout<<k+1<<endl;
    	
    	return 0;	
    } 
    
  • 相关阅读:
    docker搭建主从复制mysql
    mysql主从复制(mariadb)
    docker搭建mysql8.0
    docker安装mysql
    终端配置kxsw
    AJAX教程
    移动端常见布局
    css为什么需要精灵图
    元素的显示与隐藏
    css网页布局总结
  • 原文地址:https://www.cnblogs.com/huaruoji/p/12951898.html
Copyright © 2011-2022 走看看