1080. Graduate Admission (30)

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

此题主要是个条件排序, 还用到了 map<>映射,还有如何根据题目构造结构和相关的变量,基本上完全按照要求来,下面是一次ac的代码,没写什么注释 ,不过思路清晰

#include <stdio.h>
#include <iostream> 
#include <stdlib.h>
#include <vector>
#include <string>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <queue>
#include <map>
#include <unordered_map>
#include <sstream>

using namespace std;

#define N 40001
#define M 101
#define K 6

struct mydata{
  int no;
  int ge;// national entrance exam
  int gt;
  int totalscore;
  vector<int> choises;
  int rank;
  int school;
};

int n, m, k;

int quota[M];

vector<mydata> vstu;

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

vector<int> schools[M]; //第 i个学校 填报的stu的 no 

map<int, int> um; // no编号的 stu 在 vstu的编号

vector<int> luqu[M];

bool cmp2(int no1, int no2)
{
  if (vstu[um[no1]].rank > vstu[um[no2]].rank)
  {
    return true;
  }
  return false;
}

bool cmp3(int a, int b)
{
  return a < b;
}

int main()
{
  //freopen("in", "r", stdin);
  while (scanf("%d%d%d",&n,&m,&k) != EOF)
  {
    um.clear();
    int i,j;
    for (i = 0; i < m; i++)
    {
      scanf("%d", &quota[i]);
    }

    mydata dt;
    int tmp;
    vstu.resize(n);
    for (i = 0; i < n; i++)
    {
      dt.no = i;
      dt.school = -1;
      dt.choises.clear();
      scanf("%d%d", &dt.ge, &dt.gt);
      dt.totalscore = dt.ge + dt.gt;
      for (j = 0; j < k; j++){
        scanf("%d", &tmp);
        dt.choises.push_back(tmp);
        schools[tmp].push_back(dt.no);
      }
      vstu[i] = dt;
    }
    sort(vstu.begin(), vstu.end(), cmp);
    vstu[0].rank = 1;
    um[vstu[0].no] = 0;
    for (i = 1; i < n; i++)
    {
      if (vstu[i].totalscore == vstu[i - 1].totalscore &&
        vstu[i].ge == vstu[i - 1].ge)
        vstu[i].rank = vstu[i - 1].rank;
      else{
        vstu[i].rank = i + 1;
      }
      um[vstu[i].no] = i;
    }

    int cout[M];
    memset(cout, 0, sizeof(cout));

    for (i = 0; i < n; i++)
    {
      while(vstu[i].school == -1)
      {
        mydata dt = vstu[i];
        for (j = 0; j < k; j++)
        {
          int thChoice = vstu[i].choises[j];
          if (cout[thChoice] < quota[thChoice])
          {
            vstu[i].school = thChoice;
            cout[thChoice] ++;
            luqu[thChoice].push_back(vstu[i].no);
            break;
          }
          else{
            int theLastNo = luqu[thChoice][quota[thChoice] - 1];
            if (vstu[i].rank == vstu[um[theLastNo]].rank)
            {
              cout[thChoice] ++;
              luqu[thChoice].push_back(vstu[i].no);
              vstu[i].school = thChoice;
              break;
            }
          }
        }
        if (j == k)
          break;
      }
    }

    for (i = 0; i < m; i++)
    {
      vector<int> vvv = luqu[i];
      int lenn = vvv.size();
      if (lenn == 0)
      {
        printf("\n");
      }
      else{
        if (lenn == 1)
          printf("%d\n", luqu[i][0]);
        else{
          sort(vvv.begin(), vvv.end(), cmp3);
          printf("%d", vvv[0]);
          for (j = 1; j < lenn; j++)
          {
            printf(" %d", vvv[j]);
          }
          printf("\n");
        }
      }
    }

    //printf("\n");
  }
  return 0;
}

你可能感兴趣的:(排序)