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

news/2024/7/4 22:17:32

主题链接:http://acm.hdu.edu.cn/showproblem.php?

pid=3177


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
HDU 2009-10 Programming Contest


题意:

向一个容量为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;
}


转载于:https://www.cnblogs.com/yxwkf/p/5052090.html


http://www.niftyadmin.cn/n/4136292.html

相关文章

怎样用python打开文件_你真的会用python进行文件操作吗

本篇文章主要比较系统的介绍了python中文件操作&#xff0c;以及在在使用中需要注意的问题。 什么是文件 文件是系统存储区域的一个命名位置&#xff0c;用来存储一些信息&#xff0c;便于后续访问。能够在非易失性存储器中实现持续性存储&#xff0c;比如在硬盘上。当我们要读…

2017年全球创新公司琅琊榜及10条成功启示录

导读&#xff1a;每年年初&#xff0c;FastCompany都会公布他们评出的最有创新力的10个公司榜单&#xff0c;今年是这个榜单公布的第十年。 今年的榜单上&#xff0c;除了谷歌、亚马逊等大公司&#xff0c;也不乏一些从小的方向切入市场的公司让人印象深刻。此外&#xff0c;中…

总结c++类的构造函数 拷贝构造函数 析构函数 赋值运算符重载的特点以及函数调用顺序

对 c类的构造函数 拷贝构造函数 析构函数 赋值运算符重载 相关知识的总结&#xff0c;并附上例子&#xff0c;希望对大家有帮助&#xff0c;有错误大家可以指出来 一 构造函数 1 构造函数&#xff1a; 构造函数时一个特殊的成员函数&#xff0c;用来初始化对象的数据成员&am…

2.1 线性表的逻辑结构与存储结构

在之前的数据结构知识铺垫2&#xff1a;物理结构与逻辑结构一文中, 我们介绍了物理结构与逻辑结构, 物理结构即存储结构. 本篇文章我们着重探讨一下线性表的逻辑结构与存储结构. 1. 线性表的逻辑结构 图1. 线性表的逻辑结构 线性表是具有相同特性的数据元素的有限序列, 每个元…

springMVC初探视图解析器——XmlViewResolver

XmlViewResolver解析器 XmlViewResolver基于XML文件中的视图bean来解析“逻辑视图”。XmlViewResolver默认会从/WEB-INF/views.xml中加载视图bean&#xff0c; 当然你也可以自己设置该xml文件的位置&#xff0c;该解析器有个属性“location”可设置xml位置 当处理器返回“逻辑视…

python装饰器class_python的装饰器

捋了一遍又一遍&#xff0c;终于对装饰器有了一点点的认识 基本的装饰器长这样&#xff1a; defadd_news(func):def new_func(*args, **kwargs):print("这是新添加的内容")return func(*args, **kwargs)return new_func add_news def my_func(): print("----som…

c++继承知识总结

c继承相关知识总结 一 继承关系&#xff1a;public protected private 不矫情 直接贴代码 举例代码主要从&#xff1a; a. 基类成员对其对象的可见性&#xff1a; 只有public成员可以访问 b. 基类成员对派生类的可见性&#xff1a; c. 基类成员对派生类对象的可见性&#xf…

python字符串小数转化整数_Python字符串、整数、和浮点型数相互转换实例

前言序锦 在编程中&#xff0c;经常要用到字符串的相互转换&#xff0c;现在在这里记录一下Python里面的字符串和整数以及浮点型数之间是如何进行相互转换的。 int(str)函数将符合整数的规定的字符串转换成int型的 float(str)函数将符合浮点型的规定的字符串转换成float型的 st…