首页 > 试题广场 >

PAT Ranking (25)

[编程题]PAT Ranking (25)
  • 热度指数:3425 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
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.

输入描述:
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.


输出描述:
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.
示例1

输入

2<br/>5<br/>1234567890001 95<br/>1234567890005 100<br/>1234567890003 95<br/>1234567890002 77<br/>1234567890004 85<br/>4<br/>1234567890013 65<br/>1234567890011 25<br/>1234567890014 100<br/>1234567890012 85

输出

9<br/>1234567890005 1 1 1<br/>1234567890014 1 2 1<br/>1234567890001 3 1 2<br/>1234567890003 3 1 2<br/>1234567890004 5 1 4<br/>1234567890012 5 2 2<br/>1234567890002 7 1 5<br/>1234567890013 8 2 3<br/>1234567890011 9 2 4
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class Student
{
public:
    string id;                //准考证号
    int score;                //分数
    int location_number;    //考场号
    int local_rank;            //班级排名
    int rank;                //总排名
};

bool compare(Student stu1, Student stu2)
{
    if (stu1.score == stu2.score)
        return stu1.id < stu2.id;
    return stu1.score > stu2.score;
}

vector<Student>stu; //进行班级排名
vector<Student>stu2;//进行总排名

int main()
{
    int n;            //考场数
    int num;        //学生数
    string id;
    int score;
    cin >> n;

    for (int i = 1; i <= n; i++)
    {
        cin >> num;
        for (int j = 1; j <= num; j++)
        {
            cin >> id >> score;
            Student s;
            s.id = id;
            s.score = score;
            s.location_number = i;
            stu.push_back(s);
        }

        //计算班级排名
        sort(stu.begin(), stu.end(), compare);
        stu[0].local_rank = 1;
        for (int j = 1; j < stu.size(); j++)
        {
            if (stu[j].score == stu[j - 1].score)
                stu[j].local_rank = stu[j - 1].local_rank;
            else
                stu[j].local_rank = j + 1;
        }
        for (auto e : stu)
            stu2.push_back(e);
        stu.clear();
    }

    //计算总排名
    sort(stu2.begin(), stu2.end(), compare);
    stu2[0].rank = 1;
    for (int j = 1; j < stu2.size(); j++)
    {
        if (stu2[j].score == stu2[j - 1].score)
            stu2[j].rank = stu2[j - 1].rank;
        else
            stu2[j].rank = j + 1;
    }

    //输出
    cout << stu2.size() << endl;
    for (int i = 0; i < stu2.size(); i++)
    {
        cout << stu2[i].id << " " << stu2[i].rank << " " << stu2[i].location_number <<
            " " << stu2[i].local_rank << endl;
    }
}


发表于 2020-09-26 20:08:23 回复(0)
更多回答
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct Student{
    char ID[15];  
    int grade;
    int location_number;   
    int local_rank;       
}stu[30005];   
              
bool cmp(Student a, Student b){
    if(a.grade != b.grade) return a.grade > b.grade;
    else return strcmp(a.ID, b.ID) < 0;
}
int main(void){
    int num = 0;    
    int n;           
    scanf("%d", &n);
    for(int i = 1; i <= n; i++){ 
        int k;   
        scanf("%d", &k); 
        for(int j = 1; j <= k; j++){
            scanf("%s %d", stu[num].ID, &stu[num].grade);
            stu[num].location_number = i;    
            num++;
        }
        sort(stu + num - k, stu + num, cmp);   
        /****** ×éÄÚÅÅÃû ******/
        stu[num - k].local_rank = 1; 
        for(int p = 1; p < k; p++){       
            if(stu[num - k + p].grade == stu[num - k + p - 1].grade) 
            stu[num - k + p].local_rank = stu[num - k + p - 1].local_rank;
            else stu[num - k + p].local_rank = p + 1;
        }        
        
    }
    printf("%d\n", num); 
    
    sort(stu, stu + num, cmp); 
    int Rank = 1;
    for(int i = 0; i < num; i++){
        if(i > 0 && stu[i].grade != stu[i - 1].grade){
            Rank = i + 1;
        }
        printf("%s %d %d %d\n", stu[i].ID, Rank, stu[i].location_number, stu[i].local_rank);
    }
    
    return 0;
}
发表于 2018-01-29 23:17:56 回复(0)
#include<iostream>
#include<vector>
#include<algorithm>
#include <cstring>

