Code Forces 1367A Sorting Parts 题解

(原题链接:CF传送门


 

1|0题目背景(我看不懂英文嘤嘤嘤)

Sorting Parts

You have an array a of length n. You can exactly once select an integer len between 1 and n−1 inclusively.

And then sort in non-decreasing order the prefix of the array of length len and the suffix of the array of length n−len independently.

For example, if the array is a=[3,1,4,5,2], and you choose len=2, then after that the array will be equal to [1,3,2,4,5].

Could it be that after performing this operation, the array will not be sorted in non-decreasing order?

翻译:给定一个数组a,其长度为n,现在从1到n-1中选任意一个len,对len前和len后的部分分别排序。若对于每一个len,均保证排序后的数组是有序的,输出“NO”,否则输出“YES”。

输入:测试组数t,数组长度n,a[i]的值。

输出:对于每组测试,输出“YES”或“NO”。

2|0题意解析

这道题作为我打CF比赛的第一道题,也是2022-2-12CF全球赛的A题,其思路还是简单的。

首先想到的是暴力做法。

但是一看数据范围:1 ≤ t ≤ 100,2 ≤ n ≤ 104 ,1 ≤ a ≤109.

显然,用暴力妥妥的TLE

那怎么办呢?

其实不用排序(这题目跟排序没关系好吗!)

仔细观察题目就可以发现:要使得最终的数组无序,只要满足“存在一个合法的 len 使得 len 前的最大值大于 len 后的最小值”这一条件即可。

3|0核心代码

用数组maxn[MAXN] , minn[MAXN]分别表示 从 a[1]a[i] 的最大值,以及从 a[i]a[n] 的最小值。

3|1初始化 maxn[] , minn[].

 

cin>>n; for(int i=1; i<=n; i++) { cin>>a[i]; maxn[i]=max(maxn[i-1],a[i]); }//初始化 maxn[] for(int i=n; i>=1; i--) { if(i==n) { minn[i]=a[i]; }//注意这一步! if(i<n) { minn[i]=min(minn[i+1],a[i]); } }//初始化 minn[]

3|2 判断是否有序.

int flag=0;//用flag标注 for(int i=1; i<=n-1; i++) { if(maxn[i]>minn[i]) { flag=1; break; } } if(flag==1) { cout<<"YES"<<endl; } else { cout<<"NO"<<endl; }

4|0代码展示

#include<bits/stdc++.h> using namespace std; const int MAXN=1e4+5; int a[MAXN]; int maxn[MAXN]; int minn[MAXN]; int t,n; int flag=0; int main() { cin>>t; while(t--) { flag=0; cin>>n; for(int i=1; i<=n; i++) { //1 cin>>a[i]; maxn[i]=max(maxn[i-1],a[i]); }//1 for(int i=n; i>=1; i--) { //2 if(i==n) { minn[i]=a[i]; } if(i<n) { minn[i]=min(minn[i+1],a[i]); } }//2 for(int i=1; i<=n-1; i++) { //3 if(maxn[i]>minn[i]) { flag=1; break; } }//3 if(flag==1) { cout<<"YES"<<endl; } else { cout<<"NO"<<endl; } } return 0; }

 

5|0 写在最后

求赞QAQ

 

 


__EOF__

本文作者JX_weak
本文链接https://www.cnblogs.com/JX-weak/p/15889046.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   JX_weak  阅读(95)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下
点击右上角即可分享
微信分享提示