求助求助,RE是为什么
查看原帖
求助求助,RE是为什么
933665
294allin楼主2024/2/29 15:36
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e6+1;
int s[maxn];//模拟栈
int height[maxn];
int main()
{
	int x;
	int num=0;
//={0,389,207,155,300,299,170,158,65};

	do{
		scanf("%d",&x);
		height[++num]=x;
	}while(getchar()!='\n');
	
	int pos=0;//栈顶位置
	//1.求最长不上升子序列的长度
	
	
	for(int i=1;i<=num;i++)
	{
		if(!pos)//栈空,则直接压入
		{
			s[++pos]=height[i];
		}
		//10 7 6 3,压4 10 7 6 4而不是10 7 4 3
		else//栈不空,若当前高度小于等于栈顶,则直接入栈,否则
			//在栈中用二分查找找到一个最大的小于该数的数字,替换
		{
			if(height[i]<=s[pos])
			{
				s[++pos]=height[i];
			}
			else
			{
				int low=1,high=pos;
				while(low<high)
				{
					int mid=(low+high)/2;
					if(s[mid]<height[i])
						high=mid;
					else
						low=mid+1;
				}
				s[low]=height[i];
			}
		}
	}
	cout<<pos<<endl;
	
	pos=0;
	for(int i=1;i<=num;i++)
	{
		if(!pos)//栈空,则直接压入
		{
			s[++pos]=height[i];
		}
		//10 7 6 3,压4 10 7 6 4而不是10 7 4 3
		else//栈不空,若当前高度大于栈顶,则直接入栈,否则
			//在栈中用二分查找找到一个最小的大于该数的数字,替换
		{//3 6 7 10,压4,3 4 7 10,而不是4 6 7 10
			if(height[i]>s[pos])
			{
				s[++pos]=height[i];
			}
			else
			{
				int low=1,high=pos;
				while(low<high)
				{
					int mid=(low+high)/2;
					if(s[mid]<height[i])
						low=mid+1;
					else
						high=mid;
				}
				s[low]=height[i];
			}
		}
	}
	cout<<pos<<endl;
	return 0;
	
}
2024/2/29 15:36
加载中...