UVA - 10249 The Grand Dinner

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) 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;
}

UVA - 10249 The Grand Dinner

时间: 2024-10-15 07:11:40

UVA - 10249 The Grand Dinner的相关文章

UVA 10249 - The Grand Dinner(网络流 or 贪心)

UVA 10249 - The Grand Dinner 题目链接 题意:给定几队队员,几张桌子,每队有一个人数,每个桌子也有一个容量上限,要求一种安排方案,使得没有同队人坐在一个桌子上,求方案 思路:明显贪心可以搞- -, 每次往容量最多的桌子塞就可以了..不过这题既然出在网络流这章,还是用网络流也搞了下 源点连到每队,桌子连到汇点,容量就是容量,然后每队和每个桌子相连,容量为1,表示一个桌子只能做一个队员,然后跑一下最大流即可 代码: 网络流: #include <cstdio> #inc

编程题目分类(剪辑)

1. 编程入门 2. 数据结构 3. 字符串 4. 排序 5. 图遍历 6. 图算法 7. 搜索:剪枝,启发式搜索 8. 动态规划/递推 9. 分治/递归 10. 贪心 11. 模拟 12. 算术与代数 13. 组合问题 14. 数论 15. 网格,几何,计算几何 [编程入门] PC 110101, uva 100, The 3n+1 problem, 难度 1 PC 110102, uva 10189, Minesweeper, 难度 1 PC 110103, uva 10137, The T

计划,,留

下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinejudge.org 西班牙Valladolid大学的程序在线评测系统,是历史最悠久.最著名的OJ. 一.<算法竞赛入门经典> 刘汝佳 (UVaOJ 351道题) 以下部分内容摘自:http://sdkdacm.5d6d.com/thread-6-1-1.html "AOAPC I"

算法竞赛入门经典+挑战编程+USACO

下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发.   一.UVaOJ http://uva.onlinejudge.org  西班牙Valladolid大学的程序在线评测系统,是历史最悠久.最著名的OJ.   二.<算法竞赛入门经典> 刘汝佳  (UVaOJ  351道题)  以下部分内容摘自:http://sdkdacm.5d6d.com/thread-6-1-1.html   "AO

(Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO

下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinejudge.org 西班牙Valladolid大学的程序在线评测系统,是历史最悠久.最著名的OJ. 二.<算法竞赛入门经典> 刘汝佳  (UVaOJ  351道题)  以下部分内容摘自:http://sdkdacm.5d6d.com/thread-6-1-1.html “AOAPC I”是刘汝佳(大

UVA - 12002 Happy Birthday

Description  Happy Birthday  Today it's February 13th. It's a very special day: Miguel's birthday! Like every year, he's organised a big celebration for all his friends. He prepared a succulent dinner at his house. Everyone had a lot of fun and the c

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d

UVA 10341 Solve It

Problem F Solve It Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB Solve the equation: p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0 where 0 <= x <= 1. Input Input consists of multiple test cases and te

UVA 11014 - Make a Crystal(容斥原理)

UVA 11014 - Make a Crystal 题目链接 题意:给定一个NxNxN的正方体,求出最多能选几个整数点.使得随意两点PQ不会使PQO共线. 思路:利用容斥原理,设f(k)为点(x, y, z)三点都为k的倍数的点的个数(要扣掉一个原点O).那么全部点就是f(1),之后要去除掉共线的,就是扣掉f(2), f(3), f(5)..f(n).n为素数.由于这些素数中包括了合数的情况,而且这些点必定与f(1)除去这些点以外的点共线,所以扣掉.可是扣掉后会扣掉一些反复的.比方f(6)在f