题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/F

题意:

        一堆物品放在箱子中,每个箱子最多能放两个,已知箱子的高度和物品的高度,问最少需要多少个箱子可以把物品全都装完。

        案例:

         input

        1

        10

        80

        70

        15

        30

        35

        10

        80

        20

        35

        10

        30

        output

        6

思路分析:

                                \epsfbox{p3503.eps}      

如上图,可以知道,一般都是大的和小的放在一起, 那么先进行排序,在两边同时开始相加,如果可以不超过箱子的高度,都进1,如果超过,则只有大的进1,就此循环可以得到结果。

源代码如下:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #define MAX 100010
 5 using namespace std;
 6 int main()
 7 {
 8     int t,n,l,i,a[MAX],count,j;
 9     scanf("%d",&t);
10     while(t--)
11     {
12         count=0;
13         scanf("%d%d",&n,&l);
14         for(i=0;i<n;i++)
15             scanf("%d",&a[i]);
16         sort(a,a+n);
17         for(j=0,i=n-1;i>=j;i--)       //循环比较,找准判断条件
18         {
19             count++;
20             if(a[i]+a[j]<=l)
21                 j++;
22         }
23         printf("%d\n",count);
24         if(t)printf("\n");         //仔细看题
25     }
26     return 0;
27 }

 

posted on 2015-08-08 16:30  尘埃。  阅读(741)  评论(0编辑  收藏  举报