2019 cattle off the holiday season team 3 - lemonade

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/C
Source: Cattle-off network

Title Description

It's a hot summer day on the farm, Farmer John N to give his cows made the lemonade! All N cows (for convenience, numbered 1 ... N) are like lemonade, just like some higher degree. Specifically, in order to obtain i cows lemonade behind most willing wi cows. Now all the N cows in the fields, but as long as Farmer John rang cow bells, these cows will immediately rushed to the FJ's lemonade stand. They will arrive before FJ began distributing lemonade, but no two cows will arrive at the same time. Further, when the cow reaches the i if and only if there is at most wi cows she would come in the queue line.
Farmer John want to prepare in advance a certain amount of lemonade, but he did not want to waste. The number of cows line up may depend on the order they arrive. Help him find the least possible number of cows line up.

Enter a description:

The first line contains N, the second row contains N integers separated by spaces w1, w2, ..., wN. Input ensure 1≤N≤100,000, in addition for each cow i, 0≤wi≤1000,000,000.
Description Output:
output order of arrival under the minimum number of cows may be queued in all possible cows.
Example 1

Entry

5
7 1 400 2 2

Export

3

In this case, it may last only three cows (which is the smallest possible value) in the team. And assuming w = w cows. 7 = 400 and the like in the first-team. W = 1 then the cow arrive and leave, since there has been two cows in the team. W = 2 then reaches the two cows, a left line, a leave.

Problem-solving ideas:

To achieve minimum resistance and other prioritization cows, cattle put all impatient later.
After ordering i see how many small (this cow into the i-th, while he waited up to w [i] personal) than w [i], is the answer

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 10:33:24.975 +08:00
 */
package ACMProblems.QianDaoTi;

import java.util.Arrays;

import static ACMProblems.ACMIO.*;

/*
 * 链接:https://ac.nowcoder.com/acm/contest/945/C
 * 来源:牛客网
 *
 * ## 题目描述
 * 这是农场上一个炎热的夏日,Farmer John要给他的N头奶牛发柠檬汽水了!所有的N头奶牛(方便起见,编号为1…N)都喜欢柠檬汽水,只是有些喜欢的程度更高一些。具体地说,奶牛i为了获得柠檬汽水最多愿意排在wi头奶牛之后。现在所有的N头奶牛都在田里,但是只要Farmer John敲响牛铃,这些奶牛就会立刻赶到FJ的柠檬汽水站。她们会在FJ开始分发柠檬汽水之前到达,但是没有两头奶牛会在同一时刻到达。此外,当奶牛i到达时,当且仅当至多有wi头奶牛在排队时她会来排队。
 * Farmer John想要提前准备一定量的柠檬汽水,但是他不想浪费。排队的奶牛的数量可能取决于她们到达的顺序。帮助他求出最少可能的排队的奶牛数量。
 *
 * ## 输入描述:
 * 第一行包含N,第二行包含N个用空格分隔的整数w1,w2,…,wN。输入保证1≤N≤100,000,此外对于每头奶牛i,0≤wi≤1000,000,000。
 * 输出描述:
 * 输出在所有可能的奶牛到达顺序之下,最小可能的排队的奶牛数量。
 * 示例1
 * ## 输入
 * > 5
 * 7 1 400 2 2
 *
 * ## 输出
 * >3
 *
 * **说明**
 * 在这个情况下,可能最后仅有三头奶牛在队伍中(这也是最小可能值)。假设w=7和w=400的奶牛先到并等在队伍中。然后w=1的奶牛到达并且会离开,这是由于已经有2头奶牛在队伍中了。然后w=2的两头奶牛到达,一头留下排队,一头离开。
 */
public class Carbonated {
    static Integer[] w;

    public static void main(String[] args) throws Exception {
        setStream(System.in);
        int n = nextInt();
        w = new Integer[n];
        for (int i = 0; i < n; ++i)
            w[i] = nextInt();
        Arrays.sort(w, (o1, o2) -> {
            if (o1 < o2)
                return 1;
            else if (o1.equals(o2))
                return 0;
            else return -1;
        });
        int ans = getAnswer(n);
        System.out.println(ans);
    }

    private static int getAnswer(int n) {
        if (n == 1) {
            if (w[0] > 0)
                return 1;
            else return 0;
        }
        if (w[0] > 0) {
            for (int i = 1; i < n; ++i) {
                if (w[i] < i)
                    return i;
            }
            return n;
        } else return 0;
    }
}

Guess you like

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