题目:
  有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. 最有效地优化 Microsoft SQL Server 的性能

      为了最有效地优化 Microsoft SQL Server 的性能,您必须明确当情况不断变化时,性能将在哪些方面得到最大程度的改进,并集中分析这些方面.否则,在这些问题上您可能花费大量的时间和精力 ...

  2. ubuntu登陆出现问题

    手贱改了用户root权限结果登陆时提示system administrator is not allowed to login from this screen(郁闷勒) 这时可以按ctrl+F2进入 ...

  3. jquery之empty()与remove()区别

    要用到移除指定元素的时候,发现empty()与remove([expr])都可以用来实现.可仔细观察效果的话就可以发现.empty()是只移除了 指定元素中的所有子节点,拿$("p" ...

  4. db2常用命令(1)

    DB2常用命令   1.启动实例(db2inst1):实例相当于informix中的服务 db2start 2.停止实例(db2inst1): db2stop 3.列出所有实例(db2inst1) d ...

  5. 使用C# yield关键字来提高性能和可读性

    对于”yield”这个关键字我已经见过N次了,直到最近我才知道这个关键字所蕴含的力量.我将在下面展示出一些使用”yield”让你的代码有更高可读性和更好性能的例子. 为了让你对yield有一些快速概览 ...

  6. ios html5 网页取消默认样式

    ios的的默认样式修改成扁平化的样式 重要的一句css  -webkit-appearance: none;  将样式清除 单数会出现将raido的选择按钮也会消失 所以需要对radio的样式进行重新 ...

  7. IntelliJ IDEA14 配置 SVN

    最新升级IDEA13到14版本,升级后发现IDEA中SVN无法正常使用,但文件夹下能够正常使用. 并且报错:svn: E204899: Cannot run program "svn&quo ...

  8. easyui editor combobox multiple

    $.extend($.fn.datagrid.defaults.editors,{ combobox: { init: function(container, options){ var combo ...

  9. Tomcat内存设置

    MyEclipse Tomcat的JDK菜单:“Optional Java VM arguments:”框中输入“有前置空格”   -Xms512m -Xmx512m -XX:PermSize=512 ...

  10. HTML标签的应用(新手)

    雪碧图(精灵图): sprite compass-合并(尽量宽高相同) 兼容性: 1.resct重置技术:normalize技术 2.加前缀:-webkit- -moz- -0- -ms- 3.< ...