HDU3177-Crixalis's Equipment

来源:互联网 发布:剑网三御姐脸捏脸数据 编辑:程序博客网 时间:2024/05/16 11:51

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
题意:要将一些物品搬进洞里面,这些物品搬运过程中需要空间为bi,自身的体积为ai,问这一部分物品能不能全部搬到洞里面去!
 
思路:贪心算法,我们每次搬运想到就是使剩余的洞穴空间尽量大,当有两件物品时,有如下信息
A a1 b1
B a2 b2
加入先搬A,则搬运过程中瞬间占用空间最大为a1+b2,如果先搬运B,则这一个瞬间最大占用体积为a2+b1,我们想使剩下的体积尽量大,所以我们选择min(a1+b2,a2+b1),如果先选A,则有a1+b2<a2+b1,即b1-a1>b2-a2,所以我们可以看出,应该先选搬运体积和本身体积差值最大的贪心,由此得到最优搬运顺序!

#include<stdio.h>#include<string.h>#include<stdlib.h>struct data{    int a;    int b;    int c;}node[1100];int cmp(void const *x,void const *y){    return ((struct data *)y)->c-((struct data *)x)->c;}int main(){    int T,v,n,i,max,flag;    scanf("%d",&T);    while(T--)    {        scanf("%d %d",&v,&n);        for(i=0,max=0;i<n;i++)        {            scanf("%d %d",&node[i].a,&node[i].b);            node[i].c=node[i].b-node[i].a;            if(node[i].b>max) max=node[i].b;             }        if(max>v)        {            printf("No\n");            continue;        }        qsort(node,n,sizeof(node[0]),cmp);        for(i=0,flag=1;i<n;i++)        {            if(v>=node[i].b)                v-=node[i].a;            else            {                flag=0;                break;            }        }        if(flag==1) printf("Yes\n");        else printf("No\n");    }    system("pause");    return 0;}


 
 
原创粉丝点击