题意:

给定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(贪心)的更多相关文章

  1. UVALive 4863 Balloons 贪心/费用流

    There will be several test cases in the input. Each test case will begin with a line with three inte ...

  2. [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 ...

  3. codeforces 725D . Contest Balloons(贪心+优先队列)

    题目链接:codeforces 725D . Contest Balloons 先按气球数从大到小排序求出初始名次,并把名次排在第一队前面的队放入优先队列,按w-t-1值从小到大优先,然后依次给气球给 ...

  4. UVAlive 2911 Maximum(贪心)

    Let x1, x2,..., xm be real numbers satisfying the following conditions: a) -xi ; b) x1 + x2 +...+ xm ...

  5. uvalive 2911 Maximum(贪心)

    题目连接:2911 - Maximum 题目大意:给出m, p, a, b,然后xi满足题目中的两个公式, 要求求的 xp1 + xp2 +...+ xpm 的最大值. 解题思路:可以将x1 + x2 ...

  6. UVALive - 4225(贪心)

    题目链接:https://vjudge.net/contest/244167#problem/F 题目: Given any integer base b ≥ 2, it is well known ...

  7. UVALive 4850 Installations 贪心

    题目链接  题意 工程师要安装n个服务,其中服务Ji需要si单位的安装时间,截止时间为di.超时会有惩罚值,若实际完成时间为ci,则惩罚值为max{0,ci-di}.从0时刻开始执行任务,问惩罚值最大 ...

  8. CodeForces - 725D Contest Balloons 贪心

              D. Contest Balloons          time limit per test 3 seconds         memory limit per test 2 ...

  9. UVALive - 6268 Cycling 贪心

    UVALive - 6268 Cycling 题意:从一端走到另一端,有T个红绿灯,告诉你红绿灯的持续时间,求最短的到达终点的时间.x 思路:

随机推荐

  1. 前缀+排序 HDOJ 4311 Meeting point-1

    题目传送门 题意:给一些坐标轴上的点,选一个点,使得其他点到该点曼哈顿距离和最小 分析:这题有很强的技巧性,直接计算每个点的曼哈顿距离和是不可行的.这里用到了前缀的思想,先对点按照x从左到右排序,p[ ...

  2. Android偏好设置(6)应用和监听各偏好参数

    Reading Preferences By default, all your app's preferences are saved to a file that's accessible fro ...

  3. SpringCloud开发学习总结(七)—— 声明式服务调用Feign(一)

    在实践的过程中,我们会发现在微服务架构中实现客户端负载均衡的服务调用技术Spring Cloud Ribbon<SpringCloud开发学习总结(四)—— 客户端负载均衡Ribbon> ...

  4. 关于python2.7的md5加密遇到的问题(TypeError: Unicode-objects must be encoded before hashing)

    https://blog.csdn.net/u012087740/article/details/48439559 import hashlib import sys def md5s(): m=ha ...

  5. .NET面试题解析(00)-系列文章索引

    .NET面试题解析(01)-值类型与引用类型 .NET面试题解析(02)-拆箱与装箱 .NET面试题解析(03)-string与字符操作 .NET面试题解析(04)-类型.方法与继承 .NET面试题解 ...

  6. ES6知识点汇总

    MDN镇楼: https://developer.mozilla.org/zh-CN/ 1.ES6新添加数据类型:symbol  -----------   https://developer.moz ...

  7. SpringMVC与请求控制器

    MVC设计模式 视图(View)      -对应组件:JSP或者HTML文件 控制器(controller) -对应组件:Servlet 模型(Model)   -对应组件:JavaBean MVC ...

  8. Spring data jpa中Query和@Query分别返回map结果集

    引用: http://blog.csdn.net/yingxiake/article/details/51016234 http://blog.csdn.net/yingxiake/article/d ...

  9. XamarinAndroid 自动绑定View变量

    Android 编程时我们少不了使用FindIdByView函数,在Xamarin Android开发时也需要如此.这个工作很无聊且烦人.在常规Android开发中,人们已经发明了一些方法免除这项工作 ...

  10. git ---合并和删除分支

    git merge  分支名 //合并子分支到当前分支 git branch -d 分支名//删除分支