Algorithm


problem Link : https://onlinejudge.org/index.php?option=onlinejudge&Itemid=8&page=show_problem&problem=1190 

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

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <bits/stdc++.h>
using namespace std;

int main() {
    int m,n,v;
    while(scanf("%d %d",&m,&n),m){
        priority_queue < pair<int,int>> tables;
        vector < pair<int,int>> teams;
        vector < vector<int>> res(m);
        for(int i=0;i < m;i++){
            cin >> v;
            teams.push_back({v,i});
        }
        sort(teams.begin(),teams.end(),greater < pair<int,int>>());
        for(int i=0;i < n;i++){
            cin >> v;
            tables.push({v,i});
        }
        bool valid = true;
        for(int i=0;i < teams.size() && valid;i++){
            int teamSize = teams[i].first;
            // get top k tables
            if(tables.size() < teamSize) valid = false;
            else {
                vector < pair<int,int>> usedTables;
                vector<int> team;
                for(int j=0;j < teamSize;j++>{
                    pair<int,int> table = tables.top();tables.pop();
                    team.push_back(table.second+1);
                    if(--table.first)
                        usedTables.push_back(table);
                }
                res[teams[i].second] = team;
                for(auto& p : usedTables) tables.push(p);
            }
        }
        if(valid){
            printf("1\n");
            for(auto& team : res){
                for(auto& seat : team){
                    cout << seat;
                    if(&seat == &team.back()) cout << endl;
                    else cout << " ";
                }
            }
        } else {
            printf("0\n");
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
4 5
4 5 3 5
3 5 2 6 4
4 5
4 5 3 5
3 5 2 6 3
0 0

Output

x
+
cmd
1 2 4 5
1 2 3 4 5
2 4 5
1 2 3 4 5
0
Advertisements

Demonstration


UVA Online Judge solution - 10249 - The Grand Dinner - UVA Online Judge solution C,C++,Java

Previous
UVA Online Judge solution -10235 - Simply Emirp - UVA Online Judge solution C,C++,Java
Next
UVA Online Judge solution -10252 - Common Permutation - UVA Online Judge solution in C,C++,java