B题 Sort the Array
题目大意:
判断能否通过一次倒置,使序列变为一个递增序列
如果可以,输出倒置那一段的起始点和终点的位置;
题目链接:http://codeforces.com/problemset/problem/451/B
我自己的做法是用一个数组b保存原数组中小于后一个点的点的下标。
如果b数组中后一个数比前一个数大了超过一,说明有2段递减序列,不成立。
如果均后一个比前一个大1,那么在判断倒置前起始的位置和它倒置后的下一个位置是否递增进行判断,在进行倒置前结束的位置和它倒置后的上一个位置是否递增进行判断
均成立,输出两个位置
#include<iostream>
#include<cstdio>
using namespace std;
#define N 100100
int a[N],b[N];
int cnt;
int main()
{
int n,flag; while(scanf("%d",&n)!=EOF){
flag=;cnt=; for(int i=;i<n;i++) b[i]=-;
for(int i=;i<n;i++) cin>>a[i];
for(int i=;i<=n-;i++){
if(a[i]>a[i+]) b[cnt++]=i;
}
if(cnt>)
{
b[cnt]=b[cnt-]+;
cnt++;
} for(int i=;i<cnt-;i++){
//cout<<b[i]<<endl;
if(b[i+]-b[i]!=)
{
flag=;
break;
}
}
if(b[]==-) {cout<<"yes"<<endl<<<<' '<<<<endl;continue;} if(flag==)
{
if(b[cnt-]<n-) {if(a[b[]]>a[b[cnt-]+])flag=;}
if(b[]>){if(a[b[cnt-]]<a[b[]-]) flag=;}
} if(flag==)cout<<"no"<<endl;
else{
cout<<"yes"<<endl<<b[]+<<' '<<b[cnt-]+<<endl;
}
} return ;
}
B题 Sort the Array的更多相关文章
- CF451B Sort the Array 水题
Codeforces Round #258 (Div. 2) Sort the Array B. Sort the Array time limit per test 1 second memory ...
- Codeforces Round #258 (Div. 2) B. Sort the Array(简单题)
题目链接:http://codeforces.com/contest/451/problem/B --------------------------------------------------- ...
- [LeetCode] 912. Sort an Array 数组排序
Given an array of integers nums, sort the array in ascending order. Example 1: Input: [5,2,3,1] Outp ...
- [CareerCup] 11.2 Sort Anagrams Array 异位词数组排序
11.2 Write a method to sort an array of strings so that all the anagrams are next to each other. 这道题 ...
- Codeforces Round #258 (Div. 2) . Sort the Array 贪心
B. Sort the Array 题目连接: http://codeforces.com/contest/451/problem/B Description Being a programmer, ...
- Codeforces Round #258 (Div. 2)——B. Sort the Array
B. Sort the Array time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- LeetCode 912. 排序数组(Sort an Array) 43
912. 排序数组 912. Sort an Array 题目描述 每日一算法2019/6/15Day 43LeetCode912. Sort an Array
- Minimum number of swaps required to sort an array
https://www.hackerrank.com/challenges/minimum-swaps-2/problem Minimum Swaps II You are given an unor ...
- 【leetcode】912. Sort an Array
题目如下: Given an array of integers nums, sort the array in ascending order. Example 1: Input: [5,2,3,1 ...
随机推荐
- 机器学习概念之特征选择(Feature selection)
不多说,直接上干货! .
- AJPFX总结集合的概念
//java 中集合的概述========================================================== 集合的概念: 为 ...
- 协程和I/O模型
1.协程: 单线程实现并发 在应用程序里控制多个任务的切换+保存状态 优点: 应用程序级别速度要远远高于操作系统的切换 缺点: 多个任务一旦有一个阻塞没有切换,整个线程都阻塞在原地 该线程内的其他的任 ...
- IDEA下MyBatis错误总结
1. Pom.xml配置 语法顺序 <properties resource="config.properties"> </properties> < ...
- git push时报错filename too long的解决
命令行输入:git config core.longpaths true 之后再进行 git 的push命令
- IOS开发之关于UIButton点击没有响应问题
1.如果一个UIButton的frame超出父视图的frame,UIButton还是可以显现的,但响应不了点击事件了,当开发中,遇到UIButton点击没有响应问题时,我们需要输出btn及它父视图的f ...
- vue-cli下面的config/index.js注解 webpack.base.conf.js注解
config/indexjs详解上代码: 'use strict' // Template version: 1.3.1 // see http://vuejs-templates.github.io ...
- JavaScript 在线测试
<iframe src="http://www.it1352.com/Onlinetools/OnlineCompileCommon/17?c_height=100&r_hei ...
- Linux OpenGL 实践篇-11-shadow
OpenGL 阴影 在三维场景中,为了使场景看起来更加的真实,通常需要为其添加阴影,OpenGL可以使用很多种技术实现阴影,其中有一种非常经典的实现是使用一种叫阴影贴图的实现,在本节中我们将使用阴影贴 ...
- 如何修改站点url
1.config目录下的config_global.php 文件,修改:$_config['cookie']['cookiedomain'] = '.xxxxx.com';2.config目录下的co ...