1025 PAT Ranking (25 分)
Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several places, and the ranklists will be merged immediately after the test. Now it is your job to write a program to correctly merge all the ranklists and generate the final rank.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive number N (≤100), the number of test locations. Then N ranklists follow, each starts with a line containing a positive integer K (≤300), the number of testees, and then K lines containing the registration number (a 13-digit number) and the total score of each testee. All the numbers in a line are separated by a space.
Output Specification:
For each test case, first print in one line the total number of testees. Then print the final ranklist in the following format:
registration_number final_rank location_number local_rank
The locations are numbered from 1 to N. The output must be sorted in nondecreasing order of the final ranks. The testees with the same score must have the same rank, and the output must be sorted in nondecreasing order of their registration numbers.
Sample Input:
2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85
Sample Output:
9
1234567890005 1 1 1
1234567890014 1 2 1
1234567890001 3 1 2
1234567890003 3 1 2
1234567890004 5 1 4
1234567890012 5 2 2
1234567890002 7 1 5
1234567890013 8 2 3
1234567890011 9 2 4
第一次超时
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Student student[] = new Student[30000];
int num=0;
try{
int n = Integer.parseInt(br.readLine());
for(int i=0;i<n;i++){
//num 为考生数
int k=Integer.parseInt(br.readLine());
for(int j=0;j<k;j++){
student[num]=new Student();
// System.out.println(student[num].id);
String arr= br.readLine();
String arrs[] = arr.split(" ");
student[num].id=new BigInteger(arrs[0]);
student[num].score=Integer.parseInt(arrs[1]);
student[num].location_number=i+1;
num++;
}
//内部排序
int num1=num-k;
sort(student, num1, num);
//内部排名次
student[num-k].local_rank=1;
for(int j=num-k+1;j<num;j++){
if (student[j].score==student[j-1].score){
student[j].local_rank=student[j-1].local_rank;
}else {
student[j].local_rank=j+1-(num-k);
}
}
}
}catch(IOException e){
e.printStackTrace();
}
//全部排序
sort(student,0,num);
System.out.println(num);
int mm=1;
for(int i=0;i<num;i++){
if(i>0&&student[i].score==student[i-1].score){
System.out.println(student[i].id+" "+mm+" "+student[i].location_number+" "+student[i].local_rank);
}else {
mm=i+1;
System.out.println(student[i].id + " " + mm + " " + student[i].location_number + " " + student[i].local_rank);
}
}
}
static void sort(Student[] student,int num1,int num2){
for(int i=num1;i<num2-1;i++){
int ll=0;
//int local_rank=1;
// student[num1].local_rank=local_rank;
for(int j=num1;j<num2-ll-1;j++){
if(student[j].score<student[j+1].score){
swap(student,j);
//student[j].local_rank= student[j+1].local_rank;
//student[j+1].local_rank=local_rank+1;
// local_rank++;
}
if(student[j].score==student[j+1].score){
if(student[j].id==student[j+1].id.max(student[j].id)){
// student[j+1].local_rank=student[j].local_rank;
swap(student,j);
//local_rank+=2;
}
}
}
ll++;
}
}
//交换位置
static void swap(Student[] student,int j){
Student temp[]=new Student[1];
temp[0]=student[j];
student[j]=student[j+1];
student[j+1]=temp[0];
}
public static class Student{
BigInteger id;
int score;
int location_number;
int local_rank;
}
}
第二次超时
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Main {
static class cmp implements Comparator<Student>{
@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
int a=-1;
if(o1.score==o2.score){
if(o1.id==o2.id.max(o1.id)){
a=1;
}
}
if(o1.score>o2.score){
a= -1;
}
if(o1.score<o2.score){
a= 1;
}
return a;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Student student[] = new Student[30000];
int num=0;
try{
int n = Integer.parseInt(br.readLine());
for(int i=0;i<n;i++){
//num 为考生数
int k=Integer.parseInt(br.readLine());
for(int j=0;j<k;j++){
student[num]=new Student();
// System.out.println(student[num].id);
String arr= br.readLine();
String arrs[] = arr.split(" ");
student[num]=new Student();
student[num].id=new BigInteger(arrs[0]);
student[num].score=Integer.parseInt(arrs[1]);
student[num].location_number=i+1;
num++;
}
//内部排序
int num1=num-k;
Arrays.sort(student, num1, num,new cmp());
//内部排名次
student[num-k].local_rank=1;
for(int j=num-k+1;j<num;j++){
if (student[j].score==student[j-1].score){
student[j].local_rank=student[j-1].local_rank;
}else {
student[j].local_rank=j+1-(num-k);
}
}
}
}catch(IOException e){
e.printStackTrace();
}
//全部排序
Arrays.sort(student,0,num,new cmp());
System.out.println(num);
int mm=1;
for(int i=0;i<num;i++){
if(i>0&&student[i].score==student[i-1].score){
System.out.println(student[i].id+" "+mm+" "+student[i].location_number+" "+student[i].local_rank);
}else {
mm=i+1;
System.out.println(student[i].id + " " + mm + " " + student[i].location_number + " " + student[i].local_rank);
}
}
}
public static class Student{
BigInteger id;
int score;
int location_number;
int local_rank;
}
}