[codeforces 339]E. Three Swaps
[codeforces 339]E. Three Swaps
试题描述
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 integers a1, 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.
输入示例
输出示例
数据规模及约定
见“输入”
题解
暴搜 + 剪枝。。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <set>
using namespace std; const int BufferSize = 1 << 16;
char buffer[BufferSize], *Head, *Tail;
inline char Getchar() {
if(Head == Tail) {
int l = fread(buffer, 1, BufferSize, stdin);
Tail = (Head = buffer) + l;
}
return *Head++;
}
int read() {
int x = 0, f = 1; char c = getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
return x * f;
} #define maxn 1010
int n, A[maxn];
struct Cmd {
int l, r;
Cmd() {}
Cmd(int _, int __): l(_), r(__) {}
} cs[4]; bool dfs(int k) {
/*for(int i = 1; i <= k; i++)
printf("%d %d\n", cs[i].l, cs[i].r);
putchar('\n');*/
bool ok = 1;
for(int i = 1; i <= n; i++)
if(A[i] != i){ ok = 0; break; }
if(ok) {
printf("%d\n", k);
for(int i = k; i; i--) printf("%d %d\n", cs[i].l, cs[i].r);
return 1;
}
if(k == 3) return 0;
for(int l = 1; l <= n; l++) if(A[l] != l && (abs(A[l] - A[l-1]) > 1 || abs(A[l] - A[l+1]) > 1)) {
for(int r = l + 1; r <= n; r++) if(A[r] != r && (abs(A[r] - A[r-1]) > 1 || abs(A[r] - A[r+1]) > 1)) {
int L = l, R = r;
for(int i = L; i <= (L + R >> 1); i++) swap(A[i], A[R-i+L]);
cs[k+1] = Cmd(L, R);
if(dfs(k + 1)) {
L = cs[k+1].l; R = cs[k+1].r;
for(int i = L; i <= (L + R >> 1); i++) swap(A[i], A[R-i+L]);
return 1;
}
L = cs[k+1].l; R = cs[k+1].r;
for(int i = L; i <= (L + R >> 1); i++) swap(A[i], A[R-i+L]);
}
}
return 0;
} int main() {
n = read();
for(int i = 1; i <= n; i++) A[i] = read();
A[0] = A[n+1] = -1; dfs(0); return 0;
}
[codeforces 339]E. Three Swaps的更多相关文章
- [codeforces 339]D. Xenia and Bit Operations
[codeforces 339]D. Xenia and Bit Operations 试题描述 Xenia the beginner programmer has a sequence a, con ...
- [codeforces 339]C. Xenia and Weights
[codeforces 339]C. Xenia and Weights 试题描述 Xenia has a set of weights and pan scales. Each weight has ...
- 【Codeforces 339】Xenia and Bit Operations
Codeforces 339 D 题意:给定\(2^n\)个数字,现在把它们进行如下操作: 相邻的两个数取\(or\) 相邻的两个数取\(xor\) 以此类推,直到剩下一个数. 问每次修改一个数字, ...
- Educational Codeforces Round 14 D. Swaps in Permutation 并查集
D. Swaps in Permutation 题目连接: http://www.codeforces.com/contest/691/problem/D Description You are gi ...
- Codeforces 425A Sereja and Swaps(暴力枚举)
题目链接:A. Sereja and Swaps 题意:给定一个序列,能够交换k次,问交换完后的子序列最大值的最大值是多少 思路:暴力枚举每一个区间,然后每一个区间[l,r]之内的值先存在优先队列内, ...
- Educational Codeforces Round 14 D. Swaps in Permutation (并查集orDFS)
题目链接:http://codeforces.com/problemset/problem/691/D 给你n个数,各不相同,范围是1到n.然后是m行数a和b,表示下标为a的数和下标为b的数可以交换无 ...
- codeforces C. Sereja and Swaps
http://codeforces.com/contest/426/problem/C 题意:找出连续序列的和的最大值,可以允许交换k次任意位置的两个数. 思路:枚举区间,依次把区间内的比较小的数换成 ...
- Educational Codeforces Round 14 D. Swaps in Permutation(并查集)
题目链接:http://codeforces.com/contest/691/problem/D 题意: 题目给出一段序列,和m条关系,你可以无限次互相交换这m条关系 ,问这条序列字典序最大可以为多少 ...
- [Codeforces 425A] Sereja and Swaps
[题目链接] https://codeforces.com/contest/425/problem/A [算法] 枚举最终序列的左端点和右端点 , 尝试用这段区间中小的数与区间外大的数交换 时间复杂度 ...
随机推荐
- Android中如何使用Intent在Activity之间传递对象[使用Serializable或者Parcelable]
http://blog.csdn.net/cjjky/article/details/6441104 在Android中的不同Activity之间传递对象,我们可以考虑采用Bundle.putSeri ...
- sql 中的运算符级别 如and or not
写了这么多简单的sql,很多东西忘记得差不多了,差点连最基本sql运算符优先级都忘了.平时最常用到and or的优先级都忘了 and的优先级高于or的优先级 举个例子 select * from us ...
- Android onLowMemory()和onTrimMemory()
1. OnLowMemory OnLowMemory是Android提供的API,在系统内存不足,所有后台程序(优先级为background的进程,不是指后台运行的进程)都被杀死时,系统会调用OnLo ...
- ListBox
<asp:ListBox runat="server" ID="txtName" Width ="200" Height=" ...
- CentOS 7 AMD64安装nginx和mysql
NGINX: rpm -ivh http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.8.0-1.el7.ngx.x86_64.rpm 查看: ...
- 将当前网址生成快捷方式在桌面(仅支持IE)
//安装到桌面function toDesktop(sUrl,sName){ try { var WshShell = new ActiveXObject("WScript.Shell&qu ...
- Xunsearch 中文全文搜索
原文地址:http://www.yiichina.com/code/661 官网地址:http://www.xunsearch.com/ 1.安装 wget http://www.xunsearch. ...
- SpringMVC -rest风格修改删除
REST风格
- 总结一下安装linux系统经验-版本选择-安装ubuntu
linux版本选择: 初次接触,建议选 Ubuntu 或者 Fedora,这两个发行版都很容易上手,而且两者都有很强大的中文社区,遇到问题比较容易解决,而且都有国内的源,安装或者更新软件时体验相对会好 ...
- MySQL中MAX函数与Group By一起使用的注意事项(转)
mysql> select * from test; +----+-------+------+-------+ | id | name | age | class | +----+------ ...