ZeroJudge D906: Arranging Seats

Fengshou Elementary School is a small primary school with only 6 new first-grade students enrolled. Therefore, the teacher arranged their seats in a 2×3 grid pattern and numbered the seats (from 1 to 6) as shown in the following diagram:

123
456

For the sake of the students' eyesight, the teacher changes the seating arrangement on the first day of class each month. The seating arrangement follows these rules:

  1. In odd-numbered months (e.g., January, March, May, ...), the seats are arranged in ascending order according to the students' ID numbers.
  2. In even-numbered months (e.g., February, April, June, ...), the seats are arranged in ascending order based on the students' heights. If two students have the same height, the student with the smaller ID number is placed in front. Student ID numbers are unique, but heights may be the same for different students.
  3. The teacher uses a lottery system to draw a number from 1 to 6, which determines the seat number for the first student in the sorted order. The following students will then sit in the next seat number in sequence. If a student is assigned to seat number 6, the next student in the order will sit in seat number 1.

In this seating arrangement, seat number 2 is closest to the teacher and the blackboard, so after the seats are assigned, everyone is eager to know who sits in seat number 2. For example, in November, the students are rearranged based on their ID numbers in ascending order, resulting in the student names A, B, C, D, E, and F. If the teacher draws the number 5, the final seating arrangement is as follows:

CDE
FAB

In the given example, student D will be sitting in seat number 2.

Now, please write a program to determine which student is sitting in seat number 2 after the seating arrangement.

Sample Inputs/Outputs

Sample Input(s)Sample Output(s)
The first line contains a positive integer M (1 ≤ M ≤ 12), which represents the month when the seats are rearranged.
The second line contains a positive integer N (1 ≤ N ≤ 6), which is the number drawn by the teacher.
The third to eighth lines contain student data, with each line consisting of three items separated by spaces. The first item is the student's name, represented by a capital letter (A to Z). The second item is the student's seat number, consisting of three digits (001 to 999). The third item is the student's height, also consisting of three digits (001 to 999), representing the height in centimeters.
Please output a single letter representing the name of the student sitting in seat number 2.
11
5
A 101 147
B 102 120
C 103 108
D 104 130
E 105 140
F 106 120
D
4
2
L 102 127
K 101 120
M 103 138
B 104 130
A 106 110
S 105 120
A
ZeroJudge D906 範例測資

Thought Process

Use a **Map** to store the character corresponding to each seat number. If sorting by seat number, place the seat numbers into an array and sort them afterward. If sorting by height, use **Pairs** for sorting, with height as the primary key and seat number as the secondary key. Finally, output the name of the student sitting in the second seat.

Sample Code-ZeroJudge D906: Arranging Seats

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

pair<int, int> rtn (int a, int b)
{
    pair<int, int> tmp;
    tmp.first = a;
    tmp.second = b;
    return tmp;
}

int main() {
    cin.sync_with_stdio(0);
    cin.tie(0);
    map<int, int>index;
    index[1] = 1;
    index[2] = 0;
    index[3] = 5;
    index[4] = 4;
    index[5] = 3;
    index[6] = 2;
    int M, N;
    cin >> M >> N;
    if (M % 2 == 1)
    {
        vector<int>num;
        map<int, char>MAP;
        for (int i = 0; i<6; i++)
        {
            char ch;
            int ID, height;
            cin >> ch >> ID >> height;
            MAP[ID] = ch;
            num.push_back(ID);
        }
        sort(num.begin(), num.end());
        cout << MAP[num[index[N]]] << "\n";
    }
    else
    {
        vector<pair<int, int>>num;
        map<pair<int, int>, char>MAP;
        for (int i = 0; i<6; i++)
        {
            char ch;
            int ID, height;
            cin >> ch >> ID >> height;
            MAP[rtn(height, ID)] = ch;
            num.push_back(rtn(height, ID));
        }
        sort(num.begin(), num.end());
        cout << MAP[num[index[N]]] << "\n";
    }
}

//ZeroJudge D906
//Dr. SeanXD

Comments