UVALive 7147 思维题(贪心)

In normal football games, the winner team gets 3 points, loser team gets 0 point, and if there is a draw
game, both of the teams get 1 point.
In World Cup 1994, Group D there is an interest thing happened. There are 4 teams in that group,
Argentina, Nigeria, Bulgaria and Greece. Greece lost all the 3 matehes and scored 0. Argentina defeated
Nigeria, Nigeria defeated Bulgaria and Bulgaria defeat Argentina. Since there are only 2 teams could
advance to the next stage, one of the 3 teams will be eliminated. It’s really a surprise that a team
scored 6 will be eliminated in a 4 advance 2 group competition. That is really interesting and we’d like
to dig it deeper.
In this problem, there are N teams in a group. Within a group, any two teams will play each other
exactly once, so each team will have N − 1 matches in total.
In a match, the winner team will get A points and the loser team gets C points. If the match ends
with a draw, each of the teams gets B points. When all matches finished, the teams will be ranked by
their score (in case of multiple teams get the same score, they will be ordered randomly), and the top
M teams from the group can advance to the next stage.
Our questions are: What is the maximum possible score that a team can earn and still not advance?
(Note that there may be other teams in the same group that also earn that same score and do advance.)
Similarly, what is the minimum possible score that a team can earn and still advance?
Input
The first line of the input gives the number of test cases, T. T cases follow. Each case has two lines.
The first line contains two numbers, N and M. The second line contains three numbers, A, B, and C.
Output
For each test case, output one line containing ‘Case #x: y z’, where x is the test case number (starting
from 1) and y is maximum score that a team may be eliminated. z is the minimum score that a team
may advance to the next stage.

 

题目大意:一场比赛,赢的得A分,输的得C分,平手都得B分。有n支队伍,分别比赛一次,选前m个队伍晋级(分数相同的随机排名)。问:可能的最大落榜分数,可能的最小晋级分数。

思路:首先若A<C,交换A和C。

贪心地让分数都尽量集中到前m+1个队伍身上,那么就要后n-m-1个队伍都给m+1个队伍最多的分数,即max{A, B}。

那么前m+1个队伍的竞技中,希望分数尽量地平均,则要赢一场便输一场,或者全部平手,则有floor(m/2)场得分为max{A + C, B + B}。

若m为奇数,那么最后一场要有一半的队伍获胜,或全部平手,此时最低分数为max{B, C}。


同样,考虑第二个问题,贪心得让后n-m+1的队伍分数都尽量少,那么久要前m-1个队伍给后n-m+1个队伍最少的分数,即min{B, C}。

那么后n-m+1个队伍的竞技中,也是希望分数尽量地平均,则要赢一场便输一场,或者全部平手,则有floor((n-m)/2)场得分为min{A + C, B + B}。

只有这样才可以尽量的使该队得分低.

若n-m为奇数,那么最后一场要有一半的队伍获胜,或全部平手,此时最高分数为min{A, B}。//因为要该队是要晋级的队伍所以要么赢要么平局



这题是一个很棒的题目,值得思考,以后做题要耐得住烦躁.

#include<iostream>
#include<cstring>
#include<cstdio>

using namespace std;

long long sum1=0,sum2=0;
long long n,m,a,b,c;
int Sum1()
{  //保证m+1个数尽可能一样大
	sum1=max(a,b)*(n-m-1);  //让n-m-1个数尽可能小,即该队赢了或打平了n-m-1个队
	sum1+=(m/2)*max(a+c,b+b);
	if(m%2==1)
	sum1+=max(b,c);
}
int Sum2()
{
	sum2=min(c,b)*(m-1);//该队输给或打平了m-1个队
	sum2+=min(a+c,b+b)*((n-m)/2);
	if((n-m)%2==1)
		sum2+=min(a,b);
}
int main()
{
	int T;
	while(~scanf("%d",&T))
	{
		int Case=0;
		while(T--)
		{
			Case++;
			scanf("%lld%lld",&n,&m);
			scanf("%lld%lld%lld",&a,&b,&c);
			if(a<c)
			swap(a,c);
			Sum1();
			Sum2();
			printf("Case #%d: %lld %lld\n",Case,sum1,sum2);
		}
	}
}


 

你可能感兴趣的:(UVALive 7147 思维题(贪心))