F. Kobolds and Catacombs

这篇文章介绍了一种解决Codeforces竞赛题目的方法,涉及将一组高度不等的Kobolds按非降序重新排列,通过划分连续非降序区间的技巧,找到最大划分数量。博主分享了AC代码和关键步骤,适合算法爱好者学习群组排序优化问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目: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;
}
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值