using namespace std;

typedef struct{
    int score;
    char ID[15];
    int final_rank ;
    int location_number;
    int local_rank;
}V; 
vector<V> all;                                //存放全体的信息(输出)
vector<V> tmp;                                //存放每组数据 
bool GreaterSort(V a,V b){
    if(a.score != b.score) 
        return a.score > b.score;
    else 
        return strcmp(a.ID, b.ID) < 0;
}
int main(){
    int N,i,j,k,ni,NUM=0;
    int score;
    char ID[15];                                
    cin>>N;    
    for(i=1; i<=N; ++i){                    //对每组数据进行处理 
        cin>>ni;
        NUM += ni;
        all.resize(NUM);
        tmp.resize(ni); 
        for(j=0; j<ni; ++j){
            cin>>tmp[j].ID>>tmp[j].score;
            tmp[j].location_number = i;
        }
        sort(tmp.begin(),tmp.end(),GreaterSort);    //降序排列 
        tmp[0].local_rank = 1;
        
        for(k=1; k<ni; k++)
        {
            if(tmp[k].score == tmp[k-1].score)
                tmp[k].local_rank = tmp[k-1].local_rank;
            else
                tmp[k].local_rank =k+1;
        }
        
        //将每组数据放入all
        for(j=NUM-ni,k=0;j<NUM;j++,k++)
            all[j] = tmp[k] ;
            
    }
    sort(all.begin(),all.end(),GreaterSort);
    all[0].final_rank = 1;
    for(k=1; k<NUM; k++)
        {
            if(all[k].score == all[k-1].score) 
                all[k].final_rank = all[k-1].final_rank;
            else
                all[k].final_rank = k+1;
        }
    
    //输出
    cout<<NUM<<endl;
    for(i=0; i<NUM; i++)
        cout<<all[i].ID<<" "<<all[i].final_rank<<" "<<all[i].location_number<<" "<<all[i].local_rank<<endl;

    return 0;
}
发表于 2019-05-07 20:51:41 回复(0)
#include <bits/stdc++.h>
using namespace std;
const int N = 3e4+10;
struct Rank{
    char id[15];
    int score;
    int num;//小组编号
    int numRank;//组内排名
    bool operator < (const Rank &b) const{
        if(score!=b.score) return score > b.score;
        return strcmp(id,b.id)<0;
    }
}ranks[N];
int main(){
    int T,n=0,m,num = 0;
    scanf("%d",&T);
    while(T--){
        num++;//小组编号哦
        scanf("%d",&m);
        for(int i = n; i < n+m; i++){
            ranks[i].num = num;
            scanf("%s%d",ranks[i].id,&ranks[i].score);
        }
        sort(ranks+n,ranks+n+m);
        for(int i = n; i < n+m; i++){
            if(i==n||ranks[i].score!=ranks[i-1].score)ranks[i].numRank = i-n+1;
            else ranks[i].numRank = ranks[i-1].numRank;
        }
        n +=m;
    }
    printf("%d\n",n);
    sort(ranks,ranks+n);
    int finalRansk;
    for(int i = 0; i < n; i++){
        if(i==0||ranks[i].score!=ranks[i-1].score) finalRansk = i+1;
        printf("%s %d %d %d\n",ranks[i].id,finalRansk,ranks[i].num,ranks[i].numRank);
    }
    return 0;
}

发表于 2019-04-18 21:26:25 回复(0)
//首先对每个局部求排名,只要控制sort的范围就可以了
//然后对整体求排名
#include<bits/stdc++.h>
using namespace std;

struct Stu{
    string id;
    int grade,frank,lnum,lrank;
};
Stu stu[30010];
bool com(Stu s1,Stu s2){
    if(s1.grade!=s2.grade) return s1.grade>s2.grade;
    else return s1.id < s2.id; 
}

