码迷,mamicode.com
首页 > 其他好文 > 详细

【贪心】UVALive 6530——Football

时间:2014-05-06 18:40:06      阅读:337      评论:0      收藏:0      [点我收藏+]

标签:des   blog   class   code   ext   get   

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

标签:des   blog   class   code   ext   get   

原文地址:http://blog.csdn.net/mig_davidli/article/details/25008529

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!