[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.

输入示例

5
1 4 3 2 5

输出示例

1
2 4

数据规模及约定

见“输入

题解

暴搜 + 剪枝。。

#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;
}

 

转载于:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/5833937.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值