int main(){
    int n,k,i,j,start,num=0;
    cin>>n;
    for(i=1;i<=n;i++){
        start=num;
        cin>>k;
        for(j=1;j<=k;j++){
            cin>>stu[num].id>>stu[num].grade;
            stu[num++].lnum=i;
        }
        sort(stu+start,stu+num,com);
        stu[start].lrank=1;
        for(j=1;j<k;j++){
            if(stu[start+j].grade == stu[start+j-1].grade) stu[start+j].lrank=stu[start+j-1].lrank;
            else stu[start+j].lrank=j+1;
        }    
    }
    sort(stu,stu+num,com);
    stu[0].frank=1;
    for(i=1;i<num;i++){
        if(stu[i].grade==stu[i-1].grade) stu[i].frank=stu[i-1].frank;
        else stu[i].frank=i+1;
    }
    cout<<num<<endl;
    for(i=0;i<num;i++) cout<<stu[i].id<<" "<<stu[i].frank<<" "<<stu[i].lnum<<" "<<stu[i].lrank<<endl;
    return 0;
}

编辑于 2019-02-08 18:45:02 回复(0)
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

struct student{
    char id[15];
    int score;
    int lrank,location;
}s[30010];
bool cmp(student a,student b){
    if(a.score!=b.score) return a.score>b.score;
    else return strcmp(a.id,b.id)<0;
}
int main(){
    int x,y,m,n=0;
    cin>>x;
    for(int i=1;i<=x;i++){
        cin>>y;
        m=n;
        for(int j=0;j<y;j++){
            cin>>s[n].id>>s[n].score;
            s[n++].location=i;
        }
        sort(s+m,s+n,cmp);
        s[m].lrank=1;
        for(int j=m+1;j<n;j++){
            if(s[j].score==s[j-1].score)
                s[j].lrank=s[j-1].lrank;
            else s[j].lrank=j-m+1;
        }
    }
    sort(s,s+n,cmp);
    cout<<n<<'\n';
    int r=1;
    for(int i=0;i<n;i++){
        if(i>0&&s[i].score!=s[i-1].score)
            r=i+1;
        cout<<s[i].id<<" "<<r<<" "<<s[i].location<<" "<<s[i].lrank<<'\n';
    }     
    return 0;
}

发表于 2018-01-22 15:16:58 回复(0)
想不通为什么我这个有错误?
#include <iostream>

#include <cstdio>

#include <cstdio>

#include <string>

#include <set>

#include <vector>

#include <deque>

#include <stack>

#include <map>

#include <algorithm>

#include <cmath>

using namespace std ;

struct PATtest{

    int location_num;

    string registration_number;

    int score;

    int local_rank;

    int all_rank;

};

vector < PATtest > AllStu;

bool ComScore( const PATtest &A, const PATtest &B){

    return A. score > B. score ;

}

bool Comp( const PATtest &A, const PATtest &B){

    if (A. score > B. score ) {

        return true ;

    }

    if (A. score ==B. score && A. registration_number <B. registration_number ) {

        return true ;

    }

    return false ;

}

int main(){

    int N;

    cin >> N;

    

    int *location_number;

    location_number = new int (N);

    int total_number = 0 ;

    for ( int i= 0 ; i < N; ++i) {

        cin >> location_number[i];

        total_number += location_number[i];

        PATtest student;

        vector < PATtest > LocalStu;

        student. location_num = i+ 1 ;

        for ( int j= 0 ; j < location_number[i]; ++j) {

            cin >> student. registration_number >> student. score ;

            LocalStu. push_back (student);

        }

        sort (LocalStu. begin (), LocalStu. end (), ComScore );

        for ( int k= 0 ; k < LocalStu. size (); ++k) {

            LocalStu[ k ]. local_rank =k+ 1 ;

        }

        for ( int k= 1 ; k < LocalStu. size (); ++k) {

            if (LocalStu[ k ]. score == LocalStu[ k - 1 ]. score ) {

                LocalStu[ k ]. local_rank = LocalStu[ k - 1 ]. local_rank ;

            }

        }

        AllStu . insert ( AllStu . end (), LocalStu. begin (), LocalStu. end ());

    }

    sort ( AllStu . begin (), AllStu . end (), Comp );

    for ( int p= 0 ; p < AllStu . size (); ++p) {

        AllStu [ p ]. all_rank =p+ 1 ;

    }

    for ( int p= 1 ; p < AllStu . size (); ++p) {

        if ( AllStu [ p ]. score == AllStu [ p - 1 ]. score ) {

            AllStu [ p ]. all_rank = AllStu [ p - 1 ]. all_rank ;

        }

    }

    

    cout << total_number << endl ;

    for ( auto it = AllStu . begin (); it != AllStu . end (); ++it) {

        cout << it-> registration_number << ' ' << it-> all_rank << ' ' << it-> location_num << ' ' << it-> local_rank << endl ;

    }

    return 0 ;

}


