Three Swaps DFS
1 second
256 megabytes
standard input
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 < r ≤ n). 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.
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 ≤ ai ≤ n), where ai is the number of the i-th left horse in the row after the practice.
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 < ri ≤ n) — 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.
5
1 4 3 2 5
1
2 4
6
2 1 4 3 6 5
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的更多相关文章
- codeforces 691D D. Swaps in Permutation(dfs)
题目链接: D. Swaps in Permutation time limit per test 5 seconds memory limit per test 256 megabytes inpu ...
- codeforces 691D Swaps in Permutation DFS
这个题刚开始我以为是每个交换只能用一次,然后一共m次操作 结果这个题的意思是操作数目不限,每个交换也可以无限次 所以可以交换的两个位置连边,只要两个位置连通,就可以呼唤 然后连通块内排序就好了 #in ...
- uva 331 Mapping the Swaps 求交换排序的map 纯DFS
给出一个序列,每次交换两个数,求有几种交换方法能使序列变成升序. n不大于5,用dfs做. 代码: #include <cstdio> #include <cstring> # ...
- [codeforces 339]E. Three Swaps
[codeforces 339]E. Three Swaps 试题描述 Xenia the horse breeder has n (n > 1) horses that stand in a ...
- uva331 - Mapping the Swaps
Mapping the Swaps Sorting an array can be done by swapping certain pairs of adjacent entries in the ...
- Swaps in Permutation
Swaps in Permutation You are given a permutation of the numbers 1, 2, ..., n and m pairs of position ...
- 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 ...
- 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 ...
- BZOJ 3083: 遥远的国度 [树链剖分 DFS序 LCA]
3083: 遥远的国度 Time Limit: 10 Sec Memory Limit: 1280 MBSubmit: 3127 Solved: 795[Submit][Status][Discu ...
随机推荐
- svn版本控制
svn自动生成X.java.mine,X.java.r2333,X.java.r2368文件 这是你和别人的操作形成了冲突: 你和别人都从2333这个版本对X.java进行了修改, 别人先修改之后,先 ...
- gdb 命令使用
1.gdb -x command.txt 每次重复输入命令很麻烦,可以使用上面的命令,把命令输入进command.txt里面,然后直接就可以执行gdb. 2.list 2.1 list functio ...
- Problem F: Exponentiation大数求幂
DescriptionProblems involving the computation of exact values of very large magnitude and precision ...
- leetcode Container With Most Water python
class Solution(object): def maxArea(self, height): """ :type height: List[int] :rtype ...
- 简单的scrapy实战:爬取腾讯招聘北京地区的相关招聘信息
简单的scrapy实战:爬取腾讯招聘北京地区的相关招聘信息 简单的scrapy实战:爬取腾讯招聘北京地区的相关招聘信息 系统环境:Fedora22(昨天已安装scrapy环境) 爬取的开始URL:ht ...
- codeforces 600E. Lomsat gelral 启发式合并
题目链接 给一颗树, 每个节点有初始的颜色值. 1为根节点.定义一个节点的值为, 它的子树中出现最多的颜色的值, 如果有多种颜色出现的次数相同, 那么值为所有颜色的值的和. 每一个叶子节点是一个map ...
- android基于XMPP的消息推送机制
关于服务器端向Android客户端的推送,主要有三种方式:1.客户端定时去服务端取或者保持一个长Socket,从本质讲这个不叫推送,这是去服务端拽数据.但是实现简单,主要缺点:耗电等2.Google的 ...
- QTabWiget Change Color 改变颜色(每个QWidget都有一个自己的调色板palette,设置它的颜色,然后setAutoFillBackground即可)
Qt中的QTabWiget 类提供了一个便签控件,但是这个控件默认初始化的颜色是白色,和原窗口的颜色不同,看起来非常的违和,所以我们希望将其的背景颜色设为当前窗口的背景颜色.我们所要做的就是先将应用程 ...
- 美国vps哪个比较好,vps国内访问速度最快!
沃网中国是一家成立于2008年的国内idc商,提供基于hyper-v架构的VPS产品,数据中心包括国内电信.美国洛杉矶等,他们采用的是国内访问最快的加州机房ping值,160-180左右相当给力的速度 ...
- POJ 3061 Subsequence(Two Pointers)
[题目链接] http://poj.org/problem?id=3061 [题目大意] 给出S和一个长度为n的数列,问最短大于等于S的子区间的长度. [题解] 利用双指针获取每一个恰好大于等于S的子 ...