zoukankan      html  css  js  c++  java
  • CodeForces 686B-Little Robber Girl's Zoo

    题目:
      有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].采用冒泡排序就可以解决.

    代码如下:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <fstream>
     5 #include <ctime>
     6 #include <cmath>
     7 #include <cstdlib>
     8 #include <algorithm>
     9 #include <set>
    10 #include <map>
    11 #include <list>
    12 #include <stack>
    13 #include <queue>
    14 #include <iterator>
    15 #include <vector>
    16 
    17 using namespace std;
    18 
    19 #define LL long long
    20 #define INF 0x3f3f3f3f
    21 #define MOD 1000000007
    22 #define MAXN 10000010
    23 #define MAXM 1000010
    24 
    25 const int maxn = 105;
    26 LL a[maxn];
    27 
    28 int main()
    29 {
    30     int n;
    31     int ans;
    32     int pos, flag;
    33     while(scanf("%d", &n)==1)
    34     {
    35         int x, y;
    36         ans = 0;
    37         int i, j;
    38         for(i = 0; i < n; i++ )
    39            scanf("%lld", &a[i]);
    40         for(i = 0; i < n-1; i++ )
    41         {
    42             flag = 0;
    43             pos = 0;
    44             x = 0;
    45             y = 0;
    46             for(j = 0; j < n-1-i; j++ )
    47             {
    48                 if(a[j] > a[j+1])
    49                 {
    50                     flag = 1;
    51                     pos++;
    52                     if(pos == 1)
    53                     {
    54                         x = j;
    55                         y = j+1;
    56                     }
    57                     else
    58                     {
    59                         if(j - y == 1)
    60                             y = j+1;
    61                         else if(j - y >= 2)
    62                         {
    63                             printf("%d %d
    ", x+1, y+1);
    64                             x = j;
    65                             y = j+1;
    66                         }
    67                     }
    68                     int temp;
    69                     temp = a[j];
    70                     a[j] = a[j+1];
    71                     a[j+1] = temp;
    72                     //printf("%d %d
    ", j+1, j+2);
    73                 }
    74             }
    75             if(flag)
    76                 printf("%d %d
    ", x+1, y+1);
    77         }
    78     }
    79 
    80     return 0;
    81 }
  • 相关阅读:
    JS事件学习笔记(思维导图)
    [logstash-input-file]插件使用详解
    echarts折线图,纵坐标数值显示不准确的问题解决
    IDEA 创建maven jar、war、 pom项目
    Lombok介绍、使用方法和总结
    Springboot2.0访问Redis集群
    springboot2.x 整合redis集群的几种方式
    SpringBoot 2.x 使用Redis作为项目数据缓存
    Springboot2.x使用redis作为缓存
    SpringBoot中application.yml基本配置详情
  • 原文地址:https://www.cnblogs.com/xl1164191281/p/5677142.html
Copyright © 2011-2022 走看看