E. Three Swaps
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Xenia the horse breeder has n (n > 1) horses that stand in a row. Each horse has its own unique number. Initially, the i-th left horse has number i. That is, the sequence of numbers of horses in a row looks as follows (from left to right): 1, 2, 3, ..., n.

Xenia trains horses before the performance. During the practice sessions, she consistently gives them commands. Each command is a pair of numbers l, r (1 ≤ l < rn). The command l, r means that the horses that are on the l-th, (l + 1)-th, (l + 2)-th, ..., r-th places from the left must be rearranged. The horses that initially stand on the l-th and r-th places will swap. The horses on the (l + 1)-th and(r - 1)-th places will swap. The horses on the (l + 2)-th and (r - 2)-th places will swap and so on. In other words, the horses that were on the segment [l, r] change their order to the reverse one.

For example, if Xenia commanded l = 2, r = 5, and the sequence of numbers of horses before the command looked as (2, 1, 3, 4, 5, 6), then after the command the sequence will be (2, 5, 4, 3, 1, 6).

We know that during the practice Xenia gave at most three commands of the described form. You have got the final sequence of numbers of horses by the end of the practice. Find what commands Xenia gave during the practice. Note that you do not need to minimize the number of commands in the solution, find any valid sequence of at most three commands.

Input

The first line contains an integer n (2 ≤ n ≤ 1000) — the number of horses in the row. The second line contains n distinct integersa1, a2, ..., an (1 ≤ ain), where ai is the number of the i-th left horse in the row after the practice.

Output

The first line should contain integer k (0 ≤ k ≤ 3) — the number of commads Xenia gave during the practice. In each of the next k lines print two integers. In the i-th line print numbers li, ri (1 ≤ li < rin) — Xenia's i-th command during the practice.

It is guaranteed that a solution exists. If there are several solutions, you are allowed to print any of them.

