(原题链接: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. EL表达式详解(常用表达式以及取值)

    EL表达式 学习总结 一. El表达式概念 二. El中的表达式 1. 算术表达式 2. 比较表达式 3. 逻辑表达式 4. 三元表达式 5. 判空表达式 三.EL 从四个作用域中取值 1. 概念 2 ...

  2. Reflect.has检测对象是否拥有某个属性

    Reflect.has({x: 0}, 'x'); // true Reflect.has({x: 0}, 'y'); // false // returns true for properties ...

  3. Spring Boot-@Value获取值和@ConfigurationProperties获取值的比较

    @Value和@ConfigurationProperties都是用于属性的注入(相当于spring中<bean id=" " class=" "> ...

  4. Servlet实现登录注册

    1.注册页面register.html <!DOCTYPE html> <html lang="en"> <head> <meta cha ...

  5. 数组-LeetCode-笔试

    目录 数组理论基础 二分查找 二分法第一种写法 二分法第二种写法 ACM 移除元素 暴力解法 双指针法(快慢指针) ACM 有序数组的平方 暴力排序 双指针法 长度最小的子数组 暴力解法 滑动窗口 相 ...

  6. 讲解CPU之NUMA硬件体系以及机制(lscpu查看相关信息)

    先看看从系统层面反映出来的numa cpu信息.采样机器为实体机.80核.128内存. [root@ht2 src]# lscpu Architecture: x86_64 #x86架构下的64位 C ...

  7. Windows下安装mysql(非安装包)

    Windows下安装mysql(非安装包) 参考:https://www.cnblogs.com/yunlongaimeng/p/12558638.html 1.下载MYSQL(慢的话可以用迅雷,或其 ...

  8. 《手写Mybatis》第4章:Mapper XML的解析和注册使用

    作者:小傅哥 系列:https://bugstack.cn/md/spring/develop-mybatis/2022-03-20-%E7%AC%AC1%E7%AB%A0%EF%BC%9A%E5%B ...

  9. InnoDB两次写

    partial page write问题: 默认情况下,innodb的一个页面时16k大小,其数据校验也是针对这16k来校验的,将数据写入磁盘是以页面为单位的.文件系统是以4k为单位写入的,机械磁盘是 ...

  10. Glade To Code 介绍

    Glade To Code 简介 根据 Glade 文件生成指定语言的 GTK 代码的工具 使用说明 python3 glade-to-code.py -l [语言类型] -i [输入 Glade 文 ...