发表于 2016-08-16 14:30:34 回复(2)
我的只需要一次排序就能完成
importjava.util.*;
classGrade
{
    publicString id;
    publicintn;
    publicintscore;
    publicintl_r;
    publicintr;
    Grade(String id,intn,intscore)
    {
        this.id=id;
        this.n=n;
        this.score=score;
    }
}
publicclassMain {
 
    publicstaticvoidmain(String[] args) {
        // TODO Auto-generated method stub
       Scanner scan=newScanner(System.in);
       intnt=scan.nextInt();
       LinkedList<Grade> list=newLinkedList<Grade>();
       for(inti=1;i<=nt;i++)
       {
           intntt=scan.nextInt();
           for(intj=1;j<=ntt;j++)
           {
               String tempId=scan.next();
               inttempS=scan.nextInt();
               list.add(newGrade(tempId,i,tempS));
           }
       }
       Collections.sort(list,newComparator<Grade>(){
           publicintcompare(Grade arg0, Grade arg1) {
               if(arg1.score==arg0.score)
                   returnarg0.id.compareTo(arg1.id);
               returnarg1.score-arg0.score;
           }
       });
       int[] tempScore=newint[nt];
       int[] tempR=newint[nt];
       int[] tempRR=newint[nt];
       ints=-1;
       intr=0;intrr=0;
       Iterator<Grade> it=list.iterator();
       System.out.println(list.size());
       while(it.hasNext())
       {
         Grade g=it.next();
         if(g.score!=tempScore[g.n-1])
         {
            tempRR[g.n-1]++;
            tempR[g.n-1]=tempRR[g.n-1];
            g.l_r=tempR[g.n-1];
            tempScore[g.n-1]=g.score;
         }
         else{
             tempRR[g.n-1]++;
             g.l_r=tempR[g.n-1];
         }
         if(g.score!=s)
         {
             rr++;
             r=rr;
             g.r=r;
             s=g.score;
         }
         else{
             rr++;
             g.r=r;
         }
         System.out.println(g.id+" "+g.r+" "+g.n+" "+g.l_r);
       }
    }
}

发表于 2015-12-23 16:36:23 回复(0)
#include<bits/stdc++.h>
using namespace std;

struct student {
	string id;
	int score;
	int r;
	int r1;
	int site;
	bool operator < (const student & a) {
		if(score!=a.score) {
			return score>a.score;
		} else {
			return id.compare(a.id)<0;
		}
	}
};

int main() {
	int n,num=0;
	while(cin>>n) {
		student s[30010];
		for(int i=1; i<=n; i++) {
			int k;
			cin>>k;
			for(int j=0; j<k; j++) {
				cin>>s[num].id>>s[num].score;
				s[num].site=i;
				num++;
			}
			sort(s+num-k,s+num);
			s[num-k].r1=1;
			for(int i=num-k+1; i<num; i++) {
				if(s[i].score==s[i-1].score) {
					s[i].r1=s[i-1].r1;
				} else {
					s[i].r1=(i-(num-k))+1;
				}
			}
		}
		sort(s,s+num);
		s[0].r=1;
		for(int i=1; i<num; i++) {
			if(s[i].score==s[i-1].score) {
				s[i].r=s[i-1].r;
			} else {
				s[i].r=i+1;
			}
		}
		cout<<num<<endl;
		for(int i=0;i<num;i++){
			cout<<s[i].id<<" "<<s[i].r<<" "<<s[i].site<<" "<<s[i].r1<<endl;
		}
	}
	return 0;
}

