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. ExtJS笔记--applyTo和renderTo的差别

    extjs中常常会用到renderTo或applyTo配置选项.这里,我就比較下两者的差别与使用方法.1.renderTo与render方法相应2.applyTo与applyToMarkup方法相应 ...

  2. oracle3

    查看表结构 DESC emp; 查询所有列 SELECT * FROM dept; 切忌动不动就用select * set timing on; 打开显示操作时间的开关,在下面显示查询时间. CREA ...

  3. struts2操作pojo之小工程struts2ActionPOJO

    下面的源码和操作步骤依据java web整合开发王者归来第16章,16.7 Action中使用POJO:p464 pojo:就是javabean的意思,下面就是struts2操作javabean代码过 ...

  4. group by是什么意思 mysql中

    mysql语法中group by是什么意思? 在百度中搜索半天,最后找到一篇解释比较好的(不是博文,是百度知道,很郁闷那么多网友怎么就没人解释的清楚),链接如下: http://zhidao.baid ...

  5. ASP.NET Web API(二):安全验证之使用HTTP基本认证

    在前一篇文章ASP.NET Web API(一):使用初探,GET和POST数据中,我们初步接触了微软的REST API: Web API. 我们在接触了Web API的后就立马发现了有安全验证的需求 ...

  6. ACCESS表与CSV文件相互导入、导出的SQL语句

    一.将ACCESS表导出为CSV文件:Select * INTO [TEXT;FMT=CSV;DELIMITED;HDR=YES;DATABASE=E:\temp\].test.csv FROM Sh ...

  7. mvc5 + ef6 + autofac搭建项目(repository+uow)(二)

    续上篇: DBContext 在上篇 图一类库根目录创建的 DbContextBase /// <summary> /// 数据库上下文基类 /// </summary> // ...

  8. iOS 正则表达式-判断邮箱、手机号

    判断是否是邮箱 -(BOOL)isValidateEmail:(NSString *)email { NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[ ...

  9. iOS 知识-常用小技巧大杂烩

    原文链接:http://www.jianshu.com/p/7c3ee5e67d03. 自己看的. 1,打印View所有子视图 po [[self view]recursiveDescription] ...

  10. 用原生js实现一个页面乘法口诀表

    今天我自己用js实现了一个页面乘法口诀表(如图)来共享给大家,做的不是很好,如果大家有新的想法可以跟我交流哦. 代码如下: <!doctype html><html lang=&qu ...