UVA - 10249 The Grand Dinner

Description

Download as PDF

Problem D

The Grand Dinner

Input: standard input

Output: standard output

Time Limit: 15 seconds

Memory Limit: 32 MB

 

Each team participating in this years 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) integer nj (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

题意:有m个队伍,n个桌子,要求每一个队伍里的人不能出如今同一个桌子上,问是否有这样的可能

思路:贪心的每次将桌子能坐的人排序,从大的往小的坐

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 110;

struct Node {
	int id, num;
	bool operator <(const Node &a) const {
		return num > a.num;
	}
} a[maxn], b[maxn];

struct team {
	int index[maxn];
} res[maxn];
int n, m;
int tmp[maxn];

int main() {

	while (scanf("%d%d", &m, &n) != EOF && n+m) {
		int flag = 1;
		for (int i = 1; i <= m; i++) {
			scanf("%d", &a[i].num);
			tmp[i] = a[i].num;
			a[i].id = i;
			if (a[i].num > n)
				flag = 0;
		}
		for (int i = 1; i <= n; i++) {
			scanf("%d", &b[i].num);
			b[i].id = i;
		}
		for (int i = 1; i <= m && flag; i++) {
			sort(b+1, b+1+n);
			for (int j = 1; j <= a[i].num; j++) {
				if (b[j].num > 0) {
					res[a[i].id].index[j] = b[j].id;					
					b[j].num--;
				}
				else {
					flag = 0;
					break;
				}
			}
		}
		if (!flag) {
			printf("0\n");
			continue;
		}
		printf("1\n");
		for (int i = 1; i <= m; i++) {
			sort(res[i].index+1, res[i].index+1+tmp[i]);
			for (int j = 1; j <= tmp[i]; j++) {
				if (j == 1)
					printf("%d", res[i].index[j]);
				else printf(" %d", res[i].index[j]);
			}
			printf("\n");
		}
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值