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>> ...
随机推荐
- window10解决需要管理员删除文件的权限问题
1.快捷键:"win+R",输入:gpedit.msc 回车 2.依次点击:计算机配置-windows配置-安全设置-本地策略-安全选项 3.将两个管理员批准模式和管理员状态三者 ...
- 小程序wx.previewImage查看图片再次点击返回时重新加载页面问题
wx.previewImage预览图片这个过程到底发生了什么? 首先我们点击图片预览,附上查看图片代码: <image class="headImg" data-src=&q ...
- 将base64转成File文件对象
function dataURLtoFile(dataurl, filename) { //将base64转换为文件 var arr = dataurl.split(','), ...
- 《头号玩家》AI电影调研报告(三)
[AR市场正在迅猛增长] 据<工业增强现实现状2017>报告中所述,AR不再只是值得期待的新兴技术.2018年,投资此类技术已成为很多组织机构的关键战略,尤其是对于涉及复杂的制造和运营流程 ...
- caioj 1002: [视频]实数运算2[水题]
题意:输入三个数,计算并输出它们的平均值以及三个数的乘积,结果保留2位小数. 题解:简单题不写题解了-- 代码: #include <cstdio> double a, b, c; int ...
- C# 将PDF转为Excel
通常,PDF格式的文档能支持的编辑功能不如office文档多,针对PDF文档里面有表格数据的,如果想要编辑表格里面的数据,可以将该PDF文档转为Excel格式,然后编辑.本文,将以C#代码为例,介绍如 ...
- Blazor技术开发了一个访客管理系统
简单介绍一下系统功能 该系统为了在疫情期间能很好管理访客登记做好风险管控,同时可以整合智能设备做到自动确认并跟踪访客的行动轨迹,该项目完全开源. 系统流程 访客可以同通过手机进行预注册,同时上传照片, ...
- python---冒泡排序的实现
冒泡排序 思想 列表中有n个数, 每两个相邻的数, 如果前边的数比后边的数大, 就交换. 关键点: 趟: 总共执行 n-1趟 无序区: 第 i 趟时, 索引 0~ n-1-i 为无序区 ...
- 【图像处理】Golang 获取JPG图像的宽高
一.背景 有些业务需要判断图片的宽高,来做一些图片相关缩放,旋转等基础操作. 但是图片缩放,旋转,拼接等操作需要将图片从 JPG 格式转成 RGBA 格式操作,操作完毕后,再转回 JPG 图片. 那如 ...
- 7.Docker容器使用辅助工具汇总
原文地址: 点击直达 more information: https://docs.docker.com/engine/security/security/#docker-daemon-attack- ...