发表于 2022-11-08 22:51:43 回复(0)
请问各位大神,为什么我这个代码在pat的测试点4会答案错误?
#include<stdio.h>
#include<algorithm>
using namespace std;

struct student{
    long long regnum;
    int grade,location,localnum;
}a[40000];

bool cmp(struct student x,struct student y){
    if(x.grade!=y.grade){
        return x.grade>y.grade;
    }else{
        return x.regnum<y.regnum;
    }
}

int main(){
    int n,i,j,k,r,s=0;
    scanf("%d",&n);
    for(i=1;i<=n;i++){
        scanf("%d",&k);
        for(j=1;j<=k;j++){
            scanf("%lld%d",&a[s+j].regnum,&a[s+j].grade);
            a[s+j].location=i;
        }
        sort(a+s+1,a+s+k+1,cmp);
        r=1;
        for(j=1;j<=k;j++){
            if(j!=1&&a[j+s].grade!=a[j+s-1].grade){
                r=j;
            }
            a[j+s].localnum=r;
        }
        s+=k;
    }
    if(s==0){
        printf("0");
        return 0;
    }
    sort(a+1,a+s+1,cmp);
    printf("%d\n",s);
    r=1;
    for(i=1;i<=s;i++){
        if(i!=1&&a[i].grade!=a[i-1].grade){
            r=i;
        }
        printf("%lld %d %d %d\n",a[i].regnum,r,a[i].location,a[i].localnum);
    }
    return 0;
}
发表于 2022-01-28 14:37:39 回复(0)
排序!排序!排序时不要忘了并列!并列!
#include<iostream>
#include<string>
#include<map>
#include<vector>
#include<algorithm>

using namespace std;

struct stu {
    string id;
    int score;
    int location_number;
    int local_rank;
    int final_rank;
};

bool cmp(stu s1, stu s2) {
    if(s1.score > s2.score) {
        return true;
    } else if(s1.score == s2.score) {
        return s1.id < s2.id;
    }
    return false;
}

vector<stu> v[500];
vector<stu> total;

int main() {
    int N;
    cin >> N;
    for (int location_number = 1; location_number <= N; ++location_number) {
        int K;
        cin >> K;
        for (int i = 0; i < K; ++i) {
            stu s;
            cin >> s.id >> s.score;
            s.location_number = location_number;
            v[location_number].push_back(s);
        }
        sort(v[location_number].begin(), v[location_number].end(), cmp);
        for (int j = 0; j < K; ++j) {
            // 在排序时 不要忘记并列问题
            if(j > 0 && v[location_number][j].score == v[location_number][j-1].score) {
                v[location_number][j].local_rank = v[location_number][j-1].local_rank;
            } else {
                v[location_number][j].local_rank = j + 1;
            }
            total.push_back(v[location_number][j]);
        }
    }
    sort(total.begin(), total.end(), cmp);
    cout << total.size() << endl;
    for (int i = 0; i < total.size(); i++) {
        // 在排序时 不要忘记并列问题
        if(i > 0 && total[i].score == total[i-1].score) {
            total[i].final_rank = total[i-1].final_rank;
        } else {
            total[i].final_rank = i + 1;
        }
        cout << total[i].id << ' ' << total[i].final_rank << ' ' << total[i].location_number << ' ' << total[i].local_rank << endl;
    }
    return 0;
}


发表于 2020-09-19 10:59:57 回复(0)
#include<bits/stdc++.h>
using namespace std;
struct student{
	string id;
	int score;
	int location;
	int inner;
	int total;
};

bool cmp(student a,student b){
	if(a.score==b.score){
		return a.id<b.id;
	}else{
		return a.score>b.score;
	}
}

