开始用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++