题目:
  有n头数量的动物,开始它们站在一排,它们之间有高度差,所以需要将它们进行交换使得最终形成一个不减的序列,求它们交换的区间.
交换的规则:一段区间[l, r]将l与l+1、l+2与l+3、...、r-1与r交换.

分析:

  因为n不超过100,最多的交换次数为(100-1)*(100-1)<10000(序列最坏的情况),不会超过20000;因此可以不用合并,即可以不用将区间[1,2],[3,4]合并为[1,4].采用冒泡排序就可以解决.

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <ctime>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <set>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <iterator>
#include <vector> using namespace std; #define LL long long
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define MAXN 10000010
#define MAXM 1000010 const int maxn = ;
LL a[maxn]; int main()
{
int n;
int ans;
int pos, flag;
while(scanf("%d", &n)==)
{
int x, y;
ans = ;
int i, j;
for(i = ; i < n; i++ )
scanf("%lld", &a[i]);
for(i = ; i < n-; i++ )
{
flag = ;
pos = ;
x = ;
y = ;
for(j = ; j < n--i; j++ )
{
if(a[j] > a[j+])
{
flag = ;
pos++;
if(pos == )
{
x = j;
y = j+;
}
else
{
if(j - y == )
y = j+;
else if(j - y >= )
{
printf("%d %d\n", x+, y+);
x = j;
y = j+;
}
}
int temp;
temp = a[j];
a[j] = a[j+];
a[j+] = temp;
//printf("%d %d\n", j+1, j+2);
}
}
if(flag)
printf("%d %d\n", x+, y+);
}
} return ;
}

CodeForces 686B-Little Robber Girl's Zoo的更多相关文章

  1. CodeForces 686B Little Robber Girl's Zoo (构造冒泡排序)

    题意:给定一排列,让你通过一个区间交换的方式,完成排序. 析:这个题说了,最多不能超过20000次,而 n 最大才100,那么冒泡排序复杂度为 n * n,才10000,肯定是可以的,所以我们就模拟冒 ...

  2. codeforces 686B B. Little Robber Girl's Zoo(水题)

    题目链接: B. Little Robber Girl's Zoo //#include <bits/stdc++.h> #include <vector> #include ...

  3. Codeforces Round #359 (Div. 2) B. Little Robber Girl's Zoo 水题

    B. Little Robber Girl's Zoo 题目连接: http://www.codeforces.com/contest/686/problem/B Description Little ...

  4. Codeforces 437 D. The Child and Zoo 并查集

    题目链接:D. The Child and Zoo 题意: 题意比较难懂,是指给出n个点并给出这些点的权值,再给出m条边.每条边的权值为该条路连接的两个区中权值较小的一个.如果两个区没有直接连接,那么 ...

  5. codeforces 686B

    题意:给出一个序列,只允许进行相邻的两两交换,给出使序列变为非降序列的操作方案. 思路:关键点是操作次数不限,冒泡排序. #include<iostream> #include<cs ...

  6. 套题 codeforces 359

    A题:Free Ice Cream 注意要使用LL,避免爆int #include <bits/stdc++.h> #define scan(x,y) scanf("%d%d&q ...

  7. Codeforces Round #359 div2

    Problem_A(CodeForces 686A): 题意: \[ 有n个输入, +\space d_i代表冰淇淋数目增加d_i个, -\space d_i表示某个孩纸需要d_i个, 如果你现在手里 ...

  8. Codeforces Round #359 (Div. 2) B

    B. Little Robber Girl's Zoo time limit per test 2 seconds memory limit per test 256 megabytes input ...

  9. codeforces29A

    Spit Problem CodeForces - 29A In a Berland's zoo there is an enclosure with camels. It is known that ...

随机推荐

  1. C++ 学习笔记(3) —— 內联函数

    内联函数的用处: 用空间换取时间,在调用时不用每次都写调用的汇编. 什么时候内联: 比较小的函数:只有两三行 在循环里循环调用的函数 什么时候不内联: 比较大的函数,2.30行的 递归的函数

  2. django重量级web框架

    一个WEB项目要解决什么问题? 1. 基本核心功能,即将请求的request通过调用本地程序进行计算,数据库查询,返回数据,生成按照http协议生成新的数据返回给客户端,解析这些返回的数据就交由浏览器 ...

  3. 最快速的Android开发环境搭建ADT-Bundle及Hello World

    ADT-Bundle for Windows 是由Google Android官方提供的集成式IDE,已经包含了Eclipse,你无需再去下载Eclipse,并且里面已集成了插件,它解决了大部分新手通 ...

  4. Proteus 8 画原理图仿真 1602 LCD显示字符

    以下是源程序: #include <reg52.h> #include<intrins.h> /** * P2 上接的是 D1 ~ D7 */ sbit RS = P3 ^ ; ...

  5. mysql 乱码问题

    A.mysql设置 1.service mysql stop 2.sudo vim /etc/mysql/my.cnf 在[mysqld]中添加下面两行 character_set_server = ...

  6. 重置样式 - Eric Meyer的原版

    重置样式就是一组CSS声明,用来覆盖不同浏览器渲染HTML元素时的各种默认样式.重置样式一般会被加入到主样式文件的开头,用来将各个浏览器的自有默认样式重置成统一表现,确保样式表中后续追加的样式在不同浏 ...

  7. 关于css的一些知识点整理

    一.标签的类型:   行内:span.a.b.i.strong.em.   1.共处一行   2.不支持设置宽高 display:block; 转换成块 块:h1-h6 p div  ul ol 1. ...

  8. 制作图片边框:《CSS3 Border-image》

    一个边框图片border-image

  9. sublime 自动编译

    Tools --> Build System --> New: { "shell_cmd": "cc.bat \"$file\"" ...

  10. [问题2014A09] 复旦高等代数 I(14级)每周一题(第十一教学周)

    [问题2014A09]  设 \(A,B\) 分别是 \(3\times 2\), \(2\times 3\) 矩阵且满足\[AB=\begin{bmatrix} 8 & 2 & -2 ...