⬆︎
×

[PAT-A] 1137 Final Grading

Hyplus目录

Java

测试点3超时

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;

        st = new StringTokenizer(br.readLine());
        int p = Integer.parseInt(st.nextToken());
        int m = Integer.parseInt(st.nextToken());
        int n = Integer.parseInt(st.nextToken());

        HashMap<String, Student> map = new HashMap<>();
        List<Student> list = new ArrayList<>();

        for (int i = 0; i < p; i++) {
            st = new StringTokenizer(br.readLine());
            String id = st.nextToken();
            int score = Integer.parseInt(st.nextToken());
            map.putIfAbsent(id, new Student());
            map.get(id).id = id;
            map.get(id).gProgram = score;
        }

        for (int i = 0; i < m; i++) {
            st = new StringTokenizer(br.readLine());
            String id = st.nextToken();
            int score = Integer.parseInt(st.nextToken());
            map.putIfAbsent(id, new Student());
            map.get(id).id = id;
            map.get(id).gMid = score;
            map.get(id).hasMid = true;
        }

        for (int i = 0; i < n; i++) {
            st = new StringTokenizer(br.readLine());
            String id = st.nextToken();
            int score = Integer.parseInt(st.nextToken());
            map.putIfAbsent(id, new Student());
            map.get(id).id = id;
            map.get(id).gFinal = score;
            map.get(id).hasFinal = true;
        }

        for (Student stu : map.values()) {
            if (stu.gProgram >= 200) {
                if (stu.gMid > stu.gFinal) {
                    stu.gTotal = stu.gMid * 0.4 + stu.gFinal * 0.6 + 0.5;
                } else {
                    stu.gTotal = stu.gFinal;
                }

                if (stu.gTotal >= 60) {
                    list.add(stu);
                }
            }
        }

        Collections.sort(list);

        for (Student stu : list) {
            System.out.printf("%s %d %d %d %d\n", stu.id, stu.gProgram, stu.gMid, stu.gFinal, (int) stu.gTotal);
        }

        br.close();
    }
}

class Student implements Comparable<Student> {
    String id;
    int gProgram;
    int gMid = -1;
    int gFinal = -1;
    double gTotal;
    boolean hasMid = false;
    boolean hasFinal = false;

    @Override
    public int compareTo(Student t) {
        if ((int) gTotal != (int) t.gTotal) {
            return (int) t.gTotal - (int) gTotal;
        } else {
            return id.compareTo(t.id);
        }
    }
}

C++

#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#include <vector>

using namespace std;

const int N = 10010;

int p, m, n;

struct Student {
    string id;
    int Gp;
    int Gmid, Gfinal;
    double Gtotal;
    bool has_Gmid, has_Gfinal;

    bool operator<(const Student &t) const {
        if ((int) Gtotal != (int) t.Gtotal) return (int) Gtotal > (int) t.Gtotal;
        else return id < t.id;
    }
};

unordered_map <string, Student> mp;
vector <Student> v;

int main() {
    scanf("%d%d%d", &p, &m, &n);

    string id;
    int score;
    for (int i = 0; i < p; ++i) {
        cin >> id >> score;
        mp[id].id = id;
        mp[id].Gp = score;
    }
    for (int i = 0; i < m; ++i) {
        cin >> id >> score;
        mp[id].id = id;
        mp[id].Gmid = score;
        mp[id].has_Gmid = true;
    }
    for (int i = 0; i < n; ++i) {
        cin >> id >> score;
        mp[id].id = id;
        mp[id].Gfinal = score;
        mp[id].has_Gfinal = true;
    }

    for (auto &it: mp) {
        auto &stu = it.second;
        if (stu.Gp >= 200) {
            if (stu.Gmid > stu.Gfinal) stu.Gtotal = stu.Gmid * 0.4 + stu.Gfinal * 0.6 + 0.5;
            else stu.Gtotal = stu.Gfinal;

            if (!stu.has_Gmid) stu.Gmid = -1;
            if (!stu.has_Gfinal) stu.Gfinal = -1;

            if (stu.Gtotal >= 60) v.push_back(stu);
        }
    }

    sort(v.begin(), v.end());
    for (auto &i: v)
        printf("%s %d %d %d %d\n", i.id.c_str(), i.Gp, i.Gmid, i.Gfinal, (int) i.Gtotal);

    return 0;
}

发表评论