题意:

给定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. 在代码里更新autolayout布局

    //遍历view约束(高,宽) NSArray* constrains = self.View.constraints; for (NSLayoutConstraint* constraint in ...

  2. poj 2083 Fractal 递归 图形打印

    题目链接: http://poj.org/problem?id=2083 题目描述: n = 1时,图形b[1]是X n = 2时,图形b[2]是X  X        X               ...

  3. 水题 Gym 100553K Knockout Racing

    题目传送门 /* 题意:有若干个点在一个区间内来回移动,1m/s. 水题:n^2的复杂度能解决,注意时间可能大于一个周期,要取模 */ #include <cstdio> #include ...

  4. synchronized(4)修饰语句块之:synchronized(this)

    synchronized(this) 此时,线程获得的是对象锁.例如: public class Thread8 extends Thread { public void sync_fun() { s ...

  5. Android Dialogs(5)[正常显示dlg,将Fragment显示为dialog,将Aty显示为dlg,嵌入],关闭Dialog

    Showing a Dialog When you want to show your dialog, create an instance of your DialogFragment and ca ...

  6. 08 H5新增input元素

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  7. 支付宝添加scheme的方法

    点击项目名称,点击“Info”选项卡,在“URL Types”选项中,点击“+”,在“URL Schemes”中输入“myAlipay”.“myAlipay”来自于文件“APViewControlle ...

  8. 187 Repeated DNA Sequences 重复的DNA序列

    所有DNA由一系列缩写为A,C,G和 T 的核苷酸组成,例如:“ACGAATTCCG”.在研究DNA时,识别DNA中的重复序列有时非常有用.编写一个函数来查找DNA分子中所有出现超多一次的10个字母长 ...

  9. C. Mahmoud and a Message dp + 暴力

    http://codeforces.com/contest/766/problem/C 关键在于dp,如何计数. 设dp[i]表示前i个字母中,能分成多少份合法的情况.那么答案就是dp[n],其中dp ...

  10. Android学习备忘笺02Fragment

    Android中Fragment可以将UI界面分成多个区块,一般静态或动态添加Fragment. 01.新建Fragment实例 一个Fragment实例包括两个部分:类对象和布局文件(可视化部分). ...