1080. Graduate Admission (30)

题目地址:http://www.patest.cn/contests/pat-a-practise/1080

#include <cstdio> 
#include <iostream> 
#include <vector> 
#include <set>
#include <algorithm>
#include <stack>
#include <string>
#include <queue>
#include <unordered_map>
#include <iterator>
using namespace std;

#define N 40005

int n , m , kk ;

struct data
{
    int sno ;
    int ge , gi  ,total ;
    int choices[7];
    int rank ;
    int cno ;
    bool isLuqu ;
};

vector<data> students ;

int quota[105];
vector<int> cstu[105];

bool cmp(data d1,data d2)
{
    if(d1.total > d2.total )
        return true;
    if(d1.total == d2.total && d1.ge > d2.ge)
        return true;
    return false;
}

bool cmp2(int x, int y)
{
    return x < y ;
}

int main()
{
    //freopen("in.txt", "r", stdin);
    scanf("%d%d%d",&n , &m , &kk) ;
    int i , j ;
    for(i = 0 ; i < m ; i ++)
    {
        scanf("%d" , &quota[i]);
    }
    students.resize(n);
    for(i = 0 ; i < n ; i ++)
    {
        students[i].sno = i ;
        students[i].isLuqu = false ;
        scanf("%d%d" , &students[i].ge , &students[i].gi);
        students[i].total = students[i].ge + students[i].gi ;
        for(j = 0 ; j < kk ; j++)
        {
            scanf("%d" , &students[i].choices[j]);
        }
    }
    sort(students.begin() , students.end() , cmp);

    unordered_map<int,int> um ; // 学生的sno 在 students数组中的映射
    students[0].rank = 1 ;
    um[students[0].sno]= 0 ;
    for(i = 1 ; i < n ; i ++)
    {
        if(students[i].total == students[i-1].total && students[i].ge == students[i-1].ge)
        {
            students[i].rank = students[i-1].rank ;
        }else{
            students[i].rank = i + 1 ;
        }
        um[students[i].sno]= i ;
    }

    for(i = 0 ; i < n ; i ++)
    {
        data stu = students[i];
        if(!stu.isLuqu)
        {
            for(j = 0 ;j < kk ; j++)
            {
                int nowc = stu.choices[j]; // cno
                if( (int)cstu[nowc].size() < quota[nowc])
                {
                    cstu[nowc].push_back(stu.sno);
                    students[i].isLuqu = true;
                    students[i].cno = nowc ;
                    break;
                }else{
                    // 下面两句是一样的 这里必须使用um映射
                    int lastRank = students[ um[cstu[nowc][quota[nowc]-1]] ].rank;
                    //int lastRank = students[ um[cstu[nowc][(int)cstu[nowc].size() - 1]] ].rank;
                    if(stu.rank == lastRank)
                    {
                        cstu[nowc].push_back(stu.sno);
                        students[i].isLuqu = true;
                        students[i].cno = nowc ;
                        break;
                    }
                }
            }
        }
    }
    for(i = 0; i < m ; i++)
    {
        int len2 = cstu[i].size();
        if(len2 > 1)
            sort(cstu[i].begin() , cstu[i].end() , cmp2);
        if(len2 == 0)
            printf("\n");
        else{
            printf("%d",cstu[i][0]);
            for(j = 1 ; j < len2 ;j++)
                printf(" %d",cstu[i][j]);
            printf("\n");
        }
    }
    //printf("");
    return 0;
}

你可能感兴趣的:(1080. Graduate Admission (30))