主题链接:http://acm.hdu.edu.cn/showproblem.php?

pid=3177

Problem Description
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 living
underground 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.
 
Input
The first line contains an integer T, indicating the number of test cases. Then follows T cases, each one contains N + 1 lines. The first line contains 2 integers: V, volume of a hole and N, number of equipment respectively. The next N lines contain N pairs
of integers: Ai and Bi.

0<T<= 10, 0<V<10000, 0<N<1000, 0 <Ai< V, Ai <= Bi < 1000.
 
Output
For each case output "Yes" if Crixalis can move all his equipment into the new hole or else output "No".
 
Sample Input
2 20 3
10 20
3 10
1 7 10 2
1 10
2 11
 
Sample Output
Yes
No
 
Source

题意:

向一个容量为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&#39;s Equipment(贪婪)的更多相关文章

  1. hdu 3177 Crixalis&#39;s Equipment

    Crixalis's Equipment Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  2. 杭电 3177 Crixalis&#39;s Equipment

    http://acm.hdu.edu.cn/showproblem.php? pid=3177 Crixalis's Equipment Time Limit: 2000/1000 MS (Java/ ...

  3. HDOJ 3177 Crixalis&#39;s Equipment

    Crixalis's Equipment Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  4. Hdu 3177 Crixalis's Equipment

    Crixalis's Equipment Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  5. 【hdu 3177 Crixalis's Equipment】 题解

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3177 \(describe\): 有一个山洞,山洞的容积最大为\(v\).现在你有\(n\)个物品,这 ...

  6. HDU 3177 Crixalis's Equipment (贪心,差值)

    题意:判断 n 件物品是否可以搬进洞里,每件物品有实际体积A和移动时的额外体积 B . 析:第一反应就是贪心,一想是不是按B从大到小,然后一想,不对,比如体积是20,第一个 是A=11, B=19.第 ...

  7. HDU ACM 3177 Crixalis's Equipment

    Crixalis's Equipment Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  8. hdu 3966 Aragorn&#39;s Story(树链剖分+树状数组)

    pid=3966" target="_blank" style="">题目链接:hdu 3966 Aragorn's Story 题目大意:给定 ...

  9. HDU 3966 Aragorn&#39;s Story(树链剖分)

    HDU Aragorn's Story 题目链接 树抛入门裸题,这题是区间改动单点查询,于是套树状数组就OK了 代码: #include <cstdio> #include <cst ...

随机推荐

  1. Revit二次开发之绘制钢筋

    第一次在博客园上写东西,也不知道该写些什么,我想就写点最近项目到遇到的问题吧. 最近在做一个小项目,具体需求大概是在一个revit模型中的对应的楼板位置绘制钢筋. 由于刚接触Revit二次开发,之前也 ...

  2. 【虚拟化实战】容灾设计之三Stretched Cluster

    作者:范军 (Frank Fan) 新浪微博:@frankfan7 Stretched Cluster是一把双刃剑,会用的如行云流水,用不好反而受其限制. 传统的vSphere Cluster是指一个 ...

  3. 浅解ARC中的 __bridge、__bridge_retained和__bridge_transfer

    文章来源:http://www.outflush.com/2015/03/introduction-of-arc-bridge-type-transfer/ 在对 bridge 相关的修饰符解说前.首 ...

  4. JavaScript权威指南科20章 client记忆

    20 client记忆 client几种形式存储的: web记忆 cookie IE userData 离线应用 web数据库 文件系统api 20.1 localStorage 和 sessionS ...

  5. SQL ---指令实例语句

    1 1 create database+数据库名字 创建数据库 2 2 create table+表的名字 创建表 3 表中的操作: 4 3 insert into 表名 (列名1,列名2··)val ...

  6. Java深入解析读书笔记(一)

    1. goto,const为java的两个保留关键字,无任何应用语法.因此从不使用.   goto 使用循环标签:if,break out,here实现goto的功能. 2. 标识符:可由字母数字下划 ...

  7. 使用GDI+绘制的360风格按钮控件(使用CN_DRAWITEM消息重绘,并使用TGPGraphics,TGPPen,TGPImage,TGPBitmap等)good

    将下面的代码拷贝到一个单元中,创建一个包,加入这个单元后安装.使用的时候设置好背景颜色,边框颜色,图标(png格式)相对路径的文件名称.这个控件可以利用PNG图像的颜色透明特性,背景色默认透明度为50 ...

  8. 网络知识汇总(2) - Linux下如何修改ip地址

    在Linux的系统下如何才能修改IP信息   以前总是用ifconfig修改,重启后总是得重做.如果修改配置文件,就不用那么麻烦了-   A.修改ip地址   即时生效:   # ifconfig e ...

  9. CSS——inline-block属性

    Inline-block 是元素 display属性的一个值 .这个名字的由来是因为,可以简单的解释为inline+block :display设置这个值的元素,兼具行内元素( inline elem ...

  10. WebBrowser控件禁用超链接转向、脚本错误提示、默认右键菜单和快捷键

    原文:WebBrowser控件禁用超链接转向.脚本错误提示.默认右键菜单和快捷键 WebBrowser控件禁用超链接转向.脚本错误提示.默认右键菜单和快捷键从 VS2005开始,VS自带的 WebBr ...