A Contesting Decision - 1581

A Contesting Decision
Time Limit: 1000MS
Memory Limit: 10000K
Total Submissions: 2711
Accepted: 1915

Description

Judging a programming contest is hard work, with demanding contestants, tedious decisions,and monotonous work. Not to mention the nutritional problems of spending 12 hours with only donuts, pizza, and soda for food. Still, it can be a lot of fun. 
Software that automates the judging process is a great help, but the notorious unreliability of some contest software makes people wish that something better were available. You are part of a group trying to develop better, open source, contest management software, based on the principle of modular design. 
Your component is to be used for calculating the scores of programming contest teams and determining a winner. You will be given the results from several teams and must determine the winner. 
Scoring 
There are two components to a team's score. The first is the number of problems solved. The second is penalty points, which reflects the amount of time and incorrect submissions made before the problem is solved. For each problem solved correctly, penalty points are charged equal to the time at which the problem was solved plus 20 minutes for each incorrect submission. No penalty points are added for problems that are never solved. 
So if a team solved problem one on their second submission at twenty minutes, they are charged 40 penalty points. If they submit problem 2 three times, but do not solve it, they are charged no penalty points. If they submit problem 3 once and solve it at 120 minutes, they are charged 120 penalty points. Their total score is two problems solved with 160 penalty points. 
The winner is the team that solves the most problems. If teams tie for solving the most problems,then the winner is the team with the fewest penalty points.

Input

For the programming contest your program is judging, there are four problems. You are guaranteed that the input will not result in a tie between teams after counting penalty points. 
Line 1 < nTeams > 
Line 2 - n+1 < Name > < p1Sub > < p1Time > < p2Sub > < p2Time > ... < p4Time > 
The first element on the line is the team name, which contains no whitespace.Following that, for each of the four problems, is the number of times the team submitted a run for that problem and the time at which it was solved correctly (both integers). If a team did not solve a problem, the time will be zero. The number of submissions will be at least one if the problem was solved.

Output

The output consists of a single line listing the name of the team that won, the number of problems they solved, and their penalty points.

Sample Input

4
Stars 2 20 5 0 4 190 3 220
Rockets 5 180 1 0 2 0 3 100
Penguins 1 15 3 120 1 300 4 0
Marsupials 9 0 3 100 2 220 3 80

Sample Output

Penguins 3 475

Source

/***********************************************************************
    Copyright (c) 2015,wangzp
    All rights no reserved.
  
    Name: 《A Contesting Decision》In PEKING UNIVERSITY ACM
    ID:   PROBLEM 1581
    问题简述: 求几个参赛队伍中罚分最少的队伍,规则如下:
	          1.提交1次通过时间即为罚的分数;
			  2.提交问题没有通过,不管次数为多少次,罚分为0;
			  3.提交问题通过,但是大于等于2次,每多一次,就多罚20分;
			  4.先看答对题目最多,然后再看总罚分数最少的队伍即为胜利者。
    Date: Sep 28, 2015 
 
***********************************************************************/
#include
#include 

#define MAX_SCORE 10000000
#define CONTESTER 100
typedef struct {
	char name[20];
	int test_num[4];
	int test_time[4];
	int num_sloved;
	int sum_score;

}CONTEST;

/*根据规则,计算罚分*/
int contest_fun(int num,int time)
{
	int sum = 0;
	if (num == 1 && time != 0)
	{
		sum += time;
	} 
	else if (num > 1 && time != 0)
	{
		sum = time;
		while (num > 1)
		{
			sum += 20;
			num--;
		}
	}
	return sum;
}

int main(void)
{
	CONTEST contest_sub[CONTESTER];
	int min_score,index,sloved;
	int i,j,n;
	memset(contest_sub,NULL,CONTESTER*sizeof(CONTEST));

	min_score = MAX_SCORE;
	index = 0;
	sloved = -1;

	scanf("%d",&n);
	for (i = 0;i < n;i++)
	{
		scanf("%s",contest_sub[i].name);
		for (j = 0;j < 4;j++)
		{
			scanf("%d",&contest_sub[i].test_num[j]);
			scanf("%d",&contest_sub[i].test_time[j]);
			if (contest_sub[i].test_time[j] != 0)
			{
				contest_sub[i].num_sloved++;
			}
			contest_sub[i].sum_score += contest_fun(contest_sub[i].test_num[j],contest_sub[i].test_time[j]);
		}
		
		if (contest_sub[i].num_sloved >= sloved)
		{
			if (contest_sub[i].sum_score <= min_score)
			{
				sloved = contest_sub[i].num_sloved;
				min_score = contest_sub[i].sum_score;
				index = i;
			}
		}
	}
	printf("%s %d %d\n",contest_sub[index].name,contest_sub[index].num_sloved,contest_sub[index].sum_score);	
	return 0;
}

