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 ...
随机推荐
- MySQL GTID复制
什么是GTID 什么是GTID呢, 简而言之,就是全局事务ID(global transaction identifier ),最初由google实现,官方MySQL在5.6才加入该功能.GTID是事 ...
- 在input标签里只能输入数字
<input type='text' onkeyup="(this.v=function(){this.value=this.value.replace(/[^0-9-]+/,''); ...
- Spring Boot学到的内容
Hello World:了解程序入口(创建启动类) Web程序:写Controller类(@RestController),写Controller方法(@GetMapping),maven依赖spri ...
- 2018.7.21NOIP模拟赛?解题报告
题面 预计得分:70 + 60 + 30 = 160 实际得分:40 + 60 + 0 = 100 T1数组开小了 T2比赛结束后5min AC T3加了个记忆话搜索wa了.. T1 zbq吊打std ...
- jQuery选择器之表单对象属性筛选选择器
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content ...
- 在vscode中显示空格和tab符号
转自:https://blog.csdn.net/bmzk123/article/details/86501706 使用python时最烦人的就是代码对齐,而且tab和空格还不一样,为了便于对其,希望 ...
- CSData
NSString 转换成NSData 对象 NSData* xmlData = [@"testdata" dataUsingEncoding:NSUTF8StringEncodin ...
- 开启server-status失败
近日在配置监控宝的apache监控老是出错,经过研究发现如下: 下面先做一些简要的介绍,以防以后查看之用. 一.server-status是什么?二.如何打开server-status?三.serve ...
- 关于dzzoffice 破解版
最近看到很多人在搜索dzzoffice破解版,其实dzzoffie是一款全开源的产品,开放的功能是与演示站中一摸一样的,所以并不会有人破解这种全开源的系统.那么为什么会有人搜索这样的关键词呢? 可能大 ...
- 响应式布局(CSS3弹性盒flex布局模型)
传统的布局方式都是基于盒模型的 利用display.position.float来布局有一定局限性 比如说实现自适应垂直居中 随着响应式布局的流行,CSS3引入了更加灵活的弹性布局模型 flex弹性布 ...