Crixalis's Equipment

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2795    Accepted Submission(s): 1141

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
 
Recommend
lcy
 

贪心.

贪心策略:当前放的物品所需的空间 Bi 和 下一个需要放的物品所占用空间 Aj 之和是否大于 当前物品所占用空间 Ai 和下一个物品所需空间 Bi 之和.

也就是(Bi+Aj)>(Bj+Ai)的话 就先放物品i 否则 先放物品j

需要空间大的先放.

可以用一组案例来思考一下这个贪心策略:

20 3

10 20

3 4

7 7

___________

代码如下:

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define MAX 1001
struct node
{
int a;
int b;
};
struct node x[MAX];
int v,n;
bool cmp(struct node x,struct node y)
{
if(x.b==y.b) return x.a>y.a;
return (x.b-x.a)>(y.b-y.a);
}
void init()
{
memset(x,,sizeof(x));
}
void read()
{
int i;
scanf("%d %d",&v,&n);
for(i=;i<n;i++)
scanf("%d %d",&x[i].a,&x[i].b);
sort(x,x+n,cmp);
}
void cal()
{
int i;
int res=v;
for(i=;i<n;i++)
{
if(res<||res<x[i].b)
{
res-=x[i].b;
break;
}
res-=x[i].a;
}
if(res>=) printf("Yes\n");
else printf("No\n");
}
void solve()
{
init();
read();
cal();
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
solve();
}
return ;
}

Hdu 3177 Crixalis's Equipment的更多相关文章

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

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

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

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

  3. HDU ACM 3177 Crixalis's Equipment

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

  4. hdu 3177 Crixalis&#39;s Equipment

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

  5. HDU 3177 Crixalis&#39;s Equipment(贪婪)

    主题链接:http://acm.hdu.edu.cn/showproblem.php? pid=3177 Problem Description Crixalis - Sand King used t ...

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

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

  7. HDOJ 3177 Crixalis&#39;s Equipment

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

  8. hdu---3177 Crixalis's Equipment 根据 两个元素 之间的权衡进行排序

    Crixalis's Equipment Problem Description Crixalis - Sand King used to be a giant scorpion(蝎子) in the ...

  9. HD-ACM算法专攻系列(23)——Crixalis's Equipment

    题目描述: AC源码:此次考察贪心算法,解题思路:贪心的原则是使留下的空间最大,优先选择Bi与Ai差值最大的,至于为什么?这里用只有2个设备为例,(A1,B1)与(A2,B2),假设先搬运A1,搬运的 ...

随机推荐

  1. web安全培训笔记

    1.漏洞获取方法 1)扫描器扫描 2)乌云 3)线上服务漏洞 样例: 线上crm->管理员弱口令->后台上传头像处漏洞->上传php文件->进入内网->扫描内网拓扑-&g ...

  2. 和菜鸟一起学linux内核源码之基础准备篇 系列 体系结构图

    http://blog.csdn.net/eastmoon502136/article/details/8711104

  3. javascript、jsp

    1.javascript:简称js 在<body>与 </body>之间 加入<script> </script>即可 最好在<head>与 ...

  4. static inner class 什么时候被加载

    一直认为在加载outer class 的同时也会加载inner class 并且完成静态变量和代码块的初始化,今天在维基百科上面看到 “The static class definitionLazyH ...

  5. Linux下安装并破解StarUML

    下载 官网地址: http://starumlreleases-7a0.kxcdn.com/v2.7.0/StarUML-v2.7.0-64-bit.deb CSDN: http://download ...

  6. PS之放射背景

    效果图 素材 新建图层,填充颜色 新建图层,矩形工具画条形 滤镜-扭曲-极坐标 合并图层,效果如下 新建图层,画一个适当的圆 滤镜-模糊-高斯模糊 将素材人物抠出来放在中间

  7. eAccelerator介绍

    加速器 eAccelerator是一个自由开放源码php加速器,优化和动态内容缓存,提高了php脚本的缓存性能,使得PHP脚本在编译的状态下,对服务器的开销几乎完全消除. 它还有对脚本起优化作用,以加 ...

  8. jQuery获取radio选中项的值【转藏】

    <title></title> <script src="js/jquery-1.7.2.min.js"></script> < ...

  9. 关于HMTL -[HTML5]

    前言: 为什么学习它?(HTML5) 会不会HTML5,其实并不会影响我的开发效率,我觉得终究还是跟个人性格有关,我喜欢前沿的东西.这就好比我大学里学的计算机,但我仍然会去看一些跟专业不相关的书籍一样 ...

  10. O-C相关04:类方法的概述与定义和调用

    类方法的概述与定义和调用 1, 类方法的概述 类方法(class method)在其他编程语言中常常称为静态方法(例如 Java 或 C# 等). 与实例方法不同的是,类方法只需要使用类名即可调用, ...