Rockwell CF1385C Make It Good

Rockwell CF1385C Make It Good

Idea: For this question, we can start with the sample, observe the good sequence in the sample, find the law of the good sequence, and draw the following diagram:

A good sequence of 1 set of data: A good sequence of
Insert picture description here
2 sets of data: A good sequence of
Insert picture description here
3 sets of data: A good sequence of
Insert picture description here
4 sets of data:
Insert picture description here

A good sequence of 5 sets of data:
Insert picture description here

From the above 5 analysis figures in the sample, it can be seen that good sequences are divided into several categories:
1. The sequence is ordered (ascending and descending orders are acceptable ).
2. There is a trend of rising first and then falling (similar to the shape of a mountain peak, or there can be equal adjacent elements, as the example tells us).
So we can scan from back to front and mark. If the current is in descending order, mark it, which means that ascending order is no longer possible. If the mark appears and it appears in ascending order, exit, and then we set the current exit position to -1 This is the number prefix we want to delete (of course, if the sequence itself meets the exit conditions and does not touch the exit conditions, it will exit naturally, and there is no need to worry about negative numbers).
Code

#include<bits/stdc++.h>
using namespace std;
int t,n,a[200001],i;
bool f;
int main(){
    
    
	cin>>t;
	while(t--){
    
    
		f=false;
		cin>>n;
		for(i=1;i<=n;i++)
			cin>>a[i];
		for(i=n;i>0;i--){
    
    
			if(a[i]>a[i-1])
				f=true;
			if(a[i]<a[i-1]&&f){
    
    
				i--;
				break;	
			}
		}
		cout<<i<<endl;
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_52536621/article/details/113868323