F- Kobolds and Catacombs

题意:给定一个数组,可以对任意一个连续子段非递减排序,问最多可以排几次,使原序列非递减

例: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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值