双指针,70分感觉没问题呀
查看原帖
双指针,70分感觉没问题呀
684890
Rhss楼主2022/8/12 11:23
#include <bits/stdc++.h>
using namespace std;
int main(){
	int t;
	cin>>t;
	vector<int> vi;
	int x;
	int w;
	while(t--){
		vi.clear();
		cin>>w;
		int len = w;
		vi.push_back(0);
		while(w--){
			cin>>x;
			vi.push_back(x);	
		}
		int ans = 0;
		int i = 1;
		while(i<=len){
			if(vi[i]==i){
				i++;
			}else{
				int maxv = vi[i];
				int j = i+1;
				maxv=max(maxv,vi[j]);
				while(maxv>j){
					j++;
					maxv=max(maxv,vi[j]);
				}
				ans = j-i+1;
				i=j+1;
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

2022/8/12 11:23
加载中...