vector<student> res;
vector<student> sum;
int main(){
	int c=0;
	int t;
	cin>>t;
	for(int i=1;i<=t;i++){
		int count;
		cin>>count;
		while(count--){
			student temp;
			cin>>temp.id>>temp.score;
			c++;
			temp.location=i;
			res.push_back(temp);
//			sum.push_back(temp);
		}
		sort(res.begin(),res.end(),cmp);
		int number=1;
		for(int j=0;j<res.size();j++){
//			student temp=res[j];
			if(j==0){
				res[j].inner=1;
				sum.push_back(res[j]); 
				number++;
				continue;
//				break;
			}else if(res[j].score==res[j-1].score){
				res[j].inner=res[j-1].inner;
				sum.push_back(res[j]); 

				number++;
				
			}else{
				res[j].inner=number;
				sum.push_back(res[j]); 
				number++;
			}
		}
		res.clear();	
	}
	sort(sum.begin(),sum.end(),cmp);
	  int number=1;
			for(int j=0;j<sum.size();j++){
			student temp=sum[j];
			if(j==0){
				sum[j].total=1;
				number++;
//				break;
			}else if(sum[j].score==sum[j-1].score){
				sum[j].total=sum[j-1].total;
				number++;
			}else{
				sum[j].total=number;
				number++;
			}
			
		}
	cout<<c<<endl;
	for(int i=0;i<sum.size();i++){
		student temp=sum[i];
		cout<<temp.id<<" "<<temp.total<<" "<<temp.location<<" "<<temp.inner<<endl;
	}
}
幼儿园级别的代码水平,中间写的有点繁琐,用了两个vector
发表于 2020-03-17 21:03:49 回复(0)
pta上最后一个用例可能超时
def main():
    m,s = [],1
    for i in range(int(input())):
        p,q,r = 1,0,0
        for j in sorted([input().split()for j in range(int(input()))],\
                        key = lambda x:(-int(x[1]),x[0])):
            q = q + 1 if r == j[1] else 0
            m.append(j + [s,p - q])
            p,r = p + 1,j[1]
        s += 1
        
    print(len(m))
    p,q,r = 1,0,0
    for i in sorted(m,key = lambda x:(-int(x[1]),x[0])):
        q = q + 1 if r == i[1] else 0
        print(i[0],p - q,i[2],i[3])
        p,r = p + 1,i[1]
main()

编辑于 2020-03-01 12:00:03 回复(0)
补充一点,PAT最后一个测试用例如果ID用的是long long,注意补前导0。用string就天然避坑了。
#include<iostream>
#include<algorithm>
using namespace std;
struct Stu {
	string rnum;
	int frank,lnum,lrank,score;
} s[30010];
bool cmp1(Stu s1,Stu s2) {
	if(s1.score!=s2.score)return s1.score>s2.score;
	else return s1.rnum<s2.rnum;
}

int main() {
	int N,K,num=0;
	cin>>N;
	for(int i=0; i<N; i++) {
		cin>>K;
//		vector<Stu> s(K);
		for(int j=0; j<K; j++) {
			cin>>s[num].rnum>>s[num].score;
			s[num].lnum=i+1;
			num++;
		}
		sort(s+num-K,s+num,cmp1);
		s[num-K].lrank=1;
		for(int m=num-K+1; m<num; m++) {
			s[m].lrank=s[m].score==s[m-1].score?s[m-1].lrank:m+1+K-num;
		}
	}
	sort(s,s+num,cmp1);
	s[0].frank=1;
	for(int m=1; m<num; m++) {
		s[m].frank=s[m].score==s[m-1].score?s[m-1].frank:m+1;
	}
	cout<<num<<endl;
	for(int k=0;k<num;k++){
		cout<<s[k].rnum<<" "<<s[k].frank<<" "<<s[k].lnum<<" "<<s[k].lrank<<endl;
	}
	return 0;
}

发表于 2020-02-06 16:33:57 回复(0)
求助各位大佬,这个题理论来说不难,但是为啥我的代码本地跑的没问题,牛客也完全通过,但是PAT官网就是倒数第二个case就是过不去
牛客测评:
BUT PAT官网测评:

