首页 > 试题广场 >

分数线划定

[编程题]分数线划定
  • 热度指数:6152 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
\hspace{15pt}某市为世博会选拔志愿者,先进行笔试,再按笔试成绩划定面试分数线。规则如下:
\hspace{23pt}\bullet\, 计划最终录取 m 名志愿者;
\hspace{23pt}\bullet\, 面试名额定为 m150\%,向下取整,记为 t=\left\lfloor 1.5m \right\rfloor
\hspace{23pt}\bullet\, 将所有报名号及笔试成绩按成绩从高到低、成绩相同报名号从小到大排序;
\hspace{23pt}\bullet\,t 名选手的成绩即为面试分数线
\hspace{23pt}\bullet\, 所有笔试成绩不低于该分数线的选手均进入面试。

\hspace{15pt}请输出面试分数线及所有进入面试选手的信息(按排序后的顺序)。

输入描述:
\hspace{15pt}第一行输入两个整数 n,m\left(5\leqq n\leqq 5000;\ 3\leqq m\leqq n\right),分别表示报名人数与计划录取人数。
\hspace{15pt}接下来 n 行,每行输入两个整数 k,s\ (1000\leqq k\leqq 9999;\ 1\leqq s\leqq 100),分别为报名号与笔试成绩。报名号保证唯一。


输出描述:
\hspace{15pt}第一行输出两个整数:面试分数线 \textit{line} 与进入面试的人数 cnt
\hspace{15pt}接下来 cnt 行,按排序顺序输出每位选手的报名号 k 与成绩 s,每行两个整数,用空格分隔。
示例1

输入

6 3
9848 90
6731 88
1422 95
7483 84
8805 95
4162 88

输出

88 5
1422 95
8805 95
9848 90
4162 88
6731 88

说明

计算:t=\lfloor1.5\times3\rfloor=4,第 4 名成绩为 88,故分数线 =88;所有 \geqq88 的共有 5 人。
import java.util.*;

import static java.util.Comparator.comparing;
import static java.util.Comparator.reverseOrder;

public class pNc_49 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int num = in.nextInt();
        int need = in.nextInt();
        int m = (int)(1.5*need);
        ArrayList<Integer[]> list = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            Integer[] tmp = new Integer[]{in.nextInt(), in.nextInt()};
            list.add(tmp);
        }

        //Collections.sort
        Collections.sort(list,
                // 第一级:按键 1 的值升序(使用 reversed() 反转默认的升序)
                comparing((Integer[] tmp) -> tmp[1], reverseOrder())
                        // 第二级:按键 2 的值降序
                        .thenComparing((Integer[] tmp) -> tmp[0])
        );


/*        //详情见:https://blog.csdn.net/qq_31635851/article/details/120269813
        //list.sort简略写法
        list.sort(comparing((Integer[] tmp) -> tmp[1], reverseOrder())
                .thenComparing(tmp -> tmp[0])// 编译器可以推断类型
        );*/

        int pass = list.get(m-1)[1];
        int pCount = m;
        for (int i = m; i < num; i++) {
            if (list.get(i)[1]==pass){
                pCount++;
            }else {
                break;
            }
        }
        System.out.println(pass+" "+pCount);
        for (int i = 0; i < pCount; i++) {
            System.out.println(list.get(i)[0]+" "+list.get(i)[1]);
        }

    }
}

发表于 2025-11-24 03:27:34 回复(0)
最笨的办法
import java.util.Arrays;
import java.util.Scanner;
import java.util.TreeMap;
import java.util.TreeSet;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        int t =  (int)(m * 1.5 ) ;

        int[][] score = new int[n][2];

        //存储所有人的分数
        for (int i = 0; i < n; i++        ) {
            score[i][0] = in.nextInt();
            score[i][1] = in.nextInt();
        }
        // System.out.println(Arrays.toString(score));

        int[] id = new int[n];
        int[] po = new int[n];

        //按分数排序分开存储
        for (int j = 0; j < n; j++) {
            int max = 0;
            for (int i = 0; i < n; i++) {
                max = Math.max(max, score[i][1]);
            }
            for (int i = 0; i < n; i++) {
                if (score[i][1] == max) {
                    id[j] = score[i][0];
                    po[j] = score[i][1];
                    score[i][0] = 0;
                    score[i][1] = 0;
                    break;
                }

            }
            // System.out.println(max);
        }

        // System.out.println(Arrays.toString(id));
        // System.out.println(Arrays.toString(po));

        //如果考号前大后小,交换顺序
        for (int j = 0; j < n; j++) {
            for (int i = 0; i < n - 1; i++) {
                if (po[i] == po[i + 1] && id[i] > id[i + 1]) {
                    int temp = id[i];
                    id[i] = id[i + 1];
                    id[i + 1] = temp;
                }
            }
        }

        //计算分数线,这里一定要t-1,因为po的编号从0开始
        int line = po[t-1];

        //满足分数线的人计数
        int count =0;
        for(int i=0; i<n; i++){
            if(po[i] >=line){
                count++;
            }
        }

        System.out.println(line+" "+count);

        for (int i = 0; i < n; i++) {
            if (po[i] >= line) {
                System.out.println(id[i] + " " + po[i]);
            }
        }

    }
}

