(原题链接:CF传送门


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

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”。

题意解析

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

首先想到的是暴力做法。

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

显然,用暴力妥妥的TLE

那怎么办呢?

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

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

核心代码

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

初始化 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[]

 判断是否有序.

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

代码展示

#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;
}

写在最后

求赞QAQ

Code Forces 1367A Sorting Parts 题解的更多相关文章

  1. 思维题--code forces round# 551 div.2

    思维题--code forces round# 551 div.2 题目 D. Serval and Rooted Tree time limit per test 2 seconds memory ...

  2. Code Forces 796C Bank Hacking(贪心)

    Code Forces 796C Bank Hacking 题目大意 给一棵树,有\(n\)个点,\(n-1\)条边,现在让你决策出一个点作为起点,去掉这个点,然后这个点连接的所有点权值+=1,然后再 ...

  3. Code Forces 833 A The Meaningless Game(思维,数学)

    Code Forces 833 A The Meaningless Game 题目大意 有两个人玩游戏,每轮给出一个自然数k,赢得人乘k^2,输得人乘k,给出最后两个人的分数,问两个人能否达到这个分数 ...

  4. code forces 382 D Taxes(数论--哥德巴赫猜想)

    Taxes time limit per test 2 seconds memory limit per test 256 megabytes input standard input output ...

  5. Code Forces Gym 100886J Sockets(二分)

    J - Sockets Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Valera ...

  6. Code Forces 711D Directed Roads

    D. Directed Roads time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  7. CODE FESTIVAL 2017 qual A 题解

    补一发A的题解. A - Snuke's favorite YAKINIKU 题意: 输入字符串S,如果以YAKI开头输出Yes,否则输出No. #include<bits/stdc++.h&g ...

  8. Code Forces 543A Writing Code

    题目描述 Programmers working on a large project have just received a task to write exactly mm lines of c ...

  9. Code Chef February Challenge 2019题解

    传送门 \(HMAPPY2\) 咕 话说这题居然卡\(scanf\)的么??? int T;cin>>T; while(T--){ cin>>n>>a>> ...

随机推荐

  1. JavaScript实现科学计算器

    运行效果: 可实现科学计算器的功能,如:PI,sin,cos,tan等 源代码: 1 <!DOCTYPE html> 2 <html lang="zh"> ...

  2. Solon 1.6.36 发布,更现代感的应用开发框架

    相对于 Spring Boot 和 Spring Cloud 的项目 启动快 5 - 10 倍 qps 高 2- 3 倍 运行时内存节省 1/3 ~ 1/2 打包可以缩小到 1/2 ~ 1/10(比如 ...

  3. 小米手环解锁MacOS系统笔记本MacBookPro

    通过小米手环解锁笔记本 官方windows是提供了方法的. 我目前用的MacBookPro,所以说下苹果笔记本的解锁方式. 安装软件BLEUnlock 库 安装方式: brew 安装 brew ins ...

  4. 【分享汇总】25个主题分享,360°领略OpenHarmony最新技术版图

    2021年10月,开放原子开源基金会旗下开源项目 OpenAtom OpenHarmony (以下简称"OpenHarmony") 应邀参加华为2021 HDC 开发者大会,并组织 ...

  5. Jx.Cms开发笔记(四)-改造Card组件

    在Blazor 组件库 BootstrapBlazor 中Card组件介绍中我们说过,如果我们使用了Card组件的IsCollapsible属性设置了可伸缩的话,就只能使用Text属性来设置标题文本, ...

  6. 多线程JUC并发篇常见面试详解

    @ 目录 1.JUC 简介 2.线程和进程 3.并非与并行 4.线程的状态 5.wait/sleep的区别 6.Lock 锁(重点) 1.Lock锁 2.公平非公平: 3.ReentrantLock ...

  7. k8s 新版本 部署 Ingress-nginx controller

    k8s 新版本 部署 Ingress-nginx controller 本篇主要记录一下 k8s 新版本 1.23.5 中如何搭建 ingress controller 以及里面的注意项 新版本和老版 ...

  8. XCTF练习题---MISC---Excaliflag

    XCTF练习题---MISC---Excaliflag flag:3DS{Gr4b_Only_th1s_B1ts} 解题步骤: 1.观察题目,下载附件 2.拿到手以后是一张图片,话不多说,直接上Ste ...

  9. Windows下使用 Docker 部署 RabbitMQ

    安装 Docker 首先进入 https://docs.docker.com/desktop/windows/install/ 下载最新版 Docker,下载好后,双击进行安装,此处不对安装进行说明. ...

  10. [STL] deque 双端队列