传送门

题目大意

给出由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. 微服务:Eureka配置集群环境

    一.注册中心编码 1.使用idea创建一个spring boot项目,pom如下: <?xml version="1.0" encoding="UTF-8" ...

  2. jquery 获取所有父元素

    最终结果: 代码: <!DOCTYPE html> <html> <head> <style> b, span, p, html body { padd ...

  3. enumerate 枚举

  4. CodeForces - 896D :Nephren Runs a Cinema(卡特兰数&组合数学---比较综合的一道题)

    Lakhesh loves to make movies, so Nephren helps her run a cinema. We may call it No. 68 Cinema. Howev ...

  5. 【java规则引擎】简单规则的rete网络示意图

    一个Fact通过Session添加到规则网络中,如何进行规则匹配的大致过程如下 (1)通过根结点对象从EntryPointNode的Map集合中找到相应的EntryPointNode对象 (2)Ent ...

  6. CH5702 Count The Repetitions[倍增dp]

    http://contest-hunter.org:83/contest/0x50%E3%80%8C%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%E3%80%8D%E4%B ...

  7. java 实现拖动文件到窗口功能

    package tuo; import java.awt.BorderLayout; import java.awt.Color; import java.awt.datatransfer.DataF ...

  8. mapreduce-实现多表关联

    //map package hadoop3; import java.io.IOException; import org.apache.hadoop.io.LongWritable;import o ...

  9. 10个 NPM 使用技巧

    对于一个项目,常用的一些npm简单命令包含的功能有:初始化一个文件夹( npm init ),下载npm模块( npm install ),创建测试( npm tese ) 和自定义脚本( npm r ...

  10. hexo博客相关

    https://www.cnblogs.com/sulishibaobei/p/6428241.html 利用hexo+github+nodejs搭建自我博客的一天 http://www.sulish ...