//此为1005题在VS2010上的编译,已经通过,提交到九度上要做相应的修改
// 9dutest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<stdio.h>
//#include<stdlib.h>>
//关于程序的些许想法
//法一对学生成绩进行排序,从高到低录取,学校配额减一
//法二,按学校录取,对各个学校的第一志愿寻找进行排序,去除已经录取的学生以及各个学校的配额
typedef struct studentinf{
long id;
int ge;
int gi;
int application[5];
} stuinf;
void quck_sort(stuinf *a, int low, int high ){
int i = low;
int j = high;
stuinf tempstudent = a[low];
int tempgrade = a[low].ge +a[low].gi;
stuinf temp;
if(low >= high) return;
while(i < j){
while((i<j) && ((a[j].ge + a[j].gi) >= tempgrade)){ j--; }
while((i<j) && ((a[i].ge + a[i].gi) <= tempgrade)){ i++; }
if(i < j){
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
a[low] = a[i];
a[i] = tempstudent;
quck_sort(a, low, i-1);
quck_sort(a, i+1, high);
}
void adjust(stuinf *a, int length){
int i;
for(i=1; i<length; i++){
if((a[i].ge + a[i].gi) == (a[i-1].ge + a[i-1].gi) && (a[i].ge < a[i-1].ge)){
stuinf temp;
temp = a[i-1];
a[i-1] = a[i];
a[i] = temp;
}
}
}
void CalculateAdmition(stuinf *a, int N, int K, int M, int *school){
int i,j,p;
long schooladmission[100+1][100];
int tempadmitnumber[102][4];
for(i=0; i<M; i++){
tempadmitnumber[i][0] = school[i];
tempadmitnumber[i][1] = 0;
tempadmitnumber[i][2] = 0;
}
int temp;
for(i=N-1; i>=0; i--){
for(j=0; j<K; j++){
if(school[a[i].application[j]] > 0){
school[a[i].application[j]]--;
schooladmission[ a[i].application[j] ][ tempadmitnumber[a[i].application[j]][1] ] = a[i].id;
tempadmitnumber[a[i].application[j]][1]++;
tempadmitnumber[a[i].application[j]][2] = i;
break;
}
else if(((a[i].ge+a[i].gi) == (a[tempadmitnumber[a[i].application[j]][2]].ge +a[tempadmitnumber[a[i].application[j]][2]].gi)) && (a[i].ge == a[tempadmitnumber[a[i].application[j]][2]].ge)){
p = 0;
int tempid = a[i].id;
int tempadd;
schooladmission[a[i].application[j]][tempadmitnumber[a[i].application[j]][1]] = a[i].id;
tempadmitnumber[a[i].application[j]][1]++;
tempadmitnumber[a[i].application[j]][2] = i;
break;
}
}
}
long tmpbubble;
for(i=0; i<M; i++){
// printf("%d ",i);
for(j=0; j<tempadmitnumber[i][1];j++){
for(p=0;p<(tempadmitnumber[i][1]-j-1); p++){
if(schooladmission[i][p]>schooladmission[i][+1]){
tmpbubble = schooladmission[i][p];
schooladmission[i][p] = schooladmission[i][p+1];
schooladmission[i][p+1] = tmpbubble;
}
}
}
p = 1;
for(j=0; j<tempadmitnumber[i][1]; j++){
if(schooladmission[i][j]>=0){
if(p == 0){
printf(" ");
p = 1;
}
printf("%ld",schooladmission[i][j]);
p = 0;
}
}
printf("\n");
}
}
int _tmain(int argc, _TCHAR* argv[])
{
long N;
int M;
int K;
int i,j;
while(scanf("%d %d %d", &N, &M, &K) != EOF){
stuinf student[4002];
int school[103];
for(i=0; i<M; i++){
scanf("%d", &school[i]);
}
for(i=0; i<N; i++){
student[i].id = i;
scanf("%d %d", &student[i].ge, &student[i].gi);
for(j=0; j<K; j++){
scanf("%d",&student[i].application[j]);
}
}
quck_sort(student,0, N-1);
adjust(student, N);
CalculateAdmition(student,N,K,M,school);
}
return 0;
}