codeforces 339E Three Swaps 搜索

题目链接

题意:

给定一个1-n的排列

可以选择一个区间将其翻转。至多翻转三次。

问能不能变成单调递增的序列,并输出方案。

题目保证3次翻转一定有解。

思路:

爆搜,每次翻转一段最长的连续区间。

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <vector>
template <class T>
inline bool rd(T &ret) {
	char c; int sgn;
	if (c = getchar(), c == EOF) return 0;
	while (c != '-' && (c<'0' || c>'9')) c = getchar();
	sgn = (c == '-') ? -1 : 1;
	ret = (c == '-') ? 0 : (c - '0');
	while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
	ret *= sgn;
	return 1;
}
template <class T>
inline void pt(T x) {
	if (x < 0) {
		putchar('-');
		x = -x;
	}
	if (x > 9) pt(x / 10);
	putchar(x % 10 + '0');
}
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
const int N = 1005;
int n, a[N], pos[N];

bool check() {
	for (int i = 1;i <= n;i++) if (a[i] != i)return 0;
	return 1;
}

int L[6], R[6], top = 0;

bool dfs(int k) {
	if (check()){
		printf("%d\n", top);
		for (int i = top;i;i--)
			printf("%d %d\n", L[i], R[i]);
		return true;
	}
	if (k == 3)return false;
	for (int i = 1;i <= n;i++)
		if (a[i] != i && (abs(a[i] - a[i - 1]) != 1 || abs(a[i] - a[i + 1]) != 1))
		for (int j = i + 1;j <= n;j++)
		if (a[j] != j && (abs(a[j] - a[j - 1]) != 1 || abs(a[j] - a[j + 1]) != 1))
		{
			top++;int l = i, r = j;
			L[top] = l;R[top] = r;
			reverse(a + i, a + 1 + j);
			if (dfs(k + 1))return true;
			reverse(a + L[top], a + R[top] + 1);
			top--;
		}
	return false;
}
int main() {
	rd(n);
	for (int i = 1; i <= n; i++)rd(a[i]);
	a[0] = a[n + 1] = -1;
	dfs(0);
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-11 19:31:08

codeforces 339E Three Swaps 搜索的相关文章

CodeForces 283B 记忆化搜索

//搜啊搜... //因为我们每次搜索的起点都是一样的,但是数组的第一个元素不同,所以可以手工算出下一个点,从起点的下一点开始搜 1 #include "iostream" 2 #include "cstdio" 3 #include "cstring" 4 #include "algorithm" 5 using namespace std; 6 __int64 dp[200010][2]; 7 bool vis[20001

CodeForces 374C 记忆化搜索

/*题目*/ 题意:Inna喜欢Dima,所以他希望在一张n * m的 每个单元格印有'D'或者'I'或者'M'或者'A' 的桌子上  尽量多的走出 某个路径 中包含DIMA 这个单词数量最多,必须从'D'开始走,并且'D'只能到'I',然后'I'只能到'M',然后'M'只能到'A',然后'A'只能到'D',这样走, 若走不出DIMA这个单词 输出 poor dima 若存在环的话 输出 poor Inna 否则输出 走的路径中 包含 的最多的DIMA单词数量 他奶奶的 一开始,对于那个判断环的

CodeForces - 520B 按钮 [广度优先搜索/贪心]

Vasya has found a strange device. On the front panel of a device there are: a red button, a blue button and a display showing some positive integer. After clicking the red button, device multiplies the displayed number by two. After clicking the blue

Codeforces 339E

1 #include <cstdio> 2 #include <algorithm> 3 #include <cmath> 4 #include <cstring> 5 #include <cstdlib> 6 #include <ctime> 7 #include <set> 8 #include <map> 9 using namespace std; 10 11 const int maxn=1100;

CodeForces 432C Prime Swaps

Description You have an array a[1],?a[2],?...,?a[n], containing distinct integers from 1 to n. Your task is to sort this array in increasing order with the following operation (you may need to apply it multiple times): choose two indexes, i and j (1?

codeforces C. Prime Swaps

题意:给你n个数,然后在交换次数小于等于5×n的情况下使得这个序列变成升序,输出次数; 思路:哥德巴赫猜想:任何一个大于5的数都可以写成三个质数之和.尽可能的找大的素数,从1的位置向右逐步的调整,每一个位置最多5次,有的位置不到5次; 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <cmath> 5 #define maxn 100010 6 using

CodeForces 580C 树+dfs搜索

Description Kefa decided to celebrate his first big salary by going to the restaurant. He lives by an unusual park. The park is a rooted tree consisting of n vertices with the root at vertex 1. Vertex 1 also contains Kefa's house. Unfortunaely for ou

Codeforces#498F. Xor-Paths(折半搜索)

time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output There is a rectangular grid of size n×mn×m. Each cell has a number written on it; the number on the cell (i,ji,j) is ai,jai,j. Your task is

Codeforces 1010F Xor-Paths 【搜索】【暴力!】

1 #include<iostream> 2 #include<map> 3 using namespace std; 4 5 long long a[25][25],ans,k,x1[25]; 6 int n,m,mid; 7 8 map<long long, int> mp[25][25]; 9 10 void dfs1(int x,int y,long long k1,int cnt){//从x,y的位置搜到中间行 == k1是一路上^得到的数 包括(x,y) 1