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 ...
随机推荐
- C#程序A调用程序B的问题
C#程序A调用程序B,如果程序B中存在 string path1 = System.Environment.CurrentDirectory; 程序A中开启B进程的代码为: System.Diagno ...
- 一份最贴近真实面试的Java基础面试题
这是一份Java基础知识的面试题.在网上的关于Java的面试题数不胜数,但认真看过感觉大多数都没有实用性,有很多是面试官根本就不会问到的,那些已经脱离了实际开发的技术问题.而这份资料来源自一份个人觉得 ...
- laravel模型关联
hasOne 一对一 用户名-手机号hasMany 一对多 文章-评论belongTo 一对多反向 评论-文章belongsToMany 多对多 用户-角色hasManyThrough 远程 ...
- mysql use index() 优化查询
mysql use index() 优化查询 FORCE INDEX/IGNORE INDEX 的语法: SELECT *** FROM TABLE [{USE|IGNORE|FORCE} INDEX ...
- 分享一个开源的JavaScript统计图表库,40行代码实现专业统计图表
提升程序员工作效率的工具/技巧推荐系列 推荐一个功能强大的文件搜索工具SearchMyFiles 介绍一个好用的免费流程图和UML绘制软件-Diagram Designer 介绍Windows任务管理 ...
- day24-2 单例模式
目录 单例模式 类内部定义静态方法实现单例模式 装饰器实现单例模式 元类实现单例模式 单例模式 单例模式:基于某种方法实例化多次得到实例是同一个 当实例化多次得到的对象中存放的属性都一样的情况,应该将 ...
- dnsquery - 使用解析程序查询域名服务器
SYNOPSIS(总览) dnsquery [-n nameserver ] [-t type ] [-c class ] [-r retry ] [-p period ] [-d ] [-s ] [ ...
- activitmq+keepalived+nfs 非zk的高可用集群构建
nfs 192.168.10.32 maast 192.168.10.4 savel 192.168.10.31 应对这个需求既要高可用又要消息延迟,只能使用变态方式实现 nfs部署 #yum ins ...
- 平时有没有使用xml和json
平时有没有使用xml和json,在ajax交互中,哪一种更易于开发和维护,js中怎么序列化JSON字符串? 有,json相比xml可读性和可扩张性好.编码及解码难度较低.在数据交互中带宽占用少,并且在 ...
- 深入了解JVM(Java虚拟机)
虚拟机 JRE由Java API和JVM组成,JVM通过类加载器(Class Loader)加类Java应用,并通过Java API进行执行. 虚拟机(VM: Virtual Machine)是通过软 ...