传送门

题目大意

给出由1~n组成的序列,每次可将一个区间翻转。问如何从1~n的递增序列变成给出的序列,输出操作次数以及每次操作的区间。最多翻转3次,保证有解,输出任意方案即可。

分析

我们对于每一次翻转只考虑枚举所有可能的点,即我们找出每一段连续区间的两个端点,然后枚举选取这些端点中的哪两个,之后暴力翻转这一段区间即可。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
int a[],b[][],nowx[],nowy[],n;
inline bool check(){
int i;
for(i=;i<=n;i++)
if(a[i]!=a[i-]+)return ;
return ;
}
inline void pr(int wh){
int i;
cout<<wh<<endl;
for(i=wh;i>;i--)cout<<nowx[i]<<' '<<nowy[i]<<endl;
exit();
}
inline void dfs(int wh){
if(check())pr(wh);
if(wh==)return;
int i,j,k;
int cnt=;
b[wh][++cnt]=;
for(i=;i<n;i++)
if(abs(a[i]-a[i-])!=||abs(a[i]-a[i+])!=)
b[wh][++cnt]=i;
b[wh][++cnt]=n;
for(i=;i<cnt;i++)
for(j=i+;j<=cnt;j++){
nowx[wh+]=b[wh][i],nowy[wh+]=b[wh][j];
for(k=;b[wh][i]+k<=b[wh][j]-k;k++)
swap(a[b[wh][i]+k],a[b[wh][j]-k]);
dfs(wh+);
for(k=;b[wh][i]+k<=b[wh][j]-k;k++)
swap(a[b[wh][i]+k],a[b[wh][j]-k]);
}
return;
}
int main(){
int m,i,j,k,x,y;
scanf("%d",&n);
for(i=;i<=n;i++)scanf("%d",&a[i]);
dfs();
return ;
}

339E Three Swaps的更多相关文章

  1. [codeforces 339]E. Three Swaps

    [codeforces 339]E. Three Swaps 试题描述 Xenia the horse breeder has n (n > 1) horses that stand in a ...

  2. uva331 - Mapping the Swaps

    Mapping the Swaps Sorting an array can be done by swapping certain pairs of adjacent entries in the ...

  3. UVA Mapping the Swaps

    题目例如以下: Mapping the Swaps  Sorting an array can be done by swapping certain pairs of adjacent entrie ...

  4. Three Swaps DFS

    E. Three Swaps time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  5. Codefoces 432 C. Prime Swaps

    哥德巴赫猜想: 任一大于2的偶数,都可表示成两个素数之和. 任一大于5的整数都可写成三个质数之和. 贪心取尽可能大的素数..... C. Prime Swaps time limit per test ...

  6. Swaps in Permutation

    Swaps in Permutation You are given a permutation of the numbers 1, 2, ..., n and m pairs of position ...

  7. [Swift]LeetCode801. 使序列递增的最小交换次数 | 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 ...

  8. [LeetCode] 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 ...

  9. HR_Minimum Swaps 2

    源代码超时 看了评论区改用了字典序列. #!/bin/python3 import math import os import random import re import sys # Comple ...

随机推荐

  1. java学习笔记 --- IO(3)

    1.FileReader:读取字符流,默认GBK public class CharStreamDemo { public static void main(String[] args) throws ...

  2. 在C#中,Json的序列化和反序列化的几种方式

    摘自:http://www.cnblogs.com/caofangsheng/p/5687994.html 在这篇文章中,我们将会学到如何使用C#,来序列化对象成为Json格式的数据,以及如何反序列化 ...

  3. Flea

    It is known that fleas in Berland can jump only vertically and horizontally, and the length of the j ...

  4. 2017.10.3北京清北综合强化班DAY3

    括号序列(bracket) Time Limit:1000ms   Memory Limit:128MB 题目描述 LYK有一个括号序列,但这个序列不一定合法. 一个合法的括号序列如下: ()是合法的 ...

  5. 安装webpack

    首先要安装 Node.js, Node.js 自带了软件包管理器 npm,Webpack 需要 Node.js v0.6 以上支持,建议使用最新版 Node.js. 用 npm 安装 Webpack: ...

  6. vertex shader must minimally write all four components of POSITION

    Though the POSITION semantic must be written out by the vertex shader, it cannot be read in by the p ...

  7. 学习动态性能表(11)v$latch$v$latch_children

    学习动态性能表 第十一篇-(1)-V$LATCH  2007.6.7 Oracle Rdbms应用了各种不同类型的锁定机制,latch即是其中的一种.Latch是用于保护SGA区中共享数据结构的一种串 ...

  8. 使用Json Template在Azure China创建ARM类型的虚拟机

    前面几篇文章介绍过Azure的两种VM的模式,包括ASM和ARM.并且介绍了如何用Azure CLI和PowerShell创建虚拟机.本文将介绍如何采用Json的Template来创建基于ARM的VM ...

  9. Java基础--阻塞队列ArrayBlockingQueue

    ArrayBlockingQueue是阻塞队列的一种,基于数组实现,长度固定,队尾添加,队首获取, 构造函数: ArrayBlockingQueue(int capacity) ArrayBlocki ...

  10. Spring集成Quartz定时任务框架介绍

    在JavaEE系统中,我们会经常用到定时任务,比如每天凌晨生成前天报表,每一小时生成汇总数据等等.我们可以使用java.util.Timer结合java.util.TimerTask来完成这项工作,但 ...