传送门

题目大意

给出由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. python-Django初体验

    1.搭建Django开发环境 2.创建工程与应用 CentOS6.5环境下 Python 2.6 ipython 1.2.1 Django 1.6.5 pip install -y django == ...

  2. flash、js 函数 互相调用

    js调用flex函数 flex 利用ExternalInterface.addCallback(“注册的方法名”,As中的函数名)进行注册 js中,用document.getElementById(“ ...

  3. git之commit改用第三方编辑器

    git commit -m "***"方式提交注释,有限制,可通过调用第三方编辑器来编辑更好的注释 1 使用window自带的记事本 git config --global cor ...

  4. LOJ 10189 仓库建设 ——斜率优化dp

    题目:https://loj.ac/problem/10189 #include<iostream> #include<cstdio> #include<cstring& ...

  5. BZOJ1202:[HNOI2005]狡猾的商人

    浅谈并查集:https://www.cnblogs.com/AKMer/p/10360090.html 题目传送门:https://lydsy.com/JudgeOnline/problem.php? ...

  6. SpringCloud微服务实战——第二章Springboot

    Spring Boot项目工程 src/main/java:主程序入口HelloApplication,可以通过直接运行该类来启动Spring Boot应用. src/main/resources:配 ...

  7. url出现特殊字符,需要进行编码

    1) 网络访问请求:中文空格字符编码/解码 stringByAddingPercentEscapesUsingEncoding(只对 `#%^{}[]|\"<> 加空格共14个字 ...

  8. “makefile”写法详解,一步一步写一个实用的makefile,详解 sed 's,$∗\.o[ :]*,\1.o $@ : ,g' < $@.

    目的:编写一个实用的makefile,能自动编译当前目录下所有.c/.cpp源文件,支持二者混合编译.并且当某个.c/.cpp..h或依赖的源文件被修改后,仅重编涉及到的源文件,未涉及的不编译. 二要 ...

  9. HDU1695(容斥原理)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  10. Java-API:java.util百科

    ylbtech-Java-API:java.util百科 包含集合框架.遗留的 collection 类.事件模型.日期和时间设施.国际化和各种实用工具类(字符串标记生成器.随机数生成器和位数组.日期 ...