标签:des blog class code ext get
Your favorite football team is playing a charity tournament, which is part of a worldwide fundraising12
这个题题意是给出一系列比赛和结果,可以花钱买任意一场比赛或几场比赛的进球(这尼玛是反黑之前的中超联赛吧。。),问买完后最多能得多少分。。因为平局才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
标签:des blog class code ext get
原文地址:http://blog.csdn.net/mig_davidli/article/details/25008529