-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathTheGrandDinner_UVa10249.java
122 lines (102 loc) · 2.5 KB
/
TheGrandDinner_UVa10249.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package v102;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;
public class TheGrandDinner_UVa10249 {
static Table[] tables;
static Team[] teams;
static int available;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
while(true)
{
StringTokenizer st = new StringTokenizer(br.readLine());
int M = Integer.parseInt(st.nextToken());
int N = Integer.parseInt(st.nextToken());
if(M==0)
break;
st = new StringTokenizer(br.readLine());
teams = new Team[M];
for(int i = 0; i < M; i++)
teams[i] = new Team(i+1,Integer.parseInt(st.nextToken()));
st = new StringTokenizer(br.readLine());
tables = new Table[N];
available = 0;
for(int i = 0; i < N; i++)
{
tables[i] = new Table(i+1,Integer.parseInt(st.nextToken()));
if(tables[i].Capacity > 0)
available++;
}
Arrays.sort(teams);
boolean possible = true;
for(int i = 0; i < M && possible; i++)
if(!seatTeam(i))
possible = false;
if(possible)
{
sb.append("1\n");
Arrays.sort(teams, new ID());
for(int i = 0; i < M; i++)
{
Team cur = teams[i];
Arrays.sort(cur.tables);
for(int j = 0; j < cur.members; j++)
sb.append(cur.tables[j]).append(j==cur.members-1?"\n":" ");
}
}
else
sb.append("0\n");
}
System.out.print(sb);
}
static boolean seatTeam(int idx)
{
Team x = teams[idx];
if(x.members > available)
return false;
Arrays.sort(tables);
for(int i = 0, j = 0; j < x.members; i++)
if(tables[i].Capacity>0)
{
x.tables[j++] = tables[i].id;
tables[i].Capacity--;
if(tables[i].Capacity==0)
available--;
}
return true;
}
static class Team implements Comparable<Team>
{
int id, members;
int[] tables;
Team(int x, int y)
{
id = x; members = y;
tables = new int[y];
}
public int compareTo(Team o) {
return o.members - this.members;
}
}
static class Table implements Comparable<Table>
{
int id, Capacity;
Table(int x, int y) { Capacity = y; id = x;}
public int compareTo(Table o) {
return o.Capacity - this.Capacity;
}
}
static class ID implements Comparator<Team>
{
@Override
public int compare(Team o1, Team o2) {
// TODO Auto-generated method stub
return o1.id - o2.id;
}
}
}