zoukankan      html  css  js  c++  java
  • Hdu 3177 Crixalis's Equipment

    Crixalis's Equipment

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


    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
     
    Source
     
    Recommend
    lcy
     

     贪心.

    贪心策略:当前放的物品所需的空间 Bi 和 下一个需要放的物品所占用空间 Aj 之和是否大于 当前物品所占用空间 Ai 和下一个物品所需空间 Bi 之和.

    也就是(Bi+Aj)>(Bj+Ai)的话 就先放物品i 否则 先放物品j

    需要空间大的先放.

    可以用一组案例来思考一下这个贪心策略:

    20 3

    10 20

    3 4

    7 7

    ___________

    代码如下:

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <iostream>
     4 #include <algorithm>
     5 using namespace std;
     6 #define MAX 1001
     7 struct node
     8 {
     9     int a;
    10     int b;
    11 };
    12 struct node x[MAX];
    13 int v,n;
    14 bool cmp(struct node x,struct node y)
    15 {
    16     if(x.b==y.b) return x.a>y.a;
    17     return (x.b-x.a)>(y.b-y.a);
    18 }
    19 void init()
    20 {
    21     memset(x,0,sizeof(x));
    22 }
    23 void read()
    24 {
    25     int i;
    26     scanf("%d %d",&v,&n);
    27     for(i=0;i<n;i++)
    28         scanf("%d %d",&x[i].a,&x[i].b);
    29     sort(x,x+n,cmp);
    30 }
    31 void cal()
    32 {
    33     int i;
    34     int res=v;
    35     for(i=0;i<n;i++)
    36     {
    37         if(res<0||res<x[i].b)
    38         {
    39             res-=x[i].b;
    40             break;
    41         }
    42         res-=x[i].a;
    43     }
    44     if(res>=0) printf("Yes
    ");
    45     else printf("No
    ");
    46 }
    47 void solve()
    48 {
    49     init();
    50     read();
    51     cal();
    52 }
    53 
    54 int main()
    55 {
    56     int t;
    57     scanf("%d",&t);
    58     while(t--)
    59     {
    60         solve();
    61     }
    62     return 0;
    63 }
    View Code
  • 相关阅读:
    Qt助手---摘录
    Qt随记
    前端编码规范之CSS(转)
    给QT新手的练手项目——基于QT的GIF播放器(转)
    Qt收藏
    C++开发者都应该使用的10个C++11特性(转)
    为学Linux,我看了这些书(转)
    Qt去掉view项的焦点虚线框的方法(转)
    QTableWidget详解(样式、右键菜单、表头塌陷、多选等)(转)
    JZOJ 3085. 图的计数
  • 原文地址:https://www.cnblogs.com/By-ruoyu/p/3905586.html
Copyright © 2011-2022 走看看