题面:

传送门

A. Out of Sorts

Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 256 megabytes
 
Keeping an eye on long term career possibilities beyond the farm, Bessie the cow has started learning algorithms from various on-line coding websites.
Her favorite algorithm thus far is "bubble sort". Here is Bessie’s implementation, in cow-code, for sorting an array A of length N.
 
sorted = false
while (not sorted):
      sorted = true
      moo
      for i = 0 to N-2:
            if A[i+1] < A[i]:
                  swap A[i], A[i+1]
                  sorted = false
 
Apparently, the "moo"command in cow-code does nothing more than print out "moo". Strangely, Bessie seems to insist on including it at various points in her code.
Given an input array, please predict how many times "moo"will be printed by Bessie’s code.
 
Input
The first line of input contains N (1 ≤ N ≤ 100,000). The next N lines describe A[0]...A[N −1], each being an integer in the range 0...10^9. Input elements are not guaranteed to be distinct.
 
Output
Print the number of times "moo" is printed.
 
Example
Input
5
1
5
3
8
2
Output
4
 

题目描述:

有一头奶牛在网站学会了冒泡排序,但是奶牛写的代码里面每进行一次排序前就会输出“moo”。现在给出一组数,执行奶牛的代码,求:输出“moo”的次数。
 

题目分析:

这道题其实就是求冒泡排序的次数。刚开始可能的想法是:求每个数的逆序数。但这个太麻烦了,因为是A题。我们要对冒泡排序的“性质”进行思考:首先,每进行一次冒泡排序,是不是都是从左到右扫一遍?其次,关键的语句 if A[i+1] < A[i],也就是 if A[i] > A[i+1],即当前第i个元素如果比下一个元素(第i+1个元素)大,那么就进行交换。假如第i个元素是最大的,那么,第i个元素就直接到数组的最后面:
比8要小的元素是不是被“挤”到前面了(数字3,6,7,5,1,4,0),往前面挪了一步,这时就完成了一次冒泡排序
那么,再进行一次冒泡排序,结果又是怎样呢?我们发现,进行第二次冒泡排序时,数字7被移动到后面:
比7要小的元素继续往前挪了一步(数字5, 1, 4, 0)。
 
观察到了这里,是不是发现了一些规律?其实我们只需要计算往前“挪”的数总共移动了多少步,是不是就可以算出冒泡排序的次数?但是我们怎么知道哪些数往前面移动了?我们把这些数排完序后,只需要计算每一个往前面移动过的数移动了多少步,取最大值就行了。
 
写代码时,我们可以用一个结构体保存每个数原来的下标,这样排完序后就可以找到之前这个数的位置,然后计算移动步数。
注意:这里的排序不能用sort函数,要用stable_sort函数,因为用sort函数会改变相同元素的相对位置,而冒泡排序是不会改变相同元素的相对位置的,所以要用stable_sort这个不会改变相同元素的相对位置的函数。最后不要忘记排好序后还要一次冒泡排序进行检查。
 
 
AC代码:
 1 #include <cstdio>
2 #include <cstring>
3 #include <iostream>
4 #include <cmath>
5 #include <set>
6 #include <algorithm>
7 using namespace std;
8 const int maxn = 1e6+5;
9 int n;
10 struct node{
11 long long a; //要进行排序的元素
12 long long p; //下标
13 };
14 node A[maxn];
15
16 bool cmp(node x, node y){ //比较函数
17 return x.a < y.a; //从小到大排序
18 }
19
20 int main(){
21 scanf("%d", &n);
22
23 for(int i = 0; i < n; i++){
24 scanf("%lld", &A[i].a);
25 A[i].p = i;
26 }
27
28 stable_sort(A, A+n, cmp); //排序
29
30 int moo = 0; //初始化最大值
31 for(int i = 0; i < n; i++){
32 //往前面移动过的数就是之前没排序前的下标大于排序后的下标
33 //这里可以与求最大值结合在一起判断
34 if(A[i].p-i > moo){ //A[i]-i就是移动的步数
35 moo = A[i].p-i;
36 }
37 }
38
39 printf("%d\n", moo+1); //记得要+1
40 return 0;
41 }
 

