Sort Getting Started Exercises 7 scores delineated problem solution

Written for: "Informatics Olympiad through a" second chapter on exercise machine 5 / Luo Gu P1068

Title Description

Expo volunteers A city selection work is being carried out in full swing. In order to select the right people, \ (A \) City players of all applicants were written test, written test score of interview scores of players before entering the interview. Interview scores based on the number of planned admissions \ (150% \) delineated that if you plan to enroll \ (m \) volunteers, interviewing scores of the ranked \ (m \ Times 150 \% \) (rounded down ) the name of the player scores, and ultimately into the interview players for the written test score of all players scores of interviews.
Now you write a program delineated interview scores, and the output of all players entering the registration number of the interview and written exam.

Input Format

The first line, two integers \ (n-, m (≤ n-5000,3. 5 ≤ ≤ ≤ n-m) \) , separated by a space, where \ (n-\) represents the total number of players entered entrant, \ (m \) represents the number of volunteers enrolled in the program. To ensure that the input data \ (m \ times 150 \% \) after rounding down less \ (n-\) .

The second row of the \ (n + 1 \) rows, each row comprising two integers, separated by a space, respectively, the player registration number \ (k (1000 ≤ k ≤ 9999) \) and the player The written exam \ (S (1 ≤ S ≤ 100) \) . Data to ensure players registration number varies.

Output Format

The first line, with a \ (2 \) integer, separated by a space, a first integer interview score; the actual number of the second integer the player to enter the interview.
From the beginning of the second row, each row containing \ (2 \) integer, the intermediate separated by a space, respectively, to the interview and player registration numbers written test results, according to the written test results from high to low output, if the same results, press output register numbers in ascending order.

Sample input

6 3 
1000 90 
3239 88 
2390 95 
7231 84 
1005 95 
1001 88

Sample Output

88 5 
1005 95 
2390 95 
1000 90 
1001 88 
3239 88 

Topic analysis

First define a named Nodestructure (I do not usually accustomed to knowing how to use named Nodeto represent the structure before are used Studentto indicate the reason for the structure of "Student" is "student" in English, but volunteers English "volunteer" a little difficult to fight, although he should be in the exam vocabulary, but here I am still writing Nodeit, we put the focus on the topics addressed, and I do not like a teacher named entangled in embarrassing structure), it It includes players registration number kand a written exam s, and we initialize an array of structures defining the structure of problems at the same time:

struct Node {
    int k;  // 报名号
    int s;  // 成绩
} a[5005];

Then is our comparison function, a comparison function to register according to the following scores from high to low, the same score of resolution from small to large:

bool cmp(Node a, Node b) {
    if (a.s != b.s) // 如果成绩不同
        return a.s > b.s;   // 按成绩从高到低排
    return a.k <b.k; // 否则,按报名号从小到大排
}

After drained before order, we only need to select \ (m \ times 150 \% \) which can be a bit before.
Interview score is ranked \ (m \ times 150 \% \) of the competitor's scores.
Codes are as follows:

#include <bits/stdc++.h>
using namespace std;
struct Node {
    int k, s;  // 分别表示报名号和成绩
} a[5005];
int n, m;
bool cmp(Node a, Node b) {
    if (a.s != b.s) // 如果成绩不同
        return a.s > b.s;   // 按成绩从高到低排
    return a.k <b.k; // 否则,按报名号从小到大排
}
int main() {
    cin >> n >> m;
    for (int i = 0; i < n; i ++)
        cin >> a[i].k >> a[i].s;
    sort(a, a+n, cmp);
    int line = a[m*3/2-1].s;    // line表示分数线
    int id = m*3/2-1;   // id对应最后录取的那个人的坐标
    while (id+1 < n && a[id+1].s == line) id ++;
    cout << line << " " << id+1 << endl; // 输出分数线及录取人数
    for (int i = 0; i <= id; i ++)  // 输出录取人的名单
        cout << a[i].k << " " << a[i].s << endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/zifeiynoip/p/11450517.html