TopCoder SRM 598 Div1 第1题

原创 2013年12月02日 20:20:27

Problem Statement

 

Fox Ciel has some items. The weight of the i-th (0-based) item is item[i]. She wants to put all items into bins.

The capacity of each bin is 300. She can put an arbitrary number of items into a single bin, but the total weight of items in a bin must be less than or equal to 300.

You are given the vector <int> item. It is known that the weight of each item is between 100 and 300, inclusive. Return the minimal number of bins 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 50 elements, inclusive.

-

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

Examples

0)

 

 

{150, 150, 150, 150, 150}

Returns: 3

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

1)

 

 

{130, 140, 150, 160}

Returns: 2

For example, you can distribute the items in 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

 

类型:其他  难度:2.5

题意:给出一组数,表示一组物品的重量,每个物品重量范围[100,300],现在要将这组物品放到若干个桶中,每个桶最多放重量300的物品,问最少需要多少桶

分析:一开始把题目想简单了,经过几次修改才正确。

1、首先将物品按重量排序,从重量大的遍历,考虑大于200的物品,这些物品必定一个物品占一个桶(因为最轻的物品重量为100,大于200的不能和其他物品放在一个桶里)

2、再遍历剩余的物品,找到当前最重的物品,并找到能和它放在一个桶中的最重的物品

(1)若没有和它能放在一个桶中的,那么这个物品单独占一个桶

(2)若有,那么这两个物品放在一个桶中

3、若当前最重物品重量为100,那么还需要(剩余物品数+2)/ 3 个桶

代码:

#include<string>
#include<cstdio>
#include<vector>
#include<cstring>
#include<map>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<set>
using namespace std;

class BinPacking
{ 	 
	public:
		int minBins(vector <int> item)
		{
			sort(item.begin(),item.end());
			
			int ans = 0;
			
			vector<int>::iterator it;
			while(!item.empty() && item.back()>200)
			{
				ans++;
				item.pop_back();
			}
			while(!item.empty())
			{
				int i=item.size()-1,j=i-1; 
				if(item[i]==100)
				{
					ans += (item.size()+2)/3;
					return ans;
				}
				for(; j>=0; j--)
				{
					if(item[i]+item[j]<=300)
						break;
				} 
				item.pop_back();
				if(j>=0) item.erase(item.begin()+j,item.begin()+j+1);
				ans++;
			} 
				
			return ans;
		} 
};

int main()
{
	int a[] = {100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 193, 160, 124, 178, 105, 150, 140, 117, 219, 126, 153, 183, 212, 179, 140, 103, 195};
	vector<int> tmp;
	for(int i=0; a[i]>0; i++)
	{
		tmp.push_back(a[i]);
	}
	BinPacking t;
	cout<<t.minBins(tmp)<<endl;
}





 

TopCoder SRM 602 Div1 第1题

Problem Statement   TypoCoder is a programming contest like TopCoder. TypoCoder also ...

TopCoder SRM 597 Div1 第1题

Problem Statement   Little Elephant from the Zoo of Lviv likes strings. You are given a str...

TopCoder SRM 609 Div1 第2题

Problem Statement   We have balls of K different colors. The colors are numbered 0 through ...

topcoder算法题SRM144 DIV1 500分题

Problem Statement   In most states, gamblers can choose from a wide variety of different ...

topcoder SRM500 DIV1 LEVEL1 250分题

Problem Statement   N friends (numbered from 0 to N-1) play a game called Mafia. The exact ...

TopCoder 250 points 17-SRM 152 DIV 1 106.51/250 42.60%

Problem Statement   You and your friends are setting up a fantasy TopCoder league, where ...

topcoder SRM 348 Div.1 LostParentheses

Problem Statement   We have an arithmetic expression made up of positive integers, the + ...

topcoder SRM div 2 level 1

Problem Statement   Dmitry likes TopCoder Single Round Matches. Once, he registered for an ...

【TopCoder SRM 157 DIV 1】 Salary 解题报告

本来想把原题给贴过来的,后来看到人家有版权保护,在网上搜了一下,好像也没有人在博客中贴过原题,为避免麻烦,就不copy人家原题了。 【题目大意】 公司按照每小时 wage 元给员工发工资,员工上班打卡...
  • ljiabin
  • ljiabin
  • 2014年08月26日 15:31
  • 947

topcoder SRM500 DIV1 level2 500分

Problem Statement   Nick likes to draw fractals. For the special occasion of Single Round M...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:TopCoder SRM 598 Div1 第1题
举报原因:
原因补充:

(最多只允许输入30个字)