Three Swaps DFS
来源:互联网 发布:java math.random 编辑:程序博客网 时间:2024/05/21 04:43
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.
51 4 3 2 5
12 4
62 1 4 3 6 5
31 23 45 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 339E Three Swaps (搜索 dfs)
- codeforces 339E Three Swaps 搜索
- UVA 331 Mapping the Swaps (回溯+dfs)
- UVa 331 Mapping the Swaps (DFS)
- 331 - Mapping the Swaps(回溯+ dfs)
- UVA Mapping the Swaps (DFS)
- uva 331 Mapping the Swaps(DFS+回溯)
- hunnu 11163 three lines #DFS
- ZOJ3516 Tree of Three,DFS
- uvalive 6902 Three Squares - dfs
- ZOJ 3516 Tree of Three(DFS)
- hdu 3419 The Three Groups(dfs)
- ZOJ-#3516Tree of Three(dfs+vector)
- uva 331 Mapping the Swaps 求交换排序的map 纯DFS
- Prime Swaps
- HDU 5878 I Count Two Three (dfs+二分)
- HDU 5878 I Count Two Three (DFS预处理 + 二分)
- 什么是Android?
- 黑马程序员——Java基础---String类和基本数据类型包装类
- 【xtku】巧删显卡右键菜单 还系统干净界面
- js 根据模板增加表格(table)
- ubuntu下搭建ftp服务器vsftpd,cuteFTP登录该服务器
- Three Swaps DFS
- Objective-C中防止重复代码的一种方法
- Java中继承thread类与实现Runnable接口的区别
- codeforces 340 A. The Wall
- linux下如何添加一个用户并且让用户获得root权限
- jQuery之使用jQuery.fn.prop()替换jQuery.fn.attr()
- Book of Evil 树双向DFS
- 设置picturebox 背景为透明
- u- boot 的认识