SRM 598 1A 2013.12.6

SRM 598 1A 2013.12.6

DIV 1

250

 

Problem Statement

    

Fox Ciel has some items. The weight of thei-th (0-based) item is item[i]. She wants to put all items into bins.  The capacity of each bin is 300. She can putan arbitrary number of items into a single bin, but the total weight of itemsin a bin must be less than or equal to 300. You are given the vector <int> item. It is known that the weightof each item is between 100 and 300, inclusive. Return the minimal number ofbins required to store all items.

Definition

    

Class:

BinPacking

Method:

minBins

Parameters:

vector <int>

Returns:

int

Method signature:

int minBins(vector <int> item)

(be sure your method is public)

    

 

Constraints

-

item will contain between 1 and 50elements, inclusive.

-

Each element of item will be between 100and 300, inclusive.

Examples

0)

 

    

{150, 150, 150, 150, 150}

Returns: 3

You have five items and each bin can holdat most two of them. You need at least three bins.

1)

 

    

{130, 140, 150, 160}

Returns: 2

For example, you can distribute the itemsin the following way:

Bin 1: 130, 150

Bin 2: 140, 160

2)

 

    

{100, 100, 100, 100, 100, 100, 100, 100,100}

Returns: 3

 

3)

 

    

{100, 200, 100, 100, 100, 100, 200, 100,200}

Returns: 4

 

4)

 

    

{157, 142, 167, 133, 135, 157, 143, 160,141, 123, 162, 159, 165, 137, 138, 152}

Returns: 8

 

This problem statement is the exclusive andproprietary property of TopCoder, Inc. Any unauthorized use or reproduction ofthis information without the prior written consent of TopCoder, Inc. isstrictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

 

138.41

#include<iostream>

#include<string>

#include<vector>

#include<algorithm>

 

using namespace std;

 

class BinPacking

{

public:int minBins(vector <int> item)

         {

                      int flag[60]={0};

                      int ans=0,max=300;

                      sort(item.begin(),item.end());

                      reverse(item.begin(),item.end());

                      int len=item.size(),c;

                      for(int i=0;i<len;i++)

                               if (flag[i]==0)

                               {

                                        c=item[i];

                                        flag[i]=1;

                                        ans++;

                                        for(intj=i+1;(j<len)&&(c<max);j++)

                                                  if (flag[j]==0)

                                                  {

                                                           if (c+item[j]<=max)

                                                           {

                                                                    c+=item[j];

                                                                    flag[j]=1;

                                                                    if (c==max) break;

                                                                   for(int k=j+1;(k<len)&&(c<max);k++)

                                                                              if (c+item[k]<=max)

                                                                              {

                                                                                       flag[k]=1;

                                                                                       c+=item[k];

                                                                              }

                                                           }

                                                  }

                               }            

 

                      return ans;

         }

};

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值