Task 3: Simulate Election (4 marks) To select the winner of the election, instant-runoff voting (IRV) is used. This method selects a winner over the course of a number of rounds. The following procedure is repeated until there is only one candidate left: Assign each ballot to the candidate who is ranked highest on that ballot and who has not yet been eliminated. Eliminate the candidate(s) with the lowest number of assigned ballots in that round, so they are not counted in future rounds. For this task, you must run a simulated IRV for an election given a list of valid positional ballots. Write a function simulate_election(ballots, parties) You need to write a function that returns the full history of elimination rounds for the given election. Arguments: ballots: a list of valid positional ballots. parties: the ordered list of parties contesting the election. Returns: a list of elimination round history, where each round consists of a tuple with: A dictionary mapping each remaining candidate to their vote total A set of candidates to be eliminated after that round. Details: If two candidates are tied for the lowest vote total in a round, they are both eliminated simultaneously If there is no unique winner, your program should raise a TiedElectionError as defined at the top of the provided code. As a reminder, you can raise an error using the built in Python statement raise TiedElectionError. This is a custom Exception type that we have defined for you. You can use it like any other Exception , and there is no need to understand how we have defined it. Interested students can learn more about custom object and classes in this optional worksheet. Example Calls:
最新发布
10-17
```python def simulate_election(ballots, parties): """ Simulates an Instant-Runoff Voting (IRV) election and returns the history of elimination rounds. Arguments: ballots: List of valid positional ballots. Each ballot is a list of party indices representing the voter's ranking (from highest to lowest preference). parties: Ordered list of parties (candidates) in the election. Used to map indices to names. Returns: A list of tuples. Each tuple contains: - A dictionary mapping each remaining candidate (by name) to their vote total - A set of candidates (by name) to be eliminated in that round Raises: TiedElectionError: If at the end of the process there is more than one candidate remaining and they are tied (i.e., no unique winner). """ # Make a copy of remaining candidates (by name), so we can eliminate them over time remaining = set(parties) # This will store the full history of rounds history = [] # Keep going until one or zero candidates remain while len(remaining) > 1: # Dictionary to count votes for each remaining candidate vote_count = {candidate: 0 for candidate in remaining} # Assign each ballot to the highest-ranked candidate still in the race for ballot in ballots: for rank in ballot: candidate = parties[rank] # Map index back to party name if candidate in remaining: vote_count[candidate] += 1 break # Found top-preferred active candidate; stop checking this ballot # Find the minimum number of votes received min_votes = min(vote_count.values()) # Identify all candidates with the minimum vote count (to be eliminated) to_eliminate = {cand for cand, votes in vote_count.items() if votes == min_votes} # Record this round's state before elimination history.append((dict(vote_count), set(to_eliminate))) # Eliminate the bottom candidates remaining -= to_eliminate # After loop: either 0 or 1 candidate remains if len(remaining) == 0: # No candidate left → tie among last ones eliminated raise TiedElectionError("Election resulted in a tie.") # Now only one candidate should remain — but we must check final condition # According to problem: if no *unique* winner exists, raise error # The only way this happens here is if the last two were tied and both eliminated, # which leaves `remaining` empty. But what if two remain and have equal votes? # Actually, our loop stops when len(remaining) <= 1. # So suppose two candidates are tied for last, both get eliminated — fine. # But if two candidates are left and both have same votes? They would NOT be eliminated # unless they are the lowest — but they're the only ones! So both would be eliminated # **only** if both have the same (lowest) value — which they do. # Therefore, if two final candidates are tied, they are BOTH eliminated → remaining becomes empty # So we already catch that via the above check. # However, let’s ensure correctness: if the last round eliminates all remaining candidates # because they’re tied for lowest, then we end up with no winner → must raise error. # Our current logic already does that: after the loop, if `remaining` is empty → raise error. return history ``` ### Explanation: The function simulates **Instant-Runoff Voting (IRV)** where voters rank candidates by preference (via indices). In each round: 1. **Votes are reassigned**: Every ballot goes to the highest-ranked candidate on that ballot who hasn't been eliminated yet. 2. **Count votes**: For all currently remaining candidates, tally how many ballots assign them as top choice. 3. **Eliminate weakest**: All candidates with the fewest votes (minimum count) are marked for elimination — even if multiple tie for lowest, they are all removed **in the same round**. 4. **Record round**: Append a tuple containing: - A dictionary of `{candidate: vote_total}` - A set of candidates to eliminate 5. **Repeat** until one or zero candidates remain. If **zero** candidates remain at the end (e.g., final two were tied and both eliminated), it means there’s no unique winner → raise `TiedElectionError`. > Note: The key insight is that ties in the **lowest** vote totals cause simultaneous eliminations. Only when exactly **one** candidate remains do we accept them as the unique winner. --- ### Example Usage: ```python # Suppose: parties = ["Liberal", "Labor", "Greens", "National"] ballots = [ [0, 1, 2, 3], # prefers Liberal > Labor > Greens > National [1, 2, 0, 3], # prefers Labor > Greens > Liberal > National [2, 1, 0, 3], # prefers Greens > Labor > Liberal > National ] # Call: history = simulate_election(ballots, parties) for i, (votes, elim) in enumerate(history): print(f"Round {i+1}: Votes={votes}, Eliminated={elim}") ``` This would output something like: ``` Round 1: Votes={'Liberal': 1, 'Labor': 1, 'Greens': 1, 'National': 0}, Eliminated={'National'} Round 2: Votes={'Liberal': 1, 'Labor': 2, 'Greens': 1}, Eliminated={'Liberal', 'Greens'} ... ``` And if only `Labor` remains → success. If next round tries to eliminate both final two due to tie → raises `TiedElectionError`. ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值