24h購物| | PChome| 登入
2012-08-25 12:30:16| 人氣696| 回應0 | 上一篇 | 下一篇

[UVA][Greedy] 10249 - The Grand Dinner

推薦 0 收藏 0 轉貼0 訂閱站台

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


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

每次抓最多人的團體出來分桌, 分桌的時候以剩餘最多的桌子開始分配

#include <stdio.h>
#include <stdlib.h>
typedef struct {
    int c, i;
} DD;
int cmp(const void *i, const void *j) {
    DD *x, *y;
    x = (DD *)i, y = (DD *)j;
    if(x->c != y->c)
        return y->c - x->c;
    return x->i - y->i;
}
int cmp1(const void *i, const void *j) {
    return *(int *)i - *(int *)j;
}
int main() {
    int n, m, i, j, B[70];
    DD A[70];
    DD D[50];
    while(scanf("%d %d", &n, &m) == 2) {
        if(n == 0 && m == 0)    break;
        for(i = 0; i < n; i++)
            scanf("%d", &A[i].c), A[i].i = i, B[i] = A[i].c;
        for(i = 0; i < m; i++)
            scanf("%d", &D[i].c), D[i].i = i;
        int ans[70][70];
        qsort(A, n, sizeof(DD), cmp);
        for(i = 0; i < n; i++) {
            qsort(D, m, sizeof(DD), cmp);
            int tmp = A[i].c, idx = 0, err = 0;
            for(j = 0; j < m; j++) {
                if(tmp == 0)    break;
                if(D[j].c == 0) {
                    err = 1;
                    break;
                } else {
                    D[j].c--;
                    tmp--;
                    ans[A[i].i][idx++] = D[j].i;
                }
            }
            if(err || tmp) break;
        }
        if(i == n) {
            puts("1");
            for(i = 0; i < n; i++) {
                qsort(ans[i], B[i], sizeof(int), cmp1);
                printf("%d", ans[i][0]+1);
                for(j = 1; j < B[i]; j++)
                    printf(" %d", ans[i][j]+1);
                puts("");
            }
        } else
            puts("0");
    }
    return 0;
}

台長: Morris
人氣(696) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][Greedy] 10382 - Watering Grass
此分類上一篇:[UVA][Greedy] 11520 - Fill the Square

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文