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 ...
随机推荐
- PHP在不同页面间传递Json数据示例代码
gettest.php文件: <?php $value["name"]= urlencode("我的姓名"); $value["pass&quo ...
- 非常强大的前端插件:emmet
安装 Emmet 也有快速生成文件头的功能啊,而且更强大啊输入下边加粗的缩写,然后Tab,就OK了啊http://docs.emmet.io/cheat-sheet/ html:4t <!DOC ...
- maven编译报错 -source 1.5 中不支持 lambda(或diamond) 表达式,编码 UTF-8 的不可映射字符
在用maven编译项目是由于项目中用了jdk 1.8, 编译是报错 -source 1.5 中不支持 lambda 表达式. 错误原因: Maven Compiler 插件默认会加 -source ...
- 如何使用 Java 生成二维码
步骤 下载jar包(QRCode.jar) maven项目手动引入jar包 编写实体类实现二维码的生成 controller调用 下载jar包(QRCode.jar) 下载网址如下: QRCode生成 ...
- html语法第 -2
1 <html> 2 <head> 3 <title>这是第一节课网页标题</title> 4 <meta charset="UTF-8 ...
- IOStableviewsectionSet
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { if (t ...
- PMP项目管理学习笔记(11)——范围管理之定义范围
定义范围过程组 定义范围包含将项目分解为团队成员要完成的具体工作之前你需要知道的所有一切. 输入:需求文档.项目章程.组织过程资产 工具:辅助工作室.产品分析.代理方案识别.专家判断 辅助工作室: 与 ...
- 应用-如何使不同的企业使用独自的数据源。使用ejb3.0+jboss6.2EAP+JPA
摘要: 如何使不同的企业使用独自的数据源.使用ejb3.0+jboss6.2EAP+JPA10C应用系统被多个企业同时使用,为了提供个性化服务,如何使不同的企业使用独自的 ...
- vue项目中快捷语法糖
1.Vue.js是渐进式框架,采用自底向上增量开发的设计基于MVVM思想. 2.Vue 完全有能力驱动采用单文件组件和Vue生态系统支持的库开发的复杂单页应用. 3.Vue.js 的目标是通过尽可能简 ...
- (转)为Spring集成的Hibernate配置二级缓存
http://blog.csdn.net/yerenyuan_pku/article/details/52896195 前面我们已经集成了Spring4.2.5+Hibernate4.3.11+Str ...