Created
January 18, 2014 14:40
-
-
Save KT-Yeh/8491489 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#include <algorithm> | |
using namespace std; | |
struct temp_type{ | |
int *t; | |
int index; | |
}temp[51]; | |
bool cmp(temp_type a,temp_type b){ | |
return *(a.t)>*(b.t); | |
} | |
int main() | |
{ | |
int M,N; //M:number of teams, N:number of tables | |
int m[71],n[51]; //m:number of members of a team, n:seating capacity of a table | |
while (scanf("%d %d",&M,&N)){ | |
if (!M && !N) break; | |
int i,j,ans[71][100]; | |
for(i=0;i<M;i++) scanf("%d",&m[i]); | |
for(i=0;i<N;i++) scanf("%d",&n[i]); | |
bool ok=1; | |
for(i=0;i<M && ok;i++){ | |
if(m[i]>N) {ok=0; break;} //隊員數量大於桌子數量 | |
//將桌子的座位數量與index複製到temp | |
//並依照剩餘座位數量排序 | |
for(j=0;j<N;j++) { | |
temp[j].t=&n[j]; | |
temp[j].index=j; | |
} | |
sort(temp,temp+N,cmp); | |
int n_ans=0; | |
//隊員m[i]個,排入剩餘位子前m[i]多的桌子 | |
for(j=0;j<m[i];j++){ | |
if(*(temp[j].t)<=0) {ok=0; break;} | |
else { | |
(*(temp[j].t))--; | |
ans[i][n_ans++]=temp[j].index; | |
} | |
} | |
sort(ans[i],ans[i]+n_ans); //將index由小排到大 | |
} | |
if(ok){ | |
printf("1\n"); | |
for(i=0;i<M;i++) { | |
for(j=0;j<m[i];j++) | |
printf("%d ",ans[i][j]+1); | |
printf("\n"); | |
} | |
} | |
else printf("0\n"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment