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 思路:
随机推荐
- Luogu P2735 电网【真·计算几何/Pick定理】By cellur925
题目传送门 刷USACO偶然遇到的,可能是人生中第一道正儿八经的计算几何. 题目大意:在平面直角坐标系中给你一个以格点为顶点的三角形,求三角形中的整点个数. 因为必修5和必修2的阴影很快就想到了数学中 ...
- CF767C Garland 【树形dp】By cellur925
一句话题意:给定一个树,树有点权,要求把树的某些边删去,使树变成三个部分,每部分点权值和相等. 我们很容易想到,再读入的时候记录所有点的点权之和,点权除以3是最后权值相等的值.如果不能整除3一定无解, ...
- 《开源自主OdooERP部署架构指南》试读:第二章数据库服务构建
文/开源智造联合创始人老杨 本文来自<开源自主OdooERP部署架构指南>的试读章节.书籍尚未出版,请勿转载.欢迎您反馈阅读意见. 使用apt.postgresql.org 您可以选择使用 ...
- Ubuntu install and uinstall
一.Ubuntu中软件安装方法 1.APT方式 (1)普通安装:apt-get install softname1 softname2 -; (2)修复安装:apt-get -f install so ...
- XOR and Favorite Number Codeforces - 617E || [CQOI2018]异或序列
https://www.luogu.org/problemnew/show/P4462 http://codeforces.com/problemset/problem/617/E 这个是莫队裸题了吧 ...
- ACM牛人博客
ACM牛人博客 kuangbin kuangbin(新) wuyiqi wuyiqi(新) ACM!荣耀之路! 九野的博客 传说中的ACM大牛!!! read more
- 题解报告:hdu 1406 完数
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1406 Problem Description 完数的定义:如果一个大于1的正整数的所有因子之和等于它的 ...
- 准确计算CoreText高度的方法:
- (int)getAttributedStringHeightWithString:(NSAttributedString *) string WidthValue:(int) width { ; ...
- 在Windows上部署Zabbix客户端
将Zabbix for Windows客户端拷贝到windows系统的c盘,修改配置文件的相关配置项后,打开cmd窗口执行: # 安装服务 c:\zabbix\bin\win32\zabbix_age ...
- 等待进程结束函数中的BUG
偶然发现一个BUG,有一个函数是这样写的: void WaitProcExit(DWORD dwPid) { HANDLE hProcess = OpenProcess(PROCESS_ALL_ACC ...