339E Three Swaps
题目大意
给出由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的更多相关文章
- [codeforces 339]E. Three Swaps
[codeforces 339]E. Three Swaps 试题描述 Xenia the horse breeder has n (n > 1) horses that stand in a ...
- uva331 - Mapping the Swaps
Mapping the Swaps Sorting an array can be done by swapping certain pairs of adjacent entries in the ...
- UVA Mapping the Swaps
题目例如以下: Mapping the Swaps Sorting an array can be done by swapping certain pairs of adjacent entrie ...
- Three Swaps DFS
E. Three Swaps time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codefoces 432 C. Prime Swaps
哥德巴赫猜想: 任一大于2的偶数,都可表示成两个素数之和. 任一大于5的整数都可写成三个质数之和. 贪心取尽可能大的素数..... C. Prime Swaps time limit per test ...
- Swaps in Permutation
Swaps in Permutation You are given a permutation of the numbers 1, 2, ..., n and m pairs of position ...
- [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 ...
- [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 ...
- HR_Minimum Swaps 2
源代码超时 看了评论区改用了字典序列. #!/bin/python3 import math import os import random import re import sys # Comple ...
随机推荐
- signal 信号具体含义解释~
) SIGHUP 本信号在用户终端连接(正常或非正常)结束时发出,通常是在终端的控 制进程结束时, 通知同一session内的各个作业,这时它们与控制终端不再关联. ) SIGINT 程序终止(int ...
- bzoj4513 储能表
求 $\sum\limits_{i=0}^{n-1} \sum\limits_{j=0}^{m-1} max((x \space xor \space j) - k,0)$ ,膜 $p$ $n,m \ ...
- UVA - 11916 Emoogle Grid (组合计数+离散对数)
假如有这样一道题目:要给一个M行N列的网格涂上K种颜色,其中有B个格子不用涂色,其他每个格子涂一种颜色,同一列中的上下两个相邻格子不能涂相同颜色.给出M,N,K和B个格子的位置,求出涂色方案总数除以1 ...
- Redis底层探秘(三):字典
字典,又称为符号表(symbol table).关联数组(associative array)或映射(map),是一种用于保存键值对的抽象数据结构. 字典经常作为一种数据结构内置在很多高级编程语言里面 ...
- EF各版本增删查改及执行Sql语句
自从我开始使用Visual Studio 也已经经历了好几个版本了,而且这中间EF等框架的改变也算是比较多的.本篇文章记录下各个版本EF执行Sql语句和直接进行增删查改操作的区别,方便自己随时切换版本 ...
- 洛谷 P1062 数列
题目描述 给定一个正整数k(3≤k≤15),把所有k的方幂及所有有限个互不相等的k的方幂之和构成一个递增的序列,例如,当k=3时,这个序列是: 1,3,4,9,10,12,13,… (该序列实际上就是 ...
- h5废弃的标签和属性及新增的标签和属性
一.废弃的标签和属性 1.表现性元素 a) basefont b) big c) center d) font e) strike f) tt 2.框架类元素 a) frame b) frameset ...
- java 使用最新api操作mongodb
// package com.auto.test.dbmodel; import java.util.ArrayList; import org.bson.Document;import org.bs ...
- k2 4.6.9安装记录-够复杂了
首先需要准备一台Windows server 2008R2 系统.可以从微软官方下载. 下载地址: http://www.microsoft.com/zh-cn/download/confirmati ...
- qt在linux下使用open,write,close等文件操作
正常在linux下使用的文件操作函数为: #include <fcntl.h>fd = open(“path”,O_RDWR|O_NONBLOCK); 但是在qt中,需要使用: #incl ...