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.2 20 3 10 20 3 10 1 7 10 2 1 10 2 11
Yes No
停放体积 移动体积
第一件物品 a1 b1
第二件物品 a2 b2
假设这两件物品的移动体积都不大于洞的体积V
那么将单独比较两个物品的时候会发现 a1+b2为先放第一件物品 后放第二件物品的最大瞬时体积
a2+b1为先放第二件物品 后放第一件物品的最大瞬时体积
我们应该选择a1+b2和a2+b1中比较小的先放那么从2件物品 扩展到N件物品
假设n件物品的移动体积都不大于洞的体积V(如果有大于的 那么结果必然是NO)
将N件物品按照b1-a1>b2-a2进行排序 然后依次放入洞中,即按照差值从大到小放入洞中
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
int a,b,c;
}s[1005];
int cmp(node x,node y)
{
return x.c>y.c;
}
int main()
{
int T;
cin>>T;
while(T--)
{
int n,m;
cin>>n>>m;
int i,flag=0;;
for(i=0;i<m;i++)
{
cin>>s[i].a>>s[i].b;
s[i].c=s[i].b-s[i].a;
}
sort(s,s+m,cmp);
for(i=0;i<m;i++)
{
if(n>=s[i].b) n-=s[i].a;
else
{
flag=1;
break;
}
}
if(flag)
cout<<"No\n";
else
cout<<"Yes\n";
}
return 0;
}
HDUJ 3177 Crixalis's Equipment 贪心
原文地址:http://blog.csdn.net/hyccfy/article/details/38010187