求调自创新思路,代码RE
查看原帖
求调自创新思路,代码RE
1101266
Fe_CuSO4__FeSO4_Cu楼主2024/8/29 09:24
#include <bits/stdc++.h>
using namespace std;
struct node
{
	vector<int>l;
	int n;
};
vector<int>a;
int maxn=INT_MIN,n;
void bfs()
{
	queue<node>q;
	node h,t;
	for(int i=0;i<a.size();i++)//初始化第i个元素为最长上升子序列中的第一个元素
	{
		h.l.clear();
		h.l.push_back(a[i]);
		h.n=i;
		q.push(h);
	}
	while(!q.empty())
	{
		h=q.front(),q.pop();
		if(h.n==n) maxn=max(maxn,int(h.l.size()));
		if(a[h.n+1]<=h.l[h.l.size()-1])//指针指的数<=序列的最后一个数
		{
			t.l=h.l,t.n=h.n+1;
			while(a[t.n]<=t.l[t.l.size()-1])
			{
				t.l.pop_back();//方案1:删除原序列中<=当前指针的数,直到>
			}
			t.l.push_back(a[t.n]);
			q.push(t);
			t.l=h.l,t.n=h.n+1;
			q.push(t);//方案2:原序列不变,指针+1;
		}
		else//指针指的数>序列的最后一个数
		{
			t.l=h.l,t.n=h.n+1;
			t.l.push_back(a[t.n]);
			q.push(t);
		}
	}
}
int main()
{
	int n;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
	}
	bfs();
	cout<<maxn;
	return 0;
}
2024/8/29 09:24
加载中...