HDU 3177 Crixalis's Equipment(贪婪)
主题链接:http://acm.hdu.edu.cn/showproblem.php?
pid=3177
Crixalis - Sand King used to be a giant scorpion(蝎子) in the deserts of Kalimdor. Though he's a guardian of Lich King now, he keeps the living habit of a scorpion like livingunderground and digging holes.
Someday Crixalis decides to move to another nice place and build a new house for himself (Actually it's just a new hole). As he collected a lot of equipment, he needs to dig a hole beside his new house to store them. This hole has a volume of V units, and Crixalis
has N equipment, each of them needs Ai units of space. When dragging his equipment into the hole, Crixalis finds that he needs more space to ensure everything is placed well. Actually, the ith equipment needs Bi units of space during the moving. More precisely
Crixalis can not move equipment into the hole unless there are Bi units of space left. After it moved in, the volume of the hole will decrease by Ai. Crixalis wonders if he can move all his equipment into the new hole and he turns to you for help.
of integers: Ai and Bi.
0<T<= 10, 0<V<10000, 0<N<1000, 0 <Ai< V, Ai <= Bi < 1000.
2 20 3
10 20
3 10
1 7 10 2
1 10
2 11
Yes
No
题意:
向一个容量为V的洞中放如入物品,每件物品有一个停放体积和一个可移动体积。问是否能放下全部的物品?
思路:—— 贪心
停放体积 移动体积
第一件物品 a1 b1
第二件物品 a2 b2
如果这两件物品的移动体积都不大于洞的体积V
那么将单独比較两个物品的时候会发现: a1+b2为先放第一件物品,后放第二件物品的最大瞬时体积
a2+b1为先放第二件物品。后放第一件物品的最大瞬时体积
那么我们就应该选择a1+b2和a2+b1中比較小的先放。
那么从2件物品。扩展到n件物品 如果n件物品的移动体积都不大于洞的体积V,
将N件物品依照a1+b2 < a2+b1进行排序,(事实上就是依照差值的大小)然后依次放入洞中。
PS:
假设仅仅是单纯的依照Bi从大到小排序的话为WA;
看看这个案例就明确了。
21 2
8 18
1 20
Yes
代码例如以下:
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;
struct num
{
int a, b;
} p[1017];
bool cmp(num A, num B)
{
return A.b-A.a > B.b-B.a;
}
int main()
{
int t;
int v, n;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&v,&n);
for(int i = 0; i < n; i++)
{
scanf("%d %d",&p[i].a,&p[i].b);
}
sort(p,p+n,cmp);
int i;
for(i = 0; i < n; i++)
{
if(v >= p[i].b)
{
v-=p[i].a;
}
else
break;
}
if(i == n)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
HDU 3177 Crixalis's Equipment(贪婪)的更多相关文章
- hdu 3177 Crixalis's Equipment
Crixalis's Equipment Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- 杭电 3177 Crixalis's Equipment
http://acm.hdu.edu.cn/showproblem.php? pid=3177 Crixalis's Equipment Time Limit: 2000/1000 MS (Java/ ...
- HDOJ 3177 Crixalis's Equipment
Crixalis's Equipment Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- Hdu 3177 Crixalis's Equipment
Crixalis's Equipment Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- 【hdu 3177 Crixalis's Equipment】 题解
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3177 \(describe\): 有一个山洞,山洞的容积最大为\(v\).现在你有\(n\)个物品,这 ...
- HDU 3177 Crixalis's Equipment (贪心,差值)
题意:判断 n 件物品是否可以搬进洞里,每件物品有实际体积A和移动时的额外体积 B . 析:第一反应就是贪心,一想是不是按B从大到小,然后一想,不对,比如体积是20,第一个 是A=11, B=19.第 ...
- HDU ACM 3177 Crixalis's Equipment
Crixalis's Equipment Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- hdu 3966 Aragorn's Story(树链剖分+树状数组)
pid=3966" target="_blank" style="">题目链接:hdu 3966 Aragorn's Story 题目大意:给定 ...
- HDU 3966 Aragorn's Story(树链剖分)
HDU Aragorn's Story 题目链接 树抛入门裸题,这题是区间改动单点查询,于是套树状数组就OK了 代码: #include <cstdio> #include <cst ...
随机推荐
- alv行可编辑时带出描述
ALV显示可以编辑的状态下可以带出描述信息等,比如维护表程序输入公司代码时需要带出公司代码的描述,这时就需要通过下面事件来触发 定义一个类: CLASS lcl_event_receiver DEFI ...
- mysql5.6 主从配置
参考网址:http://www.cnblogs.com/zhoujie/p/mysql1.html http://kerry.blog.51cto.com/172631/277414/ 1.配置主库: ...
- linux c正则
c 正则 -------------------------------------------------- 标准的C和C++都不支持正则表达式,但有一些函数库可以辅助C/C++程序员完成这一 ...
- BZOJ 2096([Poi2010]Pilots-单调队列-差值)
2096: [Poi2010]Pilots Time Limit: 30 Sec Memory Limit: 162 MB Submit: 190 Solved: 97 [ Submit][ ...
- Mac OS X将CSV格式转换为Excel文档格式,Excel转CSV中文乱码问题
一:在Mac上假设你使用Excel打开windows导出的CSV格式文档.你会发现表格中全部的的内容都显示在A列. 那么,怎样恢复正常呢,你能够将CSV格式的文档导入到Excel文档中,这样就正常显示 ...
- 王立平--android中的anim(动画)
简单有用步骤: 1.新建anim目录. 2.在anim下新建xml文件, 3.在xml下编写自己须要动画. 简单样例: 给Imageview加入动画 public class MainActivity ...
- Swift - 滑块(UISlider)的用法
1,滑块的创建 1 2 3 4 5 6 var slider=UISlider(frame:CGRectMake(0,0,300,50)) slider.center=self.view.center ...
- 硬盘重装Ubuntu12.04的感受
好久没更blog了,最近这两天系统也出了问题,win7蓝屏,ubuntu进不去-.后来win7整好了,ubuntu依旧顽固.用惯了linux,就不想在转到win7下面了,估计是习惯了各种敲命令的感觉吧 ...
- 以对象管理资源——C++智能指针auto_ptr简介
auto_ptr是C++标准库提供的类模板,它可以帮助程序员自动管理用new表达式动态分配的单个对象.auto_ptr对象被初始化为指向由new表达式创建的对象,当auto_ptr对象的生命期结束时, ...
- 【矩阵乘】【NOI 2012】【cogs963】随机数生成器
963. [NOI2012] 随机数生成器 ★★ 输入文件:randoma.in 输出文件:randoma.out 简单对照 时间限制:1 s 内存限制:128 MB **[问题描写叙述] 栋栋近期迷 ...