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;
}
};