CodeForces 451B
Sort the Array
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Description
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.
Input
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).
Output
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.
Sample Input
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
//2016.8.2
#include<iostream>
#include<cstdio>
#include<algorithm> using namespace std; int main()
{
long long arr_src[], arr_dest[];
int l, r, n;
while(cin >> n)
{
r = l = ;
for(int i = ; i <= n; i++)
{
scanf("%lld", &arr_src[i]);
arr_dest[i] = arr_src[i];
}
sort(arr_dest+, arr_dest++n);
for(int i = ; i <= n; i++)
if(arr_src[i] != arr_dest[i])
{
l = i;
break;
}
for(int i = n; i >= ; i--)
if(arr_src[i]!=arr_dest[i])
{
r = i;
break;
}
bool fg = true;
reverse(arr_dest+l, arr_dest++r);
for(int i = ; i <= n; i++)
{
if(arr_src[i] != arr_dest[i])
{
fg = false;
break;
}
}
if(fg)printf("yes\n%d %d\n", l, r);
else printf("no\n");
}
return ;
}
CodeForces 451B的更多相关文章
- CF Codeforces Round #258 (Div. 2) B (451B)
题意:找出一段逆序! 预存a[]数组到b[]数组.将b排序,然后前后找不同找到区间[l,r],然后推断[l,r]是否逆序就能够了!.当然还得特判本身就是顺序的!! ! AC代码例如以下: #inclu ...
- Codeforces Round #258 (Div. 2) 小结
A. Game With Sticks (451A) 水题一道,事实上无论你选取哪一个交叉点,结果都是行数列数都减一,那如今就是谁先减到行.列有一个为0,那么谁就赢了.因为Akshat先选,因此假设行 ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
随机推荐
- html中DIv并排显示问题
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD ...
- /system改成可写
读写: mount -o remount,rw /dev/block/mtdblock0 /system 只读mount -o remount,ro /dev/block/mtdblock0 /sys ...
- Ibatis collect select用法详解
问题:之前接触过Ibatis的使用,在做一对多的时候,一般都是手动去填充,非自动让ibatis去填充数据. 下面就用ibatis的自动填充功能来实现. 关键使用到collection 标签下的sele ...
- CodeForces 598E Chocolate Bar
区间DP预处理. dp[i][j][k]表示大小为i*j的巧克力块,切出k块的最小代价. #include<cstdio> #include<cstring> #include ...
- 加载window事件
$(document).ready()和window.onload的区别 发表于 2012-08-29 由 admin 以浏览器装载文档为例,在页面加载完毕后,浏览器会通过JavaScript为DOM ...
- Document Classification
Natural Language Processing with Python Chapter 6.1 由于nltk.FreqDist的排序问题,获取电影文本特征词的代码有些微改动. import n ...
- (中等) POJ 2886 Who Gets the Most Candies? , 反素数+线段树。
Description N children are sitting in a circle to play a game. The children are numbered from 1 to N ...
- EM 期望最大化算法
(EM算法)The EM Algorithm EM是我一直想深入学习的算法之一,第一次听说是在NLP课中的HMM那一节,为了解决HMM的参数估计问题,使用了EM算法.在之后的MT中的词对齐中也用到了. ...
- K-means算法简介
K-means 算法是无监督的 聚类算法,算法简单,有效. K-means算法: 输入参数: 指定聚类数目 k,训练集 X 输出 : k 个聚类 算法描述: K-means 算法 是一个 迭代算法,每 ...
- POJ3268Dijkstra
题意:给定n个点,m条边,求所有顶点中到顶点x的来回最短距离 分析:考虑到数据范围,选用Dijkstra,用Floyd会超时 #include <iostream> #include &l ...