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>> ...
随机推荐
- Java简单登录图形界面
本文参考与:https://blog.csdn.net/wyf2017/article/details/78831744 https://blog.csdn.net/MengKun822/articl ...
- JavaScript 中 append()、prepend()、after()、before() 的区别
内容 append().prepend().after().before() 的区别 jQuery 操作 DOM 之添加节点 方法名 作用 $(selector).append() 指定元素内部添加, ...
- numpy教程01---ndarray的创建
欢迎关注公众号[Python开发实战], 获取更多内容! 工具-numpy numpy是使用Python进行数据科学的基础库.numpy以一个强大的N维数组对象为中心,它还包含有用的线性代数,傅里叶变 ...
- STL空间分配器源码分析(二)mt_allocator
一.简介 mt allocator 是一种以2的幂次方字节大小为分配单位的空间配置器,支持多线程和单线程.该配置器灵活可调,性能高. 分配器有三个通用组件:一个描述内存池特性的数据,一个包含该池的策略 ...
- 探索Django验证码功能的实现 - DjangoStarter项目模板里的封装
前言 依然是最近在做的这个项目,用Django做后端,App上提交信息的时候需要一个验证码来防止用户乱提交,正好我的「DjangoStarter」项目脚手架也有封装了验证码功能,不过我发现好像里面只是 ...
- 省掉80%配置时间,这款Mock神器免费又好用
前端的痛苦 作为前端,最痛苦的是什么时候? 每个迭代,需求文档跟设计稿都出来了,静态页面唰唰两天就做完了.可是做前端又不是简单地把后端吐出来的数据放到页面上就完了,还有各种前端处理逻辑啊. 后端接口还 ...
- Java-GUI编程之处理位图
如果仅仅绘制一些简单的几何图形,程序的图形效果依然比较单调 . AWT 也允许在组件上绘制位图, Graphics 提供了 drawlmage() 方法用于绘制位图,该方法需要一个Image参数一一代 ...
- 【面试普通人VS高手系列】Dubbo的服务请求失败怎么处理?
今天分享的面试题,几乎是90%以上的互联网公司都会问到的问题. "Dubbo的服务请求失败怎么处理"? 对于这个问题,我们来看一下普通人和高手的回答. 普通人: 嗯- 我记得, D ...
- SpringBoot 三层架构 Controller、Service、Dao作用和关系详解
首先创建一个springboot项目. model层 model层也叫pojo层或者entity层,个人比较喜欢pojo层. 一般数据库的一张表对应一个pojo层,并且表中所有字段都在pojo层都一一 ...
- k8s入门之Ingress(七)
Ingress 的功能其实很容易理解:所谓 Ingress,就是 Service 的"Service",代理不同后端 Service 而设置的负载均衡服务. 一.安装ingress ...