admin管理员组文章数量:1530518
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
停放体积 移动体积
第一件物品 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<cstdio>
#include<algorithm>
#define MAXN 1010 using namespace std; struct equipment {int v1,v2;}a[MAXN]; bool cmp(equipment A,equipment B)
{
return A.v2-A.v1>B.v2-B.v1;
}
int main()
{
int test,vv,n,i;// freopen("1.txt","r",stdin); scanf("%d",&test);
while(test--)
{
scanf("%d%d",&vv,&n);
for(i=0;i<n;i++)
{
scanf("%d%d",&a[i].v1,&a[i].v2);
}
sort(a,a+n,cmp);
for(i=0;i<n;i++)
{
if(vv>=a[i].v2)vv-=a[i].v1;
else break;
}
if(i==n)printf("Yes\n");//之前我把Yes全是大写,结果一直报错,花了好多好多时间都没找出来,差点想毁灭世界;后来叫同学帮我一看,他
else printf("No\n"); //只看了一眼就发现问题了。我当时真的,感觉就像一个五大四粗的汉子被一个小屁孩给砍了;唉,这真是噩梦,碰 //这种情况
}
return 0;
}
版权声明:本文标题:G - Crixalis's Equipment 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dongtai/1726267127a1063363.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论