题意:给定一个数组,可以对任意一个连续子段非递减排序,问最多可以排几次,使原序列非递减
例:1 2 3 5 4 ,则最多排序次数的方式为:[1] [2] [3] [5 4]
算法:思维
题解:比较排序之后的数组b和原数组a可知,当某个位置在a和b中的前缀和相等的时候,就是一个有效区间。
代码:时间复杂度:O( n log(n) )
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <iterator>
#include <vector>
#include <cstring>
#include <string>
#include <cmath>
#include <deque>
#include <queue>
#include <map>
#include <unordered_map>
#include <set>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
#define endl "\n"
#define IOS ios::sync_with_stdio(0);cin.tie(0)
#define debug(x) cout<<"**"<<x<<"**"<<endl;
const double eps = 1e-6;
const double pi = acos(-1.0);
const int mod = 1000000007 ;
const int N = 1e6 + 10;
int n;
int a[N], b[N];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
freopen("out.out", "w", stdout);
#endif
IOS;
cin>>n;
for(int i = 0; i < n; i++) cin>>a[i], b[i] = a[i];
sort(b, b+n);
ll x= 0, y = 0, ans = 0;
for(int i = 0; i < n; i++)
{
x += a[i];
y += b[i];
if(x == y) ans++;
}
cout<<ans;
return 0;
}