B. Dreamoon Likes Permutations -------------------------------------- Thinking (simulation)

Insert picture description here
Insert picture description here

Topic:
Given n number ranges [1, n-1] let you find the split point, so that the number on the left is arranged from 1, and the number on the right is also analyzed from 1
:

Use two sets to maintain the left and right sets
. The maximum number of conditions == interval length && maximum number == interval number

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+1000;
int a[N];
int t,n;
vector<pair<int,int> > v;
int main()
{
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		v.clear(); 
		for(int i=1;i<=n;i++) scanf("%d",&a[i]);
		map<int,int> mp;
		set<int> s1,s2;
		for(int i=1;i<=n;i++)
		{
			mp[a[i]]++;
			s2.insert(a[i]);
		}
		for(int i=1;i<=n;i++)
		{
			if(mp[a[i]]==2)
			{
				s1.insert(a[i]);
				mp[a[i]]--;
				continue;
			}
			if(s1.size())
			{
				int mx1=*s1.rbegin();
				int mx2=*s2.rbegin();
				if(i-1==mx1&&mx2==(n-i+1)&&mx1==s1.size()&&mx2==s2.size())
				{
					v.push_back({i-1,n-i+1});
				}
			}
			if(mp[a[i]]==1)
			{
				s1.insert(a[i]);
				s2.erase(a[i]);
			}
		}
		cout<<v.size()<<endl; 
		for(auto it : v)
		{
			cout<<it.first<<" "<<it.second<<endl;
		 } 
	}
}
Published 572 original articles · praised 14 · 10,000+ views

Guess you like

Origin blog.csdn.net/qq_43690454/article/details/105330478