发表于 2025-10-08 15:11:11 回复(0)
import java.util.*;
import java.io.*;


// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String[] line1 = bf.readLine().split(" ");
        int n = Integer.parseInt(line1[0]);
        int m = Integer.parseInt(line1[1]);
        ArrayList<Student> list = new ArrayList<>();
        String line;
        while ((line = bf.readLine()) != null) {
            Student s = new Student(Integer.parseInt(line.split(" ")[0]),
                                    Integer.parseInt(line.split(" ")[1]));
            list.add(s);

        }
        list.sort(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int i = o2.score - o1.score;//分数从高到低
                //如分数相同再按id排序(升序)
                //如果i非0,直接用分数差的正负值排序
                i = i == 0 ? o1.id - o2.id : i;
                return i;
            }
        });
        int en_num = (int)Math.floor(m *1.5); //实际进面人数由分数线决定,同分可进,大于en_num数
        int en_sco = list.get(en_num - 1).score;
        int count = 0;//统计分数
        for (int i = 0; i < n; i++) {
            if (list.get(i).score < en_sco) {
                break;
            }
            count++;
        }

        // System.out.println(m);
        System.out.println(en_sco + " " + count);
        for (int i = 0; i < count; i++) {
            System.out.println(list.get(i));
        }

    }
}
class Student {
    int id;
    int score;
    public Student(int id, int score) {
        this.id = id;
        this.score = score;
    }

    @Override
    public String toString() {
        return id + " " + score;
    }
}

发表于 2025-09-20 12:16:52 回复(0)
import java.util.*;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        int t = (int) Math.floor(1.5 * m);
        List<Candidate> candidates = new ArrayList<>();
        while (n-- > 0) {
            int k = in.nextInt();
            int s = in.nextInt();
            Candidate candidate = new Candidate(k, s);
            candidates.add(candidate);
        }
        List<Candidate> collect = candidates.stream().sorted(Comparator.comparing(Candidate::getScore).reversed().thenComparing(Candidate::getId))
                .collect(Collectors.toList());
        int score = collect.get(t - 1).getScore();
        List<Candidate> collect1 = collect.stream().filter(c -> c.getScore() >= score).collect(Collectors.toList());
        System.out.println(score + " " + collect1.size());
        collect1.forEach(c -> System.out.println(c.getId() + " " + c.getScore()));
    }
}

class Candidate {
    int id;
    int score;
    public Candidate(int id, int score) {
        this.id = id;
        this.score = score;
    }

    public int getId() {
        return id;
    }

    public int getScore() {
        return score;
    }
}

发表于 2025-09-19 12:21:16 回复(0)
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();

        int t = (int) (m * 1.5);
        List<Candidate> candidates = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            int k = scanner.nextInt();
            int s = scanner.nextInt();
            candidates.add(new Candidate(k, s));
        }

        // 排序:成绩从高到低,成绩相同报名号从小到大
        // Override Comparator比较器
        candidates.sort((o1, o2)-> {
            if (o1.score != o2.score) return o2.score - o1.score;
            else return o1.id - o2.id;
        });

        int line = candidates.get(t - 1).score;
        int cnt = 0;
        List<Candidate> interviewCandidates = new ArrayList<>();
        for (Candidate candidate : candidates) {
            if (candidate.score >= line) {
                interviewCandidates.add(candidate);
                cnt++;
            }
        }

        System.out.println(line + " " + cnt);
        for (Candidate candidate : interviewCandidates) {
            System.out.println(candidate.id + " " + candidate.score);
        }

        scanner.close();
    }
}

class Candidate {
    int id;
    int score;

    public Candidate(int id, int score) {
        this.id = id;
        this.score = score;
    }
}

发表于 2025-08-29 15:33:35 回复(0)