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. WebService基于SoapHeader实现安全认证(一)

    本文转载:http://www.cnblogs.com/houleixx/archive/2009/08/22/webservice-soapheader-security.html WebServi ...

  2. pcap的pcap_dump()保存的文件格式

    (2009-09-01 20:36:49) 转载▼ 标签: 杂谈 分类: 专业 首先是tcpdump文件格式 当你在Windows或者Linux环境下用tcpdump命令抓取数据包时,你将得到如下格式 ...

  3. 使用C#WebClient类访问(上传/下载/删除/列出文件目录)由IIS搭建的http文件服务器

    前言 为什么要写这边博文呢?其实,就是使用C#WebClient类访问由IIS搭建的http文件服务器的问题花了我足足两天的时间,因此,有必要写下自己所学到的,同时,也能让广大的博友学习学习一下. 本 ...

  4. 给EditText的drawableRight属性的图片设置点击事件 分类: 学习笔记 android 2015-07-06 13:20 134人阅读 评论(0) 收藏

    这个方法是通用的,不仅仅适用于EditText,也适用于TextView.AutoCompleteTextView等控件. Google官方API并没有给出一个直接的方法用来设置右边图片的点击事件,所 ...

  5. Java Stax操作XML简介

    使用stax操作xml 非常的简单,它的读取过程像是一个光标在移动.针对不同的节点做不同的处理. 先看一个基于光标的模型处理xml: public class StaxTest { @Test pub ...

  6. 19个非常有用的Javascript类库

    Blackbird是一款非常酷的JavaScript调试工具,带有一个漂亮的界面显示和过滤调试信息. http://www.gscottolson.com/blackbirdjs/ Treesaver ...

  7. U3D 抛物线的方法

    本文转载:http://www.manew.com/thread-44642-1-1.html 无论是愤怒的小鸟,还是弓箭发射功能,亦或者模拟炮弹受重力影响等抛物线轨迹,都可以使用本文的方法,模拟绝对 ...

  8. C#中Hashtable、Dictionary详解以及写入和读取对比

    转载:http://www.cnblogs.com/chengxingliang/archive/2013/04/15/3020428.html 在本文中将从基础角度讲解HashTable.Dicti ...

  9. 容易被忽略的两个方法:onSaveInstanceState()和onRestoreInstanceState()

    onSaveInstanceState()和onRestoreInstanceState()两个方法,在Activity中是比较容易忽视的方法,但是不得不说还是比较好用的方法,onSaveInstan ...

  10. 如何在ASP.NET 项目中使用Silverlight页面

    闲来无事,想写个网站玩玩,比较懒,不想写太多的样式来美化,看中了Silverlight,样式布局比较省事,但是又不想全部都用Silverlight 来写,所以才有此一文. 其实Silverlight最 ...