题解 | #淘汰分数#

淘汰分数

http://www.nowcoder.com/questionTerminal/9c4a4e879b4f49939dfaebea8948f976


import java.util.Scanner;
import java.util.*;


public class Main{


    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int x = sc.nextInt();
        int y = sc.nextInt();

        int[] scores = new int[n];
        for(int i=0;i<n;i++) scores[i] = sc.nextInt();
        Arrays.sort(scores);

        //dp[i] 代表 以scores[i-1]为分界线能够淘汰的人数 
        int[] dp = new int[n+1];

        dp[0] = 0;

        for(int i=0;i<n;i++){
            int pre = i;
            while(i<n-1 && scores[i]==scores[i+1]) i++;
            //判断相等分数的人数
            int num = i-pre+1;
            for(int j=pre;j<=i && j<n;j++){
                dp[j+1] = dp[pre] + num;
            }
        }

        //寻找最先符合的情况
        for(int i=1;i<=n;i++){
            if(dp[i]>=x && dp[i]<=y && n-dp[i] >= x && n-dp[i]<=y){
                System.out.println(scores[i-1]);
                return;
            }
        }
        System.out.println(-1);


    }
}

试卷解析 文章被收录于专栏

解析

全部评论

相关推荐

5 收藏 评论
分享
牛客网
牛客企业服务