pat-1101

From forcing the timeout to finding the pattern, hahaha~~~ 

#include<iostream>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;
const int inf=1000000000;
set<int>ans,temp;
int main(){
	int n;
	cin>>n;
	vector<int> a(n);
	for(int i=0;i<n;i++){
		scanf("%d",&a[i]);
		temp.insert(a[i]);
	}
	  int u=0,max=0,min[n],minn=inf;
	  fill(min,min+n,inf);
	  for(int i=n-1;i>=0;i--){
	  	if(a[i]<minn){
	  		minn=a[i];
	  		min[i]=minn;
		  }
	  }
	for(auto it=temp.begin();it!=temp.end();it++){
		if(a[u]>max) max=a[u];
		if(*it==a[u]&&max<=*it&&min[u]>=*it)
		ans.insert(*it);
		u++; 
	}
       /* for(int i=0;i<n;i++){
		int flag=0;
		for(int j=0;j<n;j++){
			if(i>=j){
				if(a[j]>a[i]) flag=1;
			}
			else{
				if(a[j]<a[i]) flag=1;
			}
		}
		if(flag==0){
			ans.insert(a[i]);
		}
	}*/ 
	printf("%d\n",ans.size());
	for(auto it=ans.begin();it!=ans.end();it++){
		if(it!=ans.begin())printf(" ");
		printf("%d",*it);
	}
	printf("\n");
	return 0;
}

to sum up

1. This question will directly time out 4 points if the double for loop is directly executed. See the part I commented out. Later, if it meets the conditions, the left is small and the right is large, the order is the same as in the set. Of course, this is just For entry conditions, you also need to find the maximum value on the left and the minimum value on the right. When comparing the current elements, there is no nested for loop, and the result is a dot format error, and finally a newline is added. That's it~~~

2. unorderer_set, that is, the set that omits the sorting is still useful. Map omitting the sorting is okay. After all, the sorting of the map is really not used (the truth is not used), so if you use set, you can only use set

3. Try adding a newline at the end when the format is wrong

4. Establish a list of maximum and minimum values ​​in advance to reduce the nesting of for loops, summarize it and pay attention to boundary conditions

English

no 

 

Guess you like

Origin blog.csdn.net/m0_45359314/article/details/113098182