传送门

题目大意

给出由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. Codeforces Round #253 (Div. 2)B(暴力枚举)

    就暴力枚举所有起点和终点就行了. 我做这题时想的太多了,最简单的暴力枚举起始点却没想到...应该先想最简单的方法,层层深入. #include<iostream> #include< ...

  2. Codeforces Round #258 (Div. 2)C(暴力枚举)

    就枚举四种情况,哪种能行就是yes了.很简单,关键是写法,我写的又丑又长...看了zhanyl的写法顿时心生敬佩.写的干净利落,简直美如画...这是功力的体现! 以下是zhanyl的写法,转载在此以供 ...

  3. memcache应对缓存失效问题

    .两个key,一个key用来存放数据,另一个用来标记失效时间 比如key是aaa,设置失效时间为30s,则另一个key为expire_aaa,失效时间为25s. 在取数据时,用multiget,同时取 ...

  4. 【整理】C++中的unique函数

    之前总结了一下我觉得有用的erase,lower_bound,upper_bound. 现在总结一下unique,unique的作用是“去掉”容器中相邻元素的重复元素(不一定要求数组有序),它会把重复 ...

  5. 转载:jquery插件实现图片延迟加载(lazyload.js)

    转载: http://www.cnblogs.com/tinyphp/archive/2013/04/09/3009385.html

  6. 使用python实现两个文件夹里文件的对比(包含内容的对比)

    #-*-coding:utf-8-*- #=============================================================================== ...

  7. 学习动态性能表(16)--v$rowcache

    学习动态性能表 第16篇--V$ROWCACHE  2007.6.12 本视图显示数据字典缓存(也叫rowcache)的各项统计.每一条记录包含不同类型的数据字典缓存数据统计,注意数据字典缓存有层次差 ...

  8. SVN 命令使用-***

    1.检出到某一版本: svn checkout -r 974  svn://220.231.xx.xx/仓库名 svn checkout  svn://220.231.xx.xx/仓库名 svn ex ...

  9. Azure的CentOS上安装LIS (Linux Integration Service)

    Azure上虚拟化技术都是采用的Hyper-v,每台Linux虚拟机都安装了LIS(Linux Integration Service).LIS的功能是为VM提供各种虚拟设备的驱动.所以LIS直接影响 ...

  10. HDU3887(树dfs序列+树状数组)

    Counting Offspring Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...