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 思路:
随机推荐
- 原生JavaScript之深度克隆
先看一下克隆成功后的结果 深度克隆就是将obj的属性克隆到obj1上面,并且在obj上面修改属性不影响obj1上面的属性. 1.先把所有的值都遍历一遍(看是引用值和原始值)用for ( var pro ...
- docker 中部署一个springBoot项目
docker 中部署一个springBoot项目 (1)介绍 springBoot项目 1.项目结构 2.pom.xml <?xml version="1.0" encodi ...
- javascript监听值变化
最近面了有道前端实习,人品爆发一面过了.但是在一面中面试官问了一个问题”MVC用javascript如何实现?如果实现双向绑定?值变化监听?“ 当时我回答”js好像没函数进行值变化监听“ 后来想想很不 ...
- Windows软件推荐
本篇博文主要记录一些实用性的windows软件或者插件,重在积累! 工具类 1.截图软件 https://zh.snipaste.com/ Snipaste 是一个简单但强大的截图工具,也可以让你将截 ...
- Linux tcpdump命令英文文档
https://www.computerhope.com/unix/tcpdump.htm
- Use Power bi Mobile Show SSRS 2016 Mobile Report;使用 Power BI Mobile 查阅ssrs2016 mobile report
使用 power bi mobile 查阅 ssrs 2016 mobile report 很简单,以下是IOS客户端的演示. 系统自带了演示数据,包含power bi 的和 ssrs mobile ...
- 在 c#中 如何 重新激活一个控件
比如toolBar是一个组合控件 this.toolBar.CaptionHeight =this.toolBar.Items.Count * 60;//重新激活toolBar控件 CaptionHe ...
- 在spring data jpa中使用自定义转换器之使用枚举转换
转载请注明http://www.cnblogs.com/majianming/p/8553217.html 在项目中,经常会出现这样的情况,一个实体的字段名是枚举类型的 我们在把它存放到数据库中是需要 ...
- ASP.NET URLRewriter重写
URLRewriter重写是微软官方出的第三方重写插件 下载地址:http://download.csdn.net/detail/ysn1314/5421587 下载后在项目中添加引用,然后再配置文件 ...
- HTML5 File API的应用
HTML5 File API简介 HTML5File API协议族 Directories and System 文件系统和目录读取 FileWriter 写入文件 FileReader ...