题目:
  有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. UICollectionView介绍

    文章原出处未知,如有朋友知道,请告诉我,我会补上. 1.1. Collection View 全家福: UICollectionView, UITableView, NSCollectionView ...

  2. IOS调试lldb命令常用,po,

    lldb命令常用(备忘) 假如你准备在模拟器里面运行这个,你可以在"(lldb)"提示的后面输入下面的: (lldb) po $eax LLDB在xcode4.3或者之后的版本里面 ...

  3. Linux下的PostgreSQL简单安装手册

    1. 安装环境     linux版本: CentOS release 6.2 (Final)     pg版本    : postgresql-9.5.0   2. pg数据库下载地址 --http ...

  4. POJ 2876 Cantoring Along

    Description The Cantor set was discovered by Georg Cantor. It is one of the simpler fractals. It is ...

  5. Going from u to v or from v to u?_POJ2762强连通+并查集缩点+拓扑排序

         Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K       Description I ...

  6. Doctrine2 SQL语句

    $q = Doctrine_Query::create() ->update('WebusersTable q') ->set('q.login_name','?','John') ) - ...

  7. 监控流量shell脚本

    # vi /etc/rc.d/traffic_monitor.sh---------------------------------------------- #!/bin/bash PATH=/bi ...

  8. IP和端口的相关检测

    1.查看自己电脑的ip,使用ipconfig命令 2.检测某个ip是否可以连通,直接使用ping命令 3.windows下查看本机都有哪些端口开放,使用netstat -anp tcp 命令 4.查看 ...

  9. NPM 使用介绍

    NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题,常见的使用场景有以下几种: 允许用户从NPM服务器下载别人编写的第三方包到本地使用. 允许用户从NPM服务器下载并 ...

  10. (六)makefile编程

    最简单的makefile: all: gcc server.c -o ser gcc client.c  -o cli clear: rm ser cli *.o -rf  #rm -rf表示删除文件 ...