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 ...
随机推荐
- [精读]Spationtemporal Saliency Detection Using Textural Contrast and Its Applications
Spationtemporal Saliency Detection Using Textural Contrast and Its Applications Last Edit 2013/12/3 ...
- Qt制作应用插件
在Qt下,插件有两种形式,一种是用于QtCreator下,扩展IDE功能.另一种是用于扩展开发者的应用.本文要讲的是后者. 定义一个纯虚类作为插件接口 #include <QtPlugin> ...
- pathload --有效的网络带宽估计方法
上一篇博客简述了现行的带宽估计的方法,分类,以及一些问题. 见:http://blog.csdn.net/ice110956/article/details/11071969 上文列出了13种现行的方 ...
- 利用开源HTML5引擎lufylegend.js结合javascript实现的五子棋人机对弈
前言 本文主要介绍利用开源引擎 lufylegend.js开发基于Html5的游戏--五子棋,主要叙述其详细开发过程. 游戏规则 玩过五子棋的都应该知道五子棋的规则,这里就简单介绍其规则. 1 ...
- 框架基础JNI
转载请标明出处: http://blog.csdn.net/yujun411522/article/details/46342793 本文出自:[yujun411522的博客] 2.1 概述 JNI ...
- RSA密码系统 基于大数环境编写 密码学课程设计
RSA密码系统的实现 1.问题描述 RSA密码系统可具体描述为:取两个大素数p和q,令n=pq,N=(p-1)(q-1),随机选择整数d,满足gcd(d,N)=1,ed=1 modN. 公开密钥:k1 ...
- [Windows Phone]修改应用程序主题
问题: Windows Phone模拟器默认情况下是黑色(Dark)主题,开发者往往都是在黑色主题下进行应用开发,加入自定义的颜色,样式等等,而当把手机操作系统主题设为白色(Light)主题之后会发现 ...
- 解压tar.gz文件报错gzip: stdin: not in gzip format解决方法
解压tar.gz文件报错gzip: stdin: not in gzip format解决方法 在解压tar.gz文件的时候报错 1 2 3 4 5 [Sun@localhost Downloads] ...
- [android]APP启动界面——SplashActivity
概念 当前应用程序在启动的时候都会有一个展示自己公司LOGO和APP名字的界面.这个界面成为SplashActivity. 布局 <? xml version="1.0" e ...
- 【ArcGIS 10.2新特性】ArcGIS 10.2 for Server新特性
ArcGIS forServer相关的很多重大消息,如与Portal for ArcGIS进行了集成,提供对实时数据支持.离线地图使用. 1.与Portal for ArcGIS集成 用户能够配置Po ...