UVA - 10249 The Grand Dinner

Time Limit: 5000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

[]   [Go Back]   [Status]  

Description

 

Problem D    The Grand Dinner

Input: standard input  Output: standard output     Time Limit: 15 seconds  Memory Limit: 32 MB

Each team participating in this year’s ACM World Finals contest is expected to join the grand dinner to be arranged after the prize giving ceremony ends. In order to maximize the interaction among the members of different teams, it is expected that no two members of the same team sit at the same table.

Now, given the number of members in each team (including contestants, coaches, reserves, guests etc.) and the seating capacity of each available table, you are to determine whether it is possible for the teams to sit as described in the previous paragraph. If such an arrangement is possible you must also output one possible seating arrangement. If there are multiple possible arrangements, any one is acceptable.

Input

 The input file may contain multiple test cases. The first line of each test case contains two integers M (1 £ M £ 70) and N (1 £ N £ 50) denoting the number of teams and the number of tables respectively. The second line of the test case contains M integers where the i-th (1 £ i £ M) integer mi (1 £ mi £ 100) indicates the number of members of team i. The third line contains N integers where the j-th (1 £ j £ N) integernj (2 £ nj £ 100) indicates the seating capacity of table j.

A test case containing two zeros for M and N terminates the input.

Output

For each test case in the input print a line containing either 1 or 0 depending on whether or not there exists a valid seating arrangement of the team members. In case of a successful arrangement print M additional lines where the i-th (1 £ i £ M) of these lines contains a table number (an integer from 1 to N) for each of the members of team i.

Sample Input

4 5

4 5 3 5

3 5 2 6 4

4 5

4 5 3 5

3 5 2 6 3

0 0

Sample Output

1

1 2 4 5

1 2 3 4 5

2 4 5

1 2 3 4 5

0


(World Finals Warm-up Contest, Problem Setter: Rezaul Alam Chowdhury)

 

 

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<algorithm>
 4 using namespace std; 
 5 const int maxn=100;
 6 struct Team{
 7     int id,num;
 8     int seat[maxn];
 9 };
10 struct Table{
11     int id,cap;
12 };
13 Team team[maxn];
14 Table table[maxn];
15 bool myComp(const Team& a,const Team& b){
16     return a.num > b.num;
17 }
18 bool myComp2(const Table& a,const Table& b){
19     return a.cap>b.cap;
20 }
21 bool myComp3(const Team&a,const Team&b){
22     return a.id<b.id;
23 }
24 bool myCheck(int n){
25     for(int i=0;i<n;i++)
26         if(table[i].cap<0)
27             return false;
28     return true;
29 }
30 int main(){
31     //freopen("in.txt","r",stdin);
32     for(int m,n;cin>>m>>n &&(m||n);){
33         for(int i=0;i<m;i++){
34             scanf("%d",&team[i].num);
35             team[i].id=i;
36         }
37         for(int i=0;i<n;i++){
38             scanf("%d",&table[i].cap);
39             table[i].id=i;
40         }
41         sort(team,team+m,myComp);
42         int flag=(team[0].num > n? 0:1);
43         for(int i=0;i<m;i++){
44             sort(table,table+n,myComp2);
45             for(int j=0;j<team[i].num;j++){
46                 table[j].cap--;
47                 team[i].seat[j]=table[j].id;
48             }
49         }
50         if(myCheck(n)&& flag){
51             printf("1\n");
52             sort(team,team+m,myComp3);
53             for(int i=0;i<m;i++){
54                 sort(&team[i].seat[0],&team[i].seat[team[i].num]);
55                 for(int j=0;j<team[i].num;j++)
56                     cout<<(j==0?"":" ")<<team[i].seat[j]+1;
57                 printf("\n");
58             }
59         }
60         else
61             printf("0\n");
62     }
63 }

贪心算法。

首先,将队伍按人数从大到小排序,将桌子按容量从大到小排序。

目的是让人数多的队伍先坐到剩余容量大的桌子中去,这样排出来的结果是最优的。

判断:1.若人数最多的队伍比桌子的数量还多,那么就输出 0;

        2.若某一张桌子的剩余容量为负值,表明这个桌子的已坐人数大于它的限坐人数,这也要输出 0;

posted @ 2013-03-23 00:38  姜楠  阅读(546)  评论(0编辑  收藏  举报