2019 GDUT Rating Contest III : Problem A. Out of Sorts的更多相关文章

  1. 2019 GDUT Rating Contest III : Problem D. Lemonade Line

    题面: D. Lemonade Line Input file: standard input Output file: standard output Time limit: 1 second Memo ...

  2. 2019 GDUT Rating Contest III : Problem E. Family Tree

    题面: E. Family Tree Input file: standard input Output file: standard output Time limit: 1 second Memory ...

  3. 2019 GDUT Rating Contest III : Problem C. Team Tic Tac Toe

    题面: C. Team Tic Tac Toe Input file: standard input Output file: standard output Time limit: 1 second M ...

  4. 2019 GDUT Rating Contest II : Problem F. Teleportation

    题面: Problem F. Teleportation Input file: standard input Output file: standard output Time limit: 15 se ...

  5. 2019 GDUT Rating Contest I : Problem H. Mixing Milk

    题面: H. Mixing Milk Input file: standard input Output file: standard output Time limit: 1 second Memory ...

  6. 2019 GDUT Rating Contest I : Problem A. The Bucket List

    题面: A. The Bucket List Input file: standard input Output file: standard output Time limit: 1 second Me ...

  7. 2019 GDUT Rating Contest I : Problem G. Back and Forth

    题面: G. Back and Forth Input file: standard input Output file: standard output Time limit: 1 second Mem ...

  8. 2019 GDUT Rating Contest II : Problem G. Snow Boots

    题面: G. Snow Boots Input file: standard input Output file: standard output Time limit: 1 second Memory ...

  9. 2019 GDUT Rating Contest II : Problem C. Rest Stops

    题面: C. Rest Stops Input file: standard input Output file: standard output Time limit: 1 second Memory ...

随机推荐

  1. scu-4445

    Right turn frog is trapped in a maze. The maze is infinitely large and divided into grids. It also c ...

  2. l2-002 链表去重 (未解决)

    L2-002. 链表去重 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一个带整数键值的单链表L,本题要求你编写程序,删除 ...

  3. QUIC协议文档翻译——什么是QUIC

    原文地址https://docs.google.com/document/d/1gY9-YNDNAB1eip-RTPbqphgySwSNSDHLq9D5Bty4FSU/edit QUIC是一个谷歌提出 ...

  4. Java | 在 Java 中执行动态表达式语句: 前中后缀、Ognl、SpEL、Groovy、Jexl3

    在一些规则集或者工作流项目中,经常会遇到动态解析表达式并执行得出结果的功能. 规则引擎是一种嵌入在应用程序中的组件,它可以将业务规则从业务代码中剥离出来,使用预先定义好的语义规范来实现这些剥离出来的业 ...

  5. ES Next & Arrow function & Promise & Iterator & Generator yield & Async Await

    ES Next & Arrow function & Promise & Iterator & Generator yield & Async Await co ...

  6. UI 素材网站

    UI 素材网站 UI 设计师网站 国外: dribble.behance.Pinteres 国内: 优设.站酷.UI中国.花瓣 https://design.google/ https://dribb ...

  7. CSS flex waterfall layout

    CSS flex waterfall layout https://github.com/YoneChen/waterfall-flexbox https://css-tricks.com/snipp ...

  8. 移动端 750px UI 设计稿

    750px UI 设计稿 App 小程序 H5 rem & vh/vw 在移动端页面开发中,UI 一般会用750px(iphone 6)来出设计稿; 然后要求能够做到页面是自适应屏幕的,这种情 ...

  9. macOS & Nginx

    macOS & Nginx ngnix # 使用 brew 安装(如果没有 brew 命令,需要自行安装 brew) $ brew install nginx $ nginx -h # 查看 ...

  10. uniapp 发起网络请求

    推荐下我写的uni-http 创建http-config.js import Vue from 'vue' const BASE_URL = 'http://xxx.com'; if (process ...