#include<bits/stdc++.h>
using namespace std;
struct Student{
    char id[15];
    int grade;
    int fr;
    int lnum;
    int lr;
};
bool cmp(Student s1,Student s2){
    if(s1.grade!=s2.grade)return s1.grade>s2.grade;
    else{
        return strcmp(s1.id,s2.id)<0;
    }
}
struct Student s[30010];
int main(){
    int n;
    while(~scanf("%d",&n)){
        int index=0;
        for(int i=0;i<n;i++){
            int k;
            scanf("%d",&k);
            int start=index;
            for(int j=start;j<start+k;j++){
                scanf("%s %d",s[j].id,&s[j].grade);
                s[j].lnum=i+1;
                index++;
            }
            sort(s+start,s+k+start,cmp);
            s[start].lr=1;
            for(int i=start+1;i<start+k;i++){
                if(s[i-1].grade==s[i].grade){
                    s[i].lr=s[i-1].lr;
                }else{
                    s[i].lr=i-start+1;
                }
            }
        }
        sort(s,s+index,cmp);
        s[0].fr=1;
        for(int i=0;i<index;i++){
            if(s[i-1].grade==s[i].grade){
                s[i].fr=s[i-1].fr;
            }else{
                s[i].fr=i+1;
            }
        }
        printf("%d\n",index);
        for(int j=0;j<index;j++){
            printf("%s %d %d %d\n",s[j].id,s[j].fr,s[j].lnum,s[j].lr);
        }
    }
    return 0;
}


发表于 2020-02-05 11:51:41 回复(0)

思路比较简单,就是先对局部排序,对局部排名赋值,然后对整体进行赋值

#include<iostream>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<string>
#define N 30010
using namespace std;

struct testee {
    string id;
    int totalR, loca, locaR, s;
    testee(string id,int tR, int lo, int lR, int s) :id(id),totalR(tR), loca(lo), locaR(lR), s(s) {}
};
bool com(testee a, testee b) {
    if(a.s!=b.s)return a.s > b.s;
    else if (a.id != b.id)return a.id < b.id;
}
int m = 0;
int main() {
    vector<testee> peo;
    int n = 0, l[310] = {0};
    string tem;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> l[i];
        m += l[i];
        for (int j = 0,s = 0; j < l[i]; j++) {
            cin >> tem >> s;
            peo.push_back(testee(tem, 0, i, 0, s));
        }
    }
    int start = 0, end = 0;
    for (int i = 1; i <= n; i++) {//把每段排序,然后进行排序
        end = start+l[i];
        sort(peo.begin()+start, peo.begin()+ end, com);

        peo[start].locaR = 1;//第一个区排名为1
        for (int j = start; j < end; j++) {
            if (j != start && peo[j].s == peo[j - 1].s)peo[j].locaR = peo[j - 1].locaR;
             else peo[j].locaR = j - start + 1;//相对于第一名的排名
        }
        start = end;
    }
    sort(peo.begin(), peo.end(), com);
    peo[0].totalR = 1;
    for (int i = 0; i < m; i++) {
        if (i != 0 && peo[i].s == peo[i - 1].s)peo[i].totalR = peo[i - 1].totalR;
        else peo[i].totalR = i + 1;//相对于第一名的排名
    }
    cout << m<<endl;
    for (int i = 0; i < m; i++) {
        cout << peo[i].id << " " << peo[i].totalR << " " << peo[i].loca << " " << peo[i].locaR << endl;
    }
    system("pause");
    return 0;
}
发表于 2019-10-07 20:46:30 回复(0)
pat和牛客都过了
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;


//1025 PAT Ranking
//这个题的关键在于,同分同排名,但是占名额,即两个人第一名,那么第三个人是第三名。
//坑2在于同分时id小的在前,pat.4

class node {
public:
    string id;
    int score;
    int location;
    int localrank;
    int finalrank;
};


bool mycomp(const node &a, const node&b) {
    if (a.score != b.score)
        return a.score > b.score;
    else
        return a.id < b.id;
}

int main() {
    int n;//n<=100
    int k, score, cnt = 0, location = 0, localrank;
    string id;
    vector<node> all, v;
    vector<node>::iterator it, last;

    cin >> n;
    for (int j = 0; j < n; ++j) {
        cin >> k;
        cnt += k;
        location++;//区域编号从1开始
        v.clear();

        for (int i = 0; i < k; ++i) {
            cin >> id >> score;
            v.push_back({ id,score,location });
        }

        //区域排名
        sort(v.begin(), v.end(), mycomp);
        localrank = 0;
        v[0].localrank = 1;
        for (it = last = v.begin(); it != v.end(); ++it) {
            localrank++;
            if (it->score == last->score) {
                it->localrank = last->localrank;
            }
            else {
                it->localrank = localrank;
            }
            last = it;
        }
        //合并
        all.insert(all.end(), v.begin(), v.end());

    }

    //输出总人数    
    cout << cnt << endl;

    //总排名
    sort(all.begin(), all.end(), mycomp);
    int finalrank = 0;
    all[0].finalrank = 1;
    for (it = last = all.begin(); it != all.end(); ++it) {
        finalrank++;
        if (it->score == last->score) {
            it->finalrank = last->finalrank;
        }
        else {
            it->finalrank = finalrank;
        }
        last = it;

        //输出
        cout << it->id << " " << it->finalrank << " " << it->location << " " << it->localrank << endl;
    }

    return 0;
}

发表于 2019-01-29 01:08:41 回复(0)
n = input()
n = int(n)
lst = []
for i in range(0,n):
    t = []
    tem = input()
    tem = int(tem)
    for j in range(0,tem):
        temp = list(map(str,input().split()))
        t.append([temp[0],int(temp[1]),str(i+1)])
    t.sort(key = lambda x:(-x[1],x[0]))
    con,con1= 1,1
    t[0].append(str(1))
    for i in range(0,len(t)-1):
        if t[i][1]!=t[i+1][1]:
            con+=con1
            con1 = 1
            t[i+1].append(str(con))
        else:
            con1+=1
            t[i+1].append(str(con))
    lst.extend(t)
lst.sort(key = lambda x:(-x[1],x[0]))
con,con1= 1,1
lst[0].append(str(1))
for i in range(0,len(lst)-1):
    if lst[i][1]!=lst[i+1][1]:
        con+=con1
        con1 = 1
        lst[i+1].append(str(con))
    else:
        con1+=1
        lst[i+1].append(str(con))
print(len(lst))
for i in lst:
    print(i[0]+" "+i[4]+" "+i[2]+" "+i[3])

发表于 2018-12-06 14:32:18 回复(0)
注意点:
1.分数相同的话排名也相同。
2.sort的CMP函数写技巧,与局部排序。
思路:
局部排序,然后整体排序。
问题中文概要:
就是有N堆鬼排名,每堆鬼排名先局部有序,然后整体排名。
说实话,理论上上这题应该用归并排序,然自己水平太渣,还是用了快排。
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
//registration_number final_rank location_number local_rank
struct student
{
    string ID;
    int score;
    int localRank;
    int finalRank;
    int group;
};

int Cmp(struct student & a, struct student & b)
{
    if(a.score != b.score)
        return a.score > b.score;
    else 
        return strcmp(a.ID.c_str(), b.ID.c_str()) < 0;
}

int MyLocalRank(vector<struct student> & va, int first, int end)
{
    
    sort(va.begin()+first, va.begin()+end, Cmp);
    for(int i=first,j = 1,t = 0; i<end; i++,j++)
    {
        if(i>first && va[i].score == va[i-1].score)
            t++;
        else
            t = 0;
        //cout << va[i].score << " ";    
        va[i].localRank = j - t; 
    }
    return 1; 
} 

int main(int argc, char** argv) {
    int N,K;
    cin >> N;
    vector<struct student> myStudents;
    int count = 0;
    int group = 1;
    for(int i=0; i<N; i++)
    {
        cin >> K;
        string tempID;
        int oldCount = count;
        for(int j=0; j<K; j++)
        {
            struct student *t = new struct student;
            cin >> t->ID;
            cin >> t->score;
            t->group = group;
            myStudents.push_back(*t);
            count ++;
        }
        group++;
        MyLocalRank(myStudents, oldCount, count);
    }
    sort(myStudents.begin(), myStudents.end(), Cmp);
    cout << myStudents.size() << endl;
    for(int i=0,j=0; i<myStudents.size(); i++)
    {
        if(i>0 && myStudents[i].score == myStudents[i-1].score)
        {
            j++;
        }
        else
        {
            j = 0;
        }
        cout << myStudents[i].ID << " " << i+1 - j << " " << myStudents[i].group << " " << myStudents[i].localRank << endl;
    }    
    return 0;
}

发表于 2018-08-01 16:06:27 回复(0)

问题信息

难度:
26条回答 5580浏览

热门推荐

通过挑战的用户