UvaLive 4863 Balloons(贪心)
题意:
给定n个队伍, 然后A房间有a个气球, B房间有b个气球, 然后给出每个队伍所需要的气球数量和到A B房间的距离, 求把气球全部送到每个队伍的最短距离.
分析:
在气球充足的情况下, 那么我们对于每个队伍, 肯定是哪个房间近就取哪个房间的气球。
但是题目中气球的数目有限, 所以很有可能出现某个时刻第i个队伍到A比较近, 但是A没有气球了, 只能去B拿的这种情况。这样的损失就是他们的距离差。
所以猜想是先把到A和到B距离差较大的队伍先满足了, 这样就能降低损失, 有这种贪心的思想应该就能求出答案了。
#include <bits/stdc++.h>
using namespace std;
int n , a, b;
struct T{
int ned, da,db, dif;
friend bool operator<(T a, T b){//重载小于号 在sort中这样是降序
return a.dif > b.dif;
}
};
T team[];
int main(){
while(scanf("%d %d %d", &n, &a, &b) && n){
for(int i = ; i < n ;i ++){
scanf("%d %d %d", &team[i].ned,&team[i].da,&team[i].db);
team[i].dif = abs(team[i].da-team[i].db);
}
sort(team,team+n);//按到a 到b的差距来排序
int sum = ;
for(int i = ; i < n;i++){
int ned = team[i].ned;
if(team[i].da < team[i].db){//如果 到a房间距离比到b房间短, 就先去a取, 没有再去b取
int v = min(ned, a);
sum += v * team[i].da + (ned - v) * team[i].db;
a -= v; b -= (ned - v);
}
else{//如果 到b房间距离比到a房间短, 就先去b取, 没有再去a取
int v = min(ned,b);
sum += v * team[i].db + (ned - v) * team[i].da;
b -= v; a -= (ned - v);
}
}
printf("%d\n", sum);
}
return ;
}
UvaLive 4863 Balloons(贪心)的更多相关文章
- UVALive 4863 Balloons 贪心/费用流
There will be several test cases in the input. Each test case will begin with a line with three inte ...
- [Leetcode 452] 最少需要射出多少支箭Minimum Number of Arrows to Burst Balloons 贪心 重载
[题目] There are a number of spherical balloons spread in two-dimensional space. For each balloon, pro ...
- codeforces 725D . Contest Balloons(贪心+优先队列)
题目链接:codeforces 725D . Contest Balloons 先按气球数从大到小排序求出初始名次,并把名次排在第一队前面的队放入优先队列,按w-t-1值从小到大优先,然后依次给气球给 ...
- UVAlive 2911 Maximum(贪心)
Let x1, x2,..., xm be real numbers satisfying the following conditions: a) -xi ; b) x1 + x2 +...+ xm ...
- uvalive 2911 Maximum(贪心)
题目连接:2911 - Maximum 题目大意:给出m, p, a, b,然后xi满足题目中的两个公式, 要求求的 xp1 + xp2 +...+ xpm 的最大值. 解题思路:可以将x1 + x2 ...
- UVALive - 4225(贪心)
题目链接:https://vjudge.net/contest/244167#problem/F 题目: Given any integer base b ≥ 2, it is well known ...
- UVALive 4850 Installations 贪心
题目链接 题意 工程师要安装n个服务,其中服务Ji需要si单位的安装时间,截止时间为di.超时会有惩罚值,若实际完成时间为ci,则惩罚值为max{0,ci-di}.从0时刻开始执行任务,问惩罚值最大 ...
- CodeForces - 725D Contest Balloons 贪心
D. Contest Balloons time limit per test 3 seconds memory limit per test 2 ...
- UVALive - 6268 Cycling 贪心
UVALive - 6268 Cycling 题意:从一端走到另一端,有T个红绿灯,告诉你红绿灯的持续时间,求最短的到达终点的时间.x 思路:
随机推荐
- GDI+ 加载PNG图片
#include <GdiPlus.h>#pragma comment(lib, "GdiPlus.lib")using namespace Gdiplus; clas ...
- saltstack实战笔记
#运维管理工具 ansible #支持ssh,不需要客户端 saltstack #也是只是ssh,不需要客户端的 安装架构是,master /minion 安装salt的依赖模块 python zer ...
- Ubuntu 必装软件及安装教程
安装搜狗拼音输入法 因为sogou是基于fcitx的,所以先添加fcitx键盘输入法系统[系统默认是iBus].在终端中,输入命令将下载源添加至系统源(添加依赖). sudo add-apt-repo ...
- _bzoj2005 [Noi2010]能量采集
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2005 令F(i)表示i | gcd(x, y)的对数,f(i)表示gcd(x, y) = i ...
- 贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas
题目传送门 /* 题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步 贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0 ...
- git 标签
如果你达到一个重要的阶段,并希望永远记住那个特别的提交快照,你可以使用 git tag 给它打上标签. 比如说,我们想为我们的 runoob 项目发布一个"1.0"版本. 我们可以 ...
- 题解报告:hdu 2059 龟兔赛跑
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2059 Problem Description 据说在很久很久以前,可怜的兔子经历了人生中最大的打击—— ...
- AsyncTask官方教程-推荐用AsyncTask少用Thread
Using AsyncTask AsyncTask allows you to perform asynchronous work on your user interface. It perform ...
- iOS 实现复选框 checkbox --转
转自:http://www.cnblogs.com/ygm900/p/3468891.html -(void)checkboxClick:(UIButton *)btn{ btn.select ...
- TC 609DIV2(950)
Problem Statement Vocaloids Gumi, Ia, and Mayu love singing. They decided to make an album comp ...