The Grand Dinner Competitive Programming question online judge

Problem Statement:

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 1to N ) for each of the members of team i.

Solution :

				
					import sys
from heapq import heappop, heappush, heapify



def read_num():
    return list(map(int, sys.stdin.readline().split()))

def load_case():
    M, N = read_num()
    if not M:
        return None, None

    teams = read_num() # Team members
    tables = read_num() # Tables capacity

    return teams, tables


def find_seating(tables, teams):

    if not tables:
        return None

    # List of members of each group seated at each table
    team_tables = [list() for _ in teams]

    # 
    tables = sorted([(seats,table) for table, seats in enumerate(tables)], reverse=True)
    
    
    team_heap = [(-members, team) for team, members in enumerate(teams)]
    heapify(team_heap)

    for seats, table in tables:
        # Check all members are seated
        if not team_heap:
            break
        
        #
        teams_seated = [] # List of teams seated at current table
        while team_heap and len(teams_seated)<seats:
            t = heappop(team_heap)
            teams_seated.append(t)

        # Record seating for the table
        for t in teams_seated:
            team_tables[t[1]].append(table+1)

        # Re-enque into heap teams with non seated members
        for members, team in teams_seated:
            if members+1 < 0:
                heappush(team_heap, (members+1, team))


    if team_heap:
        return None # There are team members without seating
    else:
        return team_tables


def print_seating(s):
    if not s:
        print(0)
    else:
        print(1)
        for t in s:
            print(' '.join(map(str, sorted(t))))


if __name__ == '__main__':
    while True:
        teams, tables = load_case()
        if teams is None:
            break
        print_seating(find_seating(tables, teams))


				
			


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published.