Your favorite football team is playing a charity tournament, which is part of a worldwide fundraising
e ort to help children with disabilities. As in a normal tournament, three points are awarded to the
team winning a match, with no points to the losing team. If the game is drawn, each team receives one
point.
Your team played N matches during the rst phase of the tournament, which has just nished.
Only some teams, the ones with more accumulated points, will advance to the second phase of the
tournament. However, as the main objective of the tournament is to raise money, before the set of
teams that will pass to the second phase is determined, each team is allowed to buy additional goals.
These new goals count as normally scored goals, and may be used to alter the result of any of the
matches the team played.
Your team‘s budget is enough to buy up to G goals. Can you tell the maximum total number of
points your team can get after buying the goals, supposing the other teams will not buy any goals?
Input
The input le contains several test cases, each of them as described below.
The rst line contains two integers N (1 N 105
) and G (0 G 106
) representing respectively
the number of matches your team played and the number of goals your team can buy. Each of the next
N lines describes a match result with two integers S and R (0 S; R 100), indicating respectively
the goals your team scored and received on that match before buying goals.
Output
For each test case, output a line with an integer representing the maximum total number of points your
team can get after buying the goals.
Sample Input
2 1
1 1
1 1
3 2
1 3
3 1
2 2
4 10
1 1
2 2
1 3
0 4
Sample Output
4
6
12
这个题题意是给出一系列比赛和结果,可以花钱买任意一场比赛或几场比赛的进球(这尼玛是反黑之前的中超联赛吧。。),问买完后最多能得多少分。。因为平局才1分,胜利有3分,所以很自然的想到贪心策略。
1、首先赢得就不用管了。
2、平局尽量优先买赢了,因为只需要付出一个球的代价。
3、把输的比赛按净胜球排序,从小到大买,买不完拉倒。
然后就有了代码:
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;
class match
{
public:
int x;
int y;
};
bool cmp(match a,match b)
{
return (a.y-a.x)<(b.y-b.x);
}
match pack[100002];
int main()
{
int matchnum,buyscore;
while(cin>>matchnum>>buyscore)
{
int cnt=0;
int finalscore=0;
for(int i=0;i<matchnum;i++)
{
int tx,ty;
cin>>tx>>ty;
if(tx>ty)
{
finalscore+=3;
}
else
{
int p=cnt++;
pack[p].x=tx;
pack[p].y=ty;
}
}
sort(pack,pack+cnt,cmp);
//cout<<"shu+ping:"<<cnt<<endl;
for(int i=0;i<cnt;i++)
{
if(buyscore>=(pack[i].y-pack[i].x)+1)
{
finalscore+=3;
int jingshengqiu=pack[i].y-pack[i].x+1;
//cout<<"i.x"<<pack[i].x<<pack[i].y<<"jingsheeng:"<<jingshengqiu<<endl;
buyscore=buyscore-(jingshengqiu);
//cout<<"sheng "<<finalscore<<"qiu"<<buyscore<<endl;
}
else if(buyscore>=(pack[i].y-pack[i].x))
{
finalscore+=1;
int jingshengqiu=pack[i].y-pack[i].x;
buyscore-=jingshengqiu;
//cout<<"sheng "<<finalscore<<"qiu"<<buyscore<<endl;
}
}
cout<<finalscore<<endl;
}
return 0;
}
【贪心】UVALive 6530——Football,布布扣,bubuko.com
【贪心】UVALive 6530——Football
原文:http://blog.csdn.net/mig_davidli/article/details/25008529