Sample test(s)
input
5
1 4 3 2 5
output
1
2 4
input
6
2 1 4 3 6 5
output
3
1 2
3 4
5 6
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <queue>
#include <algorithm>
#include <limits.h>
using namespace std;
#define X first
#define Y second
#define PB(x) push_back(x)
#define MP(x,y) make_pair(x,y)
#define CLR(x) memset(x,0,sizeof(x));
#define Rep(i,x,y) for(int i=x;i<y;i++)
#define For(i,x,y) for(int i=x;i<=y;i++)
#define DFor(i,x,y) for(int i=x;i>=y;i--)
const int oo=INT_MAX>>2;
const int sp[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
typedef pair<int, int> F;
vector<F> ans;
int n,v[2000];
int getl(){
For(i,1,n) if (v[i]!=i) return i; return 0;
}
int getr(){
DFor(i,n,1)if (v[i]!=i) return i;
}
int getp(int x){
For(i,1,n) if (v[i]==x) return i;
} int dfs(int k)
{
int l=getl();
if (!l) return 1;
if (k==4) return 0;
int r=getp(l);
reverse(v+l, v+r+1);
if (dfs(k+1)) return ans.PB(MP(l, r)),1;
reverse(v+l, v+r+1); r=getr();
l=getp(r);
reverse(v+l, v+r+1);
if (dfs(k+1)) return ans.PB(MP(l, r)),1;
reverse(v+l, v+r+1);
return 0;
}
int main()
{
// freopen("/Users/MAC/Desktop/Error202/Error202/1.in","r",stdin);
// freopen("/Users/MAC/Desktop/Error202/Error202/1.out","w",stdout);
cin>>n;
For(i,1,n) cin>>v[i];
dfs(1);
if (ans.size()) {
cout<<ans.size()<<endl;
Rep(i,0,ans.size()) cout<<ans[i].X<<" "<<ans[i].Y<<endl;
}
else cout<<"2\n1 2\n1 2";
}

Three Swaps DFS的更多相关文章

  1. codeforces 691D D. Swaps in Permutation(dfs)

    题目链接: D. Swaps in Permutation time limit per test 5 seconds memory limit per test 256 megabytes inpu ...

  2. codeforces 691D Swaps in Permutation DFS

    这个题刚开始我以为是每个交换只能用一次,然后一共m次操作 结果这个题的意思是操作数目不限,每个交换也可以无限次 所以可以交换的两个位置连边,只要两个位置连通,就可以呼唤 然后连通块内排序就好了 #in ...

  3. uva 331 Mapping the Swaps 求交换排序的map 纯DFS

    给出一个序列,每次交换两个数,求有几种交换方法能使序列变成升序. n不大于5,用dfs做. 代码: #include <cstdio> #include <cstring> # ...

  4. [codeforces 339]E. Three Swaps

    [codeforces 339]E. Three Swaps 试题描述 Xenia the horse breeder has n (n > 1) horses that stand in a ...

  5. uva331 - Mapping the Swaps

    Mapping the Swaps Sorting an array can be done by swapping certain pairs of adjacent entries in the ...

  6. Swaps in Permutation

    Swaps in Permutation You are given a permutation of the numbers 1, 2, ..., n and m pairs of position ...

  7. CodeForces - 441D: Valera and Swaps(置换群)

    A permutation p of length n is a sequence of distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n). A permu ...

  8. 801. Minimum Swaps To Make Sequences Increasing

    We have two integer sequences A and B of the same non-zero length. We are allowed to swap elements A ...

  9. BZOJ 3083: 遥远的国度 [树链剖分 DFS序 LCA]

    3083: 遥远的国度 Time Limit: 10 Sec  Memory Limit: 1280 MBSubmit: 3127  Solved: 795[Submit][Status][Discu ...

随机推荐

  1. 小测试 php代理,nginx代理,直接访问对比

    #php proxy total sent request num: 507 total handle read times: 506 506 fetches, 2 max parallel, 2.7 ...

  2. Spark学习之RDD的理解

    转自:http://www.infoq.com/cn/articles/spark-core-rdd/ 感谢张逸老师的无私分享 RDD,全称为Resilient Distributed Dataset ...

  3. <转>四个重要属性——Action、Data、Category、Extras

    Intent作为联系各Activity之间的纽带,其作用并不仅仅只限于简单的数据传递.通过其自带的属性,其实可以方便的完成很多较为复杂的操作.例如直接调用拨号功能.直接自动调用合适的程序打开不同类型的 ...

  4. SQL Server 镜像

    sql server 2005镜像制作 以下是操作步骤:-- =========================================== -- 无论是主体服务器.镜像服务器, 还是见证服务 ...

  5. 利用 Android Studio 和 Gradle 打包多版本APK

    在项目开发过程中,经常会有需要打包不同版本的 APK 的需求. 比如 debug版,release版,dev版等等. 有时候不同的版本中使用到的不同的服务端api域名也不相同. 比如 debug_ap ...

  6. 20个经典bootsrtap后台html网站模板推荐

    今天为大家推荐20款不同风格的Bootstrap后台管理模板,每一款都经典可用,能预览和下载,保证让你挑得眼花缭乱. 1,Simpli flag蓝色 Simpli Flat蓝色管理模板是一款采用Fla ...

  7. php 接口示例

    php 接口示例: public function dev(){ $m=new Model('machine_info'); $ip=$_GET['ip']; echo $ip; //$arr=$m- ...

  8. //Build/ 2014 开发者大会Azure重点整理

     寓教于乐,轻松掌握 Windows Apps和 Cloud //Build/ 2014开发者大会第二天重点整理 (上) //Build/ 2014开发者大会第二天的主题演讲主要包含两部分:Mic ...

  9. CSS3学习----选择器、字体

     属性选择器: [att*=val]{}若att元素属性值包括val指定字符,则使用该样式 [att^=val]{}若att元素属性值开头字符为val,则使用该样式 [att&=val]{ ...

  10. php入门微理解

    1.php是什么?(来自百度百科) php:Hypertext preprocessor(超文本预处理器).一种开源脚本语言.主要用于web开发. 2.与其它语言的关系 介于HTML和C/C++,Ja ...