B. Box Fitting-CodeCraft-21 and Codeforces Round #711 (Div. 2)

Problem - 1498B - Codeforces

B. Box Fitting

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given nn rectangles, each of height 11. Each rectangle's width is a power of 22 (i. e. it can be represented as 2x2x for some non-negative integer xx).

You are also given a two-dimensional box of width WW. Note that WW may or may not be a power of 22. Moreover, WW is at least as large as the width of the largest rectangle.

You have to find the smallest height of this box, such that it is able to fit all the given rectangles. It is allowed to have some empty space left in this box after fitting all the rectangles.

You cannot rotate the given rectangles to make them fit into the box. Moreover, any two distinct rectangles must not overlap, i. e., any two distinct rectangles must have zero intersection area.

See notes for visual explanation of sample input.

Input

The first line of input contains one integer tt (1≤t≤5⋅1031≤t≤5⋅103) — the number of test cases. Each test case consists of two lines.

For each test case:

  • the first line contains two integers nn (1≤n≤1051≤n≤105) and WW (1≤W≤1091≤W≤109);
  • the second line contains nn integers w1,w2,…,wnw1,w2,…,wn (1≤wi≤1061≤wi≤106), where wiwi is the width of the ii-th rectangle. Each wiwi is a power of 22;
  • additionally, maxi=1nwi≤Wmaxi=1nwi≤W.

The sum of nn over all test cases does not exceed 105105.

Output

Output tt integers. The ii-th integer should be equal to the answer to the ii-th test case — the smallest height of the box.

Example

input

Copy

2
5 16
1 2 8 4 8
6 10
2 8 8 2 2 8

output

Copy

2
3

Note

For the first test case in the sample input, the following figure shows one way to fit the given five rectangles into the 2D box with minimum height:

In the figure above, the number inside each rectangle is its width. The width of the 2D box is 1616 (indicated with arrow below). The minimum height required for the 2D box in this case is 22 (indicated on the left).

In the second test case, you can have a minimum height of three by keeping two blocks (one each of widths eight and two) on each of the three levels.

=================================================================================

所谓层数最少,也就是每层利用度最高,而每层利用度最高,是需要剩下的最少,要想剩下的最少,我们必须先放大的,因为先放大的还可以放一些小的,而先放小的会出现剩下5个空间,放2不放4最终导致这一行剩余了。

第二便是为了使剩下的尽可能少,我们要尽快把缺口大的填补上,而填补相同空间,先填大的比小的更优

# include<iostream>
# include<cstring>
# include<queue>
# include<algorithm>
using namespace std;

# define mod 1000000007

typedef long long int ll;

priority_queue<int>q;

int val[100000+10];


bool cmp(int a,int b)
{
    return a>b;

}
int main ()
{


 // 8 8 8 4  2 2 1
 //先让大的占据空间,否则会造成大空间的浪费


 int t;

 cin>>t;

 while(t--)
 {
     int n,v;

     cin>>n>>v;

     for(int i=1;i<=n;i++)
     {
         cin>>val[i];
     }
     while(!q.empty())
        q.pop();

     sort(val+1,val+1+n,cmp);

     q.push(v);


     int len=1;

     while(!q.empty()&&len<=n)
     {
         int now=q.top();

         q.pop();

         if(now>=val[len])
         {


             q.push(now-val[len]);

             len++;


         }
         else
         {
             q.push(now);

             q.push(v-val[len]);

             len++;

         }
     }

     cout<<q.size()<<endl;



 }

    return 0;

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qinsanma and Code

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值