题目:https://codeforces.com/gym/103202/problem/F
Kobolds are rat-like, candle-loving cave folk, digging deep beneath the surface for millennia. Today, they gather together in a queue to explore yet another tunnel in their catacombs!
But just before the glorious movement initiates, they have to arrange themselves in non-descending heights. The shortest is always the leader digging small holes, and the followers swelling it.
The kobolds are hyperactive; they like to move here and there. To make the arrangement easier, they decide to group themselves into consecutive groups first, then reorder in each group.
What’s the maximum number of consecutive groups they can be partitioned into, such that after reordering the kobolds in each group in non-descending order, the entire queue is non-descending?
For example, given a queue of kobolds of heights [1, 3, 2, 7, 4], we can group them into three consecutive groups ([1] [3, 2] [7, 4]), such that after reordering each group, the entire queue can be non-descending.
Input
The first line of the input contains a single integer n (1≤n≤106), denoting the number of kobolds.
The second line contains n integers a1,a2,…,an (1≤ai≤109), representing the heights of the kobolds in the queue.
Output
Print a single integer, denoting the maximum number of groups.
Example
input
5
1 3 2 7 4
output
3
题目意思:
给你一组数据,划分区间组,然后对每个区间排序,最后连接起来是递增的,让你求最大能划分多少组
//我个菜鸡卡半年。。
AC代码:
#include <iostream>
#include <algorithm>
using namespace std;
int a[1000100]; //储存原来的数据
int b[1000100]; //储存排序后的数据
int main() {
int n;
scanf("%d",&n); //数据个数
for (int i = 0; i < n; i++) //输入
scanf("%d",a+i),b[i] = a[i];
sort(b, b + n);
int ans = 0; //结果
//用于查找区间
long long sum1 = 0;
long long sum2 = 0;
for (int i = 0; i < n; i++) { //遍历
sum1 += a[i];
sum2 += b[i];
if (sum1 == sum2) ans++; //根据未排序每个数的和,已排序每个数的和是否相等来确定划分的区间
}
cout << ans;
return 0;
}