PTA 1012 The Best Rank (25 分)

開始用java寫的,僅僅是答案應該能過全部測試點,但是不可避免地有個測試點超時了hhhhhhhhh
於是找了網上的更多答案,就找了2個結果都是超時的,而且有個超時全軍覆沒,有個僅僅過了一個測試點(1/5),還不如我
…emmmm,我還是用C++寫吧

先貼java代碼

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Scanner;

public class PAT1012 {

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    int n = scanner.nextInt();
    int m = scanner.nextInt();
    HashMap<Integer, Student> map = new HashMap<>();
    ArrayList<Integer> aList = new ArrayList<>();
    ArrayList<Integer> cList = new ArrayList<>();
    ArrayList<Integer> mList = new ArrayList<>();
    ArrayList<Integer> eList = new ArrayList<>();
    for (int i = 0; i < n; i++) {
        int id = scanner.nextInt();
        int cScore = scanner.nextInt();
        int mScore = scanner.nextInt();
        int eScore = scanner.nextInt();
        Student student = new Student();
        student.setC(cScore);
        student.setM(mScore);
        student.setE(eScore);
        student.setA((cScore + mScore + eScore) / 3);
        aList.add(student.getA());
        cList.add(cScore);
        mList.add(mScore);
        eList.add(eScore);
        map.put(id, student);
    }
    Collections.sort(aList);
    Collections.sort(cList);
    Collections.sort(mList);
    Collections.sort(eList);

    ArrayList<Integer> printStudents = new ArrayList<>();
    for (int i = 0; i < m; i++) {
        printStudents.add(scanner.nextInt());
    }
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < m; i++) {
        Integer id = printStudents.get(i);
        Student student = map.get(id);
        if (student == null) {
            sb.append("N/A");
            sb.append("\n");
        } else {
            int aRank = n - aList.lastIndexOf(student.getA());
            int cRank = n - cList.lastIndexOf(student.getC());
            int mRank = n - mList.lastIndexOf(student.getM());
            int eRank = n - eList.lastIndexOf(student.getE());
            int smallest = aRank;
            String name = "A";
            if (smallest > cRank) {
                smallest = cRank;
                name = "C";
            }
            if (smallest > mRank) {
                smallest = mRank;
                name = "M";
            }
            if (smallest > eRank) {
                smallest = eRank;
                name = "E";
            }
            sb.append(smallest);
            sb.append(" ");
            sb.append(name);
            sb.append("\n");
        }
    }
    System.out.println(sb.toString().substring(0, sb.toString().length() - 1));
}

static class Student {
    private Integer A;
    private Integer C;
    private Integer M;
    private Integer E;

    public Integer getA() {
        return A;
    }

    public void setA(Integer a) {
        A = a;
    }

    public Integer getC() {
        return C;
    }

    public void setC(Integer c) {
        C = c;
    }

    public Integer getM() {
        return M;
    }

    public void setM(Integer m) {
        M = m;
    }

    public Integer getE() {
        return E;
    }

    public void setE(Integer e) {
        E = e;
    }
}

}

然後是C++代碼

#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<string.h>
using namespace std;
typedef struct student{
	int id;
	int grade[4];
	int bestGrade;
int bestRank;
}student;
#define INF 0x6FFFFFFF
char charTable[] = {'A','C','M','E'};
int main()
{
int n,m;
cin>>n>>m;
vector<int> aList;
vector<int> cList;
vector<int> mList;
vector<int> eList;
map<int,int> stuMap;
vector<vector<int>> vectorList;
vector<student> students;
for(int i = 0;i<n;i++){
	int id,cScore,mScore,eScore;
	cin>>id>>cScore>>mScore>>eScore;
	student st;
	st.grade[0]= (cScore + mScore + eScore + 1)/3;
	aList.push_back((cScore + mScore + eScore + 1)/3);
	st.grade[1]= cScore;
	cList.push_back(cScore);
	st.grade[2]= mScore;
	mList.push_back(mScore);
	st.grade[3]= eScore;
	eList.push_back(eScore);
	st.id = id;
	st.bestRank = INF;
	stuMap[id]=i;
	students.push_back(st);
}

sort(aList.begin(),aList.end());
reverse(aList.begin(),aList.end());
vectorList.push_back(aList);

sort(cList.begin(),cList.end());
reverse(cList.begin(),cList.end());
vectorList.push_back(cList);

sort(mList.begin(),mList.end());
reverse(mList.begin(),mList.end());
vectorList.push_back(mList);

sort(eList.begin(),eList.end());
reverse(eList.begin(),eList.end());
vectorList.push_back(eList);
for(int i =0;i<n;i++){
	for(int j=0;j<4;j++){
		int score = students[i].grade[j];
		for(int k =0;k<n;k++){
			if(vectorList[j][k] == score){
				if(students[i].bestRank > k){
					students[i].bestRank = k;
					students[i].bestGrade = j; 
				}
				break;
			}
		}
	}
}
for(int i = 0;i<m;i++){
	int id;
	cin>>id;
	map<int,int>::iterator  it = stuMap.find(id);

	if(it != stuMap.end()){
		int idx = it->second;
		cout<<students[idx].bestRank + 1<<" "<<charTable[students[idx].bestGrade]<<endl;
	}else{
		cout<<"N/A"<<endl;
	}
}

}

雖然我C++比較菜,但是還是全部通過測試點了.
刷題 GOOD_JAVA < NORMAL_C++

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章