PAT A1080Graduate Admission

Posted 进击的alphaCat

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT A1080Graduate Admission相关的知识,希望对你有一定的参考价值。

题目来源

==> 【PAT A1080】Graduate Admission

这道题的录取的思路是这样的:首先对所有考生进行排序和排名,然后对每一个同学遍历他/她的志愿录取。
由于学校录取首先是看招生数,然后如果两名报考考生的排名相同,即使人数会超,也要将他/她们都录取,因此可以在学校的结构体里加入 lastrank变量,记录上一位录取同学的rank。于是每次判断只需要看报考考生的rank是否等于该学校的lastrank,或者学校的人数未满,就可以将考生的id加入到学校的结构体中。(用id,减少数据耦合度)。

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <map>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <cctype>

// cout<<fixed<<setprecision(2); 保留小数
using std::vector;
using std::string;
using std::map;
using std::cin;
using std::cout;
using std::endl;

struct Student

  int id;
  double Ge,Gi,Gf;  
  int rank=1;
  vector<int> choice;  
;

struct School

    int total_num;
    int quota;
    int lastrank=1;
    vector<int> admit;
;

bool cmp(const Student &s1, const Student &s2)

    return s1.Gf != s2.Gf ? s1.Gf > s2.Gf : s1.Ge > s2.Ge; 


int main()

    int N,M,K;
    cin >> N >> M >> K;
    School *school = new School[M];
    for (int i = 0; i != M; ++i) 
        cin >> school[i].quota;
    
    Student *stu = new Student[N];
    for (int i =0; i != N;++i) 
        scanf("%lf %lf",&stu[i].Ge,&stu[i].Gi);
        stu[i].id = i;
        stu[i].Gf = (stu[i].Ge + stu[i].Gi) /2 ;
        for ( int j = 0; j != K; ++j) int choice; scanf("%d",&choice); stu[i].choice.push_back(choice);
    
    
    // process
    	// 排名
    std::sort(stu,stu + N,cmp);
    for ( int i = 1; i != N;++i) 
        if ( stu[i].Gf == stu[i-1].Gf && stu[i].Ge == stu[i-1].Ge ) stu[i].rank = stu[i-1].rank;
        else stu[i].rank = i+1;
    
        // 录取
    for (int i =0; i!=N;++i) 
        for ( int j = 0;j!= K;++j) 
            School &sch = school[ stu[i].choice[j]];
            if ( stu[i].rank == sch.lastrank || sch.total_num < sch.quota ) 
                ++sch.total_num;
                sch.admit.push_back(stu[i].id);
                sch.lastrank = stu[i].rank;
                break;
                
        
    
    
    // output
    for (int i = 0; i != M;++i) 
       bool firstword(true);
       if (school[i].admit.empty()) cout << endl; continue;
       std::sort(school[i].admit.begin(),school[i].admit.end()); 
       for (int j = 0; j != school[i].admit.size(); ++j) 
           if ( firstword) firstword = false;
           else cout << " ";
           cout << school[i].admit[j];
       
       cout << endl;
    
    
    delete [] school;
    delete [] stu;

学校录取的思路参考了柳神的题解,一开始看完题对如何写招生的算法毫无头绪。。。

以上是关于PAT A1080Graduate Admission的主要内容,如果未能解决你的问题,请参考以下文章

PAT A1080Graduate Admission

PAT 甲级 A1080 (2019/02/18)

PAT(A) 1080. Graduate Admission (30)

PAT甲级--Graduate Admission (30)

PAT (Advanced Level) 1080. Graduate Admission (30)

PAT甲级1080 Graduate Admission (30 分)