Code Forces 1367A Sorting Parts 题解
(原题链接: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 题解的更多相关文章
- 思维题--code forces round# 551 div.2
思维题--code forces round# 551 div.2 题目 D. Serval and Rooted Tree time limit per test 2 seconds memory ...
- Code Forces 796C Bank Hacking(贪心)
Code Forces 796C Bank Hacking 题目大意 给一棵树,有\(n\)个点,\(n-1\)条边,现在让你决策出一个点作为起点,去掉这个点,然后这个点连接的所有点权值+=1,然后再 ...
- Code Forces 833 A The Meaningless Game(思维,数学)
Code Forces 833 A The Meaningless Game 题目大意 有两个人玩游戏,每轮给出一个自然数k,赢得人乘k^2,输得人乘k,给出最后两个人的分数,问两个人能否达到这个分数 ...
- code forces 382 D Taxes(数论--哥德巴赫猜想)
Taxes time limit per test 2 seconds memory limit per test 256 megabytes input standard input output ...
- Code Forces Gym 100886J Sockets(二分)
J - Sockets Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Valera ...
- Code Forces 711D Directed Roads
D. Directed Roads time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- CODE FESTIVAL 2017 qual A 题解
补一发A的题解. A - Snuke's favorite YAKINIKU 题意: 输入字符串S,如果以YAKI开头输出Yes,否则输出No. #include<bits/stdc++.h&g ...
- Code Forces 543A Writing Code
题目描述 Programmers working on a large project have just received a task to write exactly mm lines of c ...
- Code Chef February Challenge 2019题解
传送门 \(HMAPPY2\) 咕 话说这题居然卡\(scanf\)的么??? int T;cin>>T; while(T--){ cin>>n>>a>> ...
随机推荐
- Mybatis实现批量添加操作
Mybatis实现批量添加操作 学习内容: 1. 使用 2. 代码实现 2.1 UserMapper.java 接口 2.2 UserMapper.xml 总结: 学习内容: 1. 使用 这里通过动态 ...
- js new Date()参数格式
最近在写页面使用new Date()获取时间戳在ie浏览器中测试发现无效:后来发现是参数格式问题, new Date()参数格式如下: 1.用整数初始化日期对象 var date1 = new Dat ...
- Wireshark-过滤器-数据包解析
目录 过滤器 数据包解析 参考 推荐阅读: https://www.cnblogs.com/zwtblog/tag/计算机网络/ 过滤器 显示过滤器 和 捕获过滤器,俩者使用非常类似. 在Wiresh ...
- Java学习day37
动态语言:是一类在运行时可以改变其结构的语言:例如新的函数.对象.甚至代码可以被引进,已有的函数可以被删除或是其他结构上的变化. 主要动态语言:Objective-C.C#.JavaScript.PH ...
- TypeScript学习_入门向
TypeScript学习_入门向 1-TypeScript简介 首先官网祭天 ---> https://www.tslang.cn/ TypeScript 是 JavaScript 的一个超集, ...
- 你能知道的或者不知道的shell变量都在这里
第2章 shell变量讲解 2.1 shell中的变量讲解 2.1.1 什么是shell变量 变量的本质就是内存中的一块区域 变量名 位置 变量是脚本中经常会使用的内容信息 变量可以在脚本中直接使用 ...
- Apache Doris 单节点(可多节点)Docker集群制作教程
集群制作Author:苏奕嘉脚本研发Author:种益调研测试Author:杨春东 前言 Apache Doris是当下非常火热和流行的MPP架构OLAP数据库,很多同学想自学/测试Doris的使用和 ...
- rocketmq消息及流程
1.为什么用mq 优势 主要有3个: 应用解耦(降低微服务之间的关联). 异步提速(微服务拿到mq消息后同时工作). 削峰填谷(可以消息堆积) 劣势 系统可用性降低(MQ一旦宕机整个系统不可用) 复杂 ...
- 树莓派开发笔记(十三):入手研华ADVANTECH工控树莓派UNO-220套件(二):安装rtc等驱动
前言 前面运行了系统,本篇是安装对应套装的驱动,使rtc等外设生效,树莓派本身是不带rtc外设的. UNO-220-P4N1AE 驱动下载 官方下载:https://www.advan ...
- SpringBoot程序预装载数据
简介 在项目实际的开发过程中,有时候会遇到需要在应用程序启动完毕对外提供服务之前预先将部分数据装载到缓存的需求.本文就总结了常见的数据预装载方式及其实践. 适用场景 预装载应用级别数据到缓存:如字典数 ...