HDU 3177 Crixalis's Equipment(贪心算法)

解决蝎子Crixalis在搬家时如何将装备成功存入有限空间洞穴的问题,采用贪心算法进行优化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Crixalis's Equipment

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1500    Accepted Submission(s): 615


Problem Description
HDU3177-Crixaliss Equipment - 天羽 - 天羽的博客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

 
题意:蝎子要将一些装备装进洞内,每件装备占空间体积为ai,搬运过程中还需要bi的空间来移动,问这些装备能否全部搬到洞内。
 
思路:用贪心算法,假设有两件装备A:a1,b1;B:a2,b2。若先搬A,则搬运B的瞬间占用空间最大为a1+b2;若先搬B,则搬运A的瞬间占用空间最大为a2+b1。要使剩下空间尽量大,则要选min(a1+b2,a2+b1),若先搬的是A,则要满足a1+b2<a2+b1,即b1-a1>b2-a2。
由此得贪心策略,每次选择搬运体积与装备体积之差最大的来搬运。
 
AC代码如下:
#include<iostream>
#include<algorithm>
using namespace std;

struct equipment
{
    int ai,bi;
    int ci;
}e[10001];
bool cmp(equipment a,equipment b)
{
    return a.ci>b.ci;
}
int main()
{

   int t,V,N;
   cin>>t;
   while(t--)
   {

       int flag=1;
       cin>>V>>N;
       for(int i=0;i<N;i++)
      {cin>>e[i].ai>>e[i].bi;
      e[i].ci=e[i].bi-e[i].ai;}
      sort(e,e+N,cmp);
      for(int i=0;i<N;i++)
      {
          if(e[i].bi>V)
         {
         flag=0;
          break;
         }
         V-=e[i].ai;
      }
      if(flag)
      cout<<"Yes"<<endl;
      else
      cout<<"No"<<endl;
   }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值