看了codeforces上的大神写的题解之后,才知道这道题水的根本!

不过相对前面两题来说,这道题的思维要难一点;

不过想到了水的根本,这题也真心不难;

方法嘛,就像剥洋葱一样,从外面往里面剥;

所以呢,dfs,每次往左或者往右找到乱序的那个元素,反转一下;

这样就行了,而且题目说了保证能够在三次内搞定;

证明我也不会,意思应该就是这样了!

 #include<cstdio>
#include<vector>
#include<algorithm>
#define maxn 1009
using namespace std;
int a[maxn],n;
vector<int>l,r;
bool found=;
int checkleft()
{
for(int i=;i<=n;i++)
if(a[i]!=i) return i;
return -;
}
int checkright()
{ for(int i=n;i>;i--)
if(a[i]!=i) return i;
return -;
}
int getpos(int x)
{
for(int i=;i<=n;i++)
if(a[i]==x) return i;
}
void print()
{
printf("%d\n",l.size());
for(int i=l.size()-;i>=;i--)
printf("%d %d\n",l[i],r[i]);
} void dfs(int level)
{
if(checkleft()==-)
{
print();
found=;
}
if(found||level==) return;
int ll=checkleft();
int pp=getpos(ll);
reverse(a+ll,a+pp+);
l.push_back(ll);
r.push_back(pp);
dfs(level+);
if(found) return;
reverse(a+ll,a+pp+);
l.pop_back();
r.pop_back();
int rr=checkright();
pp=getpos(rr);
reverse(a+pp,a+rr+);
l.push_back(pp);
r.push_back(rr);
dfs(level+);
if(found) return;
reverse(a+pp,a+rr+);
l.pop_back();
r.pop_back();
} int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
dfs();
return ;
}

Codeforces Round #197 (Div. 2) : E的更多相关文章

  1. 线段树 Codeforces Round #197 (Div. 2) D. Xenia and Bit Operations

    题目传送门 /* 线段树的单点更新:有一个交叉更新,若rank=1,or:rank=0,xor 详细解释:http://www.xuebuyuan.com/1154895.html */ #inclu ...

  2. [置顶] Codeforces Round #197 (Div. 2)(完全)

    http://codeforces.com/contest/339/ 这场正是水题大放送,在家晚上限制,赛后做了虚拟比赛 A,B 乱搞水题 C 我是贪心过的,枚举一下第一个拿的,然后选使差值最小的那个 ...

  3. Codeforces Round #197 (Div. 2) (A、B、C、D、E五题合集)

    A. Helpful Maths 题目大意 给一个连加计算式,只包含数字 1.2.3,要求重新排序,使得连加的数字从小到大 做法分析 把所有的数字记录下来,从小到大排序输出即可 参考代码 #inclu ...

  4. Codeforces Round #197 (Div. 2)

    A.Helpful Maths 分析:将读入的字符转化为数字,直接排个序就可以了. #include <cstdlib> #include <cstring> #include ...

  5. Codeforces Round #197 (Div. 2) C,D两题

    开了个小号做,C题一开始看错范围,D题看了半小时才看懂,居然也升到了div1,囧. C - Xenia and Weights 给出一串字符串,第i位如果是1的话,表示有重量为i的砝码,如果有该种砝码 ...

  6. Codeforces Round #197 (Div. 2) : C

    哎....这次的比赛被安叔骂的好惨! 不行呢,要虐回来: 这道搜索,老是写错,蛋疼啊! 果然是基础没打好! #include<cstdio> using namespace std; ], ...

  7. Codeforces Round #197 (Div. 2) : D

    这题也是一个线段树的水题: 不过开始题目没看明白,害得我敲了一个好复杂的程序.蛋疼啊.... 最后十几分钟的时候突然领悟到了题意,但是还是漏掉一个细节,老是过不去... 以后比赛的时候不喝啤酒了,再也 ...

  8. Codeforces Round #197 (Div. 2) : B

    也是水题一个,不过稍微要细心点.... 贴代码: #include<iostream> using namespace std; long long n,m; ; int main() { ...

  9. Codeforces Round #197 (Div. 2) : A

    水题一个: 直接贴代码: #include<cstdio> #include<algorithm> #include<cstring> using namespac ...

随机推荐

  1. Android源码解析--Quick Search in Settings

    欢迎大家加入群里交流:429664282 基于:android-6.0.1_r17 f4b8ad6 Android Settings中存在一个SearchIndexablesProvider,它提供了 ...

  2. 开源安全测试 - BackTrack的应用

    开源安全测试 - BackTrack的应用: http://gdtesting.com/product.php?id=99  

  3. maven主仓库中找不到restlet的解决办法

    解决办法: 修改Pom.xml  增加 <repositories>         <repository>             <id>maven-rest ...

  4. Yii::app()方法详解

    Yii::app()方法详解 1.Yii::app()返回的是一个当前框架应用程序的对象 2.Yii::app()是framework/web/CWebApplication.php类实例化的对象,所 ...

  5. 隐藏/显示&nbsp;我的电脑盘符驱动…

    组策略里更改即可:点击"开始"→"运行",输入"gpedit.msc",打开组策略.在窗口左侧的"本地计算机策略"中依次 ...

  6. store procedure example

    SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- ...

  7. 那天有个小孩跟我说LINQ(五)转载

    2  LINQ TO SQL(代码下载)      我们以一个简单的销售的业务数据库为例子         表结构很简单:Users(购买者(用户)表),Products(产品信息表),Sales(销 ...

  8. java.lang.IllegalStateException: You need to use a theme.appcompat theme (or descendant) with this activity

    错误描述:java.lang.IllegalStateException: You need to use a theme.appcompat theme (or descendant) with t ...

  9. C#创建微信自定义菜单

    string posturl = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + access_to ...

  10. stm32中.bss和.data段是在哪里初始化的

    https://segmentfault.com/q/1010000004829859/a-1020000004850311 Q: STM32的启动文件startup_stm32f10x_hd.s中的 ...