ZeroJudge F634: The Return of Soldiers

The Battle of Mantou Mountain was extremely fierce. The Dawn Army headquarters dispatched N officers and soldiers for a full-scale attack. After seven days and nights of fighting, they finally captured the hilltop. The surviving officers and soldiers began to return to the unit one after another. The headquarters hopes to count the surviving officers and soldiers from this battle. However, the situation is somewhat complicated.

The officers and soldiers belong to different branches of the military: Navy (1), Army (2), Air Force (3)
Also, they have different ranks: Officer (1), Sergeant (2), Soldier (3).

There are cases where the same officer or soldier is registered multiple times, and to make matters worse, there are individuals with the same name (if name, branch, and rank are the same, they are considered the same person). The headquarters found this task to be more difficult than capturing the hill. Please help write a program to handle this statistical task.

Sample Inputs/Outputs

Sample Input(s)Sample Output(s)
The first line of the test data contains two integers, N and M.
(1 <= N, M <= 1000,000)
Starting from the second line, there are M lines. Each line contains a string S, an integer A, and an integer B:
S: Name (less than 10 characters, composed of 'A' to 'Z')
A: Branch (Navy: 1, Army: 2, Air Force: 3)
B: Rank (Officer: 1, Sergeant: 2, Soldier: 3)
The first line outputs the number of surviving personnel for each branch of the military.
The second line outputs the number of surviving personnel for each rank.
The third line outputs the percentage of surviving personnel (with one decimal place).
15 10
ABC 1 2
ABC 2 1
ABC 2 1
ACD 3 3
ACD 1 2
ACD 1 1
BBC 2 1
BBC 1 2
BBC 1 2
BBE 3 3
navy:4 army:2 air:2
officer:3 sergeant:3 soldier:2
survival rate: 53.3%
ZeroJudge F634 範例測資

Thought Process

Use a map to determine whether each person has already been registered, and use a map to record the number of personnel in each branch and rank.

Set the key value of the map to Pair<string, Pair>, where it stores S, A, and B respectively.

Sample Code-ZeroJudge F634: The Return of Soldiers

#include <iostream>
#include <map>
#include <stdio.h>
using namespace std;

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

int main() {
    int N, M;
    cin >> N >> M;
    map<pair<string, pair<int, int>>, int>person;
    map<int, int>type;
    map<int, int>level;
    double survive = 0;
    for (int i = 0; i<M; i++)
    {
        string name;
        int a, b;
        cin >> name >> a >> b;
        if (person[rtn(name, a, b)] == 0)
        {
            type[a]++;
            level[b]++;
            survive++;
        }
        person[rtn(name, a, b)]++;
    }
    cout << "navy:" << type[1] << " army:" << type[2] << " air:" << type[3] << "\n";
    cout << "officer:" << level[1] << " sergeant:" << level[2] << " soldier:" << level[3] << "\n";
    double total = double(N);
    printf("survival rate: %.1f", survive/total*100);
    cout << "%\n";
}

//ZeroJudge F634
//Dr. SeanXD

Comments