HDU-3177 Crixalis's Equipment

来源:互联网 发布:c语言if是什么意思 编辑:程序博客网 时间:2024/05/16 19:12

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

题意:

沙王要将N个物体搬到体积为V的洞里,Ai为放入洞后占得体积,Bi为放之前需要洞的体积,比如示例:要放第一个物体,需要洞体积为20,放入后占10体积,还剩10体积,所以第二个物体才能放进去。依次类推。

思路:

首先应该想到贪心,然后先对Ai排序,然后判断,结果wa,然后对Bi排序,提交又wa,最后仔细一想,不是简单的贪心就能水过。其实主要判断放物体的先后顺序,比如有两个物体x(10 ,20),y(3  , 10),发现如果先放x再放y,那么最小需要空间为max(20,10+10)=20体积,如果先放y再放x,那么最小需要空间为max(10,3+20)=23体积,所以要根据这个参数进行排序,即min(max(20,10+10),max(10,3+20)),即比较x.a+y.b和x.b+y.a.

#include<stdio.h>#include<stdlib.h>#include<math.h>#include<algorithm>using namespace std;struct eng{    int a;    int b;};eng c[1005];bool cmp(struct eng x,struct eng y){    return (x.b+y.a>=y.b+x.a);}int main(){    int t;    int v,n,i;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&v,&n);        for(i=0;i<n;i++)            scanf("%d%d",&c[i].a,&c[i].b);            sort (c,c+n,cmp);        int flag=0;        for(i=0;i<n;i++)        {            if(c[i].b>v)            {                flag=1;                break;            }            else            {                v-=c[i].a;            }        }        if(flag)        printf("No\n");        else        printf("Yes\n");    }    return 0;}


1 0
原创粉丝点击