2019 cattle off the holiday season team 3 - cows code

Creative Commons License Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )

Links: https://ac.nowcoder.com/acm/contest/945/H
Source: Cattle-off network

Title Description

Pay attention to the possibility of a long-term career outside the farm, cow Bessie started learning algorithms on different online programming site.
Her favorite method is by far the most "bubble sort." This is Bessie cows sort code length N of array A is achieved.

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

Clearly, the role of cows codes "moo" instruction only output "moo". The strange thing is, Bessie seemed obsessed with different positions in her code to use this statement.

Given an input array, Bessie predict how many times the code "moo" output.

Enter a description:

The first line of input contains N (1≤N≤100,000). The next line N described A [0] ... A [N -1], each number is in a range of
0 ... 10
. 9
integer of 0 ... 109. The input data is not guaranteed varies.

Output Description:

Output "moo" is the number of times the output.

Entry

5
1
5
3
8
2

Export

4

Problem-solving ideas

moo output number, left shift is equal to that of the highest number of items left, i.e., the maximum index difference before and after sorting + 1, even if the results.
The reason is that every time the bubble sort of pushed to the back of one of the largest arrays, which leads back to the other numbers to the left, then to the left to push the number is how many rounds.
Such as: 1. 3. 4. 6 9 9. 4. 5. 6
Here Insert Picture Description
2 farthest to the left, with 8 bits, the result is 8 + 1 = 9
solving only do it first pre-sorted to find that the maximum index difference, then +1 It can be.

AC Code:

/*
 * Copyright (c) 2019 Ng Kimbing, HNU, All rights reserved. May not be used, modified, or copied without permission.
 * @Author: Ng Kimbing, HNU.
 * @LastModified:2019-06-25 T 15:45:41.024 +08:00
 */
package ACMProblems.QianDaoTi;

import java.util.Arrays;

import static ACMProblems.ACMIO.*;

/*
 * 链接:https://ac.nowcoder.com/acm/contest/945/H
 * 来源:牛客网
 *
 * ## 题目描述
 * 留意着农场之外的长期职业生涯的可能性,奶牛Bessie开始在不同的在线编程网站上学习算法。
 * 她到目前为止最喜欢的算法是“冒泡排序”。这是Bessie的对长度为N的数组A进行排序的奶牛码实现。
 *
 * ```python
 * 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
 * ```
 *
 * 显然,奶牛码中的“moo”指令的作用只是输出“moo”。奇怪的是,Bessie看上去执着于在她的代码中的不同位置使用这个语句。
 *
 * 给定一个输入数组,请预测Bessie的代码会输出多少次“moo”。
 *
 * ## 输入描述:
 * 输入的第一行包含N(1≤N≤100,000)。接下来N行描述了A[0]…A[N−1],每个数都是一个范围为
 * 0...10
 * 9
 * 0...109的整数。输入数据不保证各不相同。
 * ## 输出描述:
 * 输出“moo”被输出的次数。
 * ## 输入
 * >5
 * 1
 * 5
 * 3
 * 8
 * 2
 * ## 输出
 * >4
 */
public class BubbleSortCount {
        static class MyPair {
        int index;
        int value;

        public MyPair(int index, int value) {
            this.index = index;
            this.value = value;
        }
    }

    public static void main(String[] args) throws Exception {
        setStream(System.in);
        int n = nextInt();
        int[] arr = new int[n];
        MyPair[] pairs = new MyPair[n];
        for (int i = 0; i < n; ++i) {
            int foo = nextInt();
            pairs[i] = new MyPair(i, foo);
        }
        Arrays.sort(pairs, (o1, o2) -> {
            if (o1.value != o2.value)
                return Integer.compare(o1.value, o2.value);
            else return Integer.compare(o1.index, o2.index);
        });
        int ans = -0x3f3f3f3f;
        for (int i = 0; i < n; ++i)
            ans = Math.max(ans, pairs[i].index - i);
        System.out.println(ans + 1);
        // 5 5 4 3 1 0 2 1 0
        //
    }
}

Guess you like

Origin blog.csdn.net/weixin_44090305/article/details/93626321
Recommended