Codeforces Round #258 (Div. 2)——B. Sort the Array
1 second
256 megabytes
standard input
standard output
Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of n distinct integers.
Unfortunately, the size of a is too small. You want a bigger array! Your friends agree to give you a bigger array, but only if you are able to answer the
following question correctly: is it possible to sort the array a (in increasing order) by reversing exactly
one segment of a? See definitions of segment and reversing in the notes.
The first line of the input contains an integer n (1 ≤ n ≤ 105)
— the size of array a.
The second line contains n distinct space-separated integers: a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ 109).
Print "yes" or "no" (without quotes), depending on the answer.
If your answer is "yes", then also print two space-separated integers denoting start and end (start must not be greater than end) indices of the segment to be
reversed. If there are multiple ways of selecting these indices, print any of them.
- 3
- 3 2 1
- yes
- 1 3
- 4
- 2 1 3 4
- yes
- 1 2
- 4
- 3 1 2 4
- no
- 2
- 1 2
- yes
- 1 1
Sample 1. You can reverse the entire array to get [1, 2, 3], which is sorted.
Sample 3. No segment can be reversed such that the array will be sorted.
Definitions
A segment [l, r] of array a is the sequence a[l], a[l + 1], ..., a[r].
If you have an array a of size n and you reverse
its segment [l, r], the array will become:
a[1], a[2], ..., a[l - 2], a[l - 1], a[r], a[r - 1], ..., a[l + 1], a[l], a[r + 1], a[r + 2], ..., a[n - 1], a[n].
—————————————————————切割线———————————————————————————
题意分析:
给定一个序列,是否能通过逆序它的一个子序列使得整个序列单调递增,假设能。输出须要逆序的下标,否则输出no
思路:
1、从左到右遍历,第一次出现比前一个数小的下标;
2、然后从右往左遍历,第一次出现比后一个数小的下标。
3、将这段区间逆序,然后推断整个序列是否单调递增
- #include<bits/stdc++.h>
- #define maxn 100005
- typedef long long LL;
- using namespace std;
- int a[maxn];
- int main()
- {
- int n,x=1,y=1;
- cin>>n;
- for(int i=1;i<=n;i++) scanf("%d",&a[i]);
- for(int i=1;i<n;i++){
- if(a[i]>a[i+1]){
- x=i;break;
- }
- }
- for(int i=n;i>=1;i--){
- if(a[i]<a[i-1]){
- y=i;
- break;
- }
- }
- reverse(a+x,a+y+1);
- int flag=0;
- for(int i=1;i<n;i++){
- if(a[i]>a[i+1]){
- flag=1;
- break;
- }
- }
- if(flag) cout<<"no"<<endl;
- else{
- cout<<"yes"<<endl<<x<<" "<<y<<endl;
- }
- return 0;
- }
Codeforces Round #258 (Div. 2)——B. Sort the Array的更多相关文章
- Codeforces Round #258 (Div. 2) B. Sort the Array(简单题)
题目链接:http://codeforces.com/contest/451/problem/B --------------------------------------------------- ...
- Codeforces Round #258 (Div. 2) B. Sort the Array
题目链接:http://codeforces.com/contest/451/problem/B 思路:首先找下降段的个数,假设下降段是大于等于2的,那么就直接输出no,假设下降段的个数为1,那么就把 ...
- Codeforces Round #258 (Div. 2/B)/Codeforces451B_Sort the Array
解题报告 http://blog.csdn.net/juncoder/article/details/38102391 对于给定的数组,取对数组中的一段进行翻转,问翻转后是否是递增有序的. 思路: 仅 ...
- Codeforces Round #258 (Div. 2)[ABCD]
Codeforces Round #258 (Div. 2)[ABCD] ACM 题目地址:Codeforces Round #258 (Div. 2) A - Game With Sticks 题意 ...
- Codeforces Round #258 (Div. 2) 小结
A. Game With Sticks (451A) 水题一道,事实上无论你选取哪一个交叉点,结果都是行数列数都减一,那如今就是谁先减到行.列有一个为0,那么谁就赢了.因为Akshat先选,因此假设行 ...
- 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)
A - Game With Sticks 题目的意思: n个水平条,m个竖直条,组成网格,每次删除交点所在的行和列,两个人轮流删除,直到最后没有交点为止,最后不能再删除的人将输掉 解题思路: 每次删除 ...
- Codeforces Round #258 (Div. 2)(A,B,C,D)
题目链接 A. Game With Sticks time limit per test:1 secondmemory limit per test:256 megabytesinput:standa ...
- Codeforces Round #258 (Div. 2)-(A,B,C,D,E)
http://blog.csdn.net/rowanhaoa/article/details/38116713 A:Game With Sticks 水题.. . 每次操作,都会拿走一个横行,一个竖行 ...
随机推荐
- AtCoder Grand Contest 018 A - Getting Difference
A - Getting Difference Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem Statement ...
- linux新安装后root密码设置
linux在安装过程中未设置root密码 导致在使用中无法su 解决方法是设置root密码: 输入: sudo passwd root [sudo] password for you: ---> ...
- 【Henu ACM Round #12 C】 Alice, Bob, Two Teams
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 考虑任意两个字符串(a,b) 假设a在b的前面 那么如果a+b>=b+a 这里的+表示字符串的链接 那么显然需要交换a,b的位 ...
- 基于 Cookie 的 SSO 中间件 kisso
kisso = cookie sso 基于 Cookie 的 SSO 中间件,它是一把快速开发 java Web 登录系统(SSO)的瑞士军刀.欢迎大家使用 kisso !! kisso 帮助文档 ...
- POJ 1426 Find The Multiple (DFS / BFS)
题目链接:id=1426">Find The Multiple 解析:直接从前往后搜.设当前数为k用long long保存,则下一个数不是k*10就是k*10+1 AC代码: /* D ...
- jquary依据td中button的元素属性删除tr行(删选出想删除的行)
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcnVveXVhbnlp/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...
- 【基础练习】【线性DP】codevs2622 数字序列(最大连续子序列和)题解
版权信息 转载请注明出处 [ametake版权全部]http://blog.csdn.net/ametake欢迎来看 这道题目本质就是朴素的最大连续子序列和 直接上题目和代码 题目描写叙述 Descr ...
- Qt虽然自己的源代码里不使用Exception,但也提供了一个QException及其子类QUnhandledException
http://doc.qt.io/qt-5/exceptionsafety.htmlhttp://doc.qt.io/qt-5/qexception.htmlhttp://doc.qt.io/qt-5 ...
- tokumx的安装和使用
Add the Tokutek package signing key. $ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-key ...
- native.js是什么且如何使用
native.js是什么且如何使用 一.总结 一句话总结:Native.js技术,简称NJS,是一种将手机操作系统的原生对象转义,映射为JS对象,在JS里编写原生代码的技术.Native.js不是一个 ...