【动态规划】poj 3211 washing clothes

首先应该注意到,各种颜色之间是互相没有干扰的,因此可以分别计算每种颜色所花的时间,然后再累加求和。

第二点,计算一种颜色衣服所花的时间,由于所花费时间的总和不变,所以实质上就是把一堆数分成两组,并且这两组的和要尽量接近,其中小者一定不会超过总和的一半,可以用动态规划来实现。

明白以上两点就很简单了。

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string.h>
using namespace std;

//POJ 3211 washing clothes
//I think dynamic programming strategy will work
//and it really works
int main(){
	int m, n;
	pair<string, int> colors[15];	
	pair<int, string> clothes[105];
	//several test cases... oh my god...
	while(cin>>m>>n && (n!=0 || m!=0)){
	for(int i=0; i<m; i++){
		cin>>colors[i].first;
		colors[i].second = 0;
	}
	for(int i=0; i<n; i++){
		cin>>clothes[i].first>>clothes[i].second;
		for(int j=0; j<m; j++){
			if(clothes[i].second == colors[j].first)
				colors[j].second += clothes[i].first;
		}
	}
	int res = 0;
	bool dp[100005];
	for(int i=0; i<m; i++){
		int upper = colors[i].second/2;
		memset(dp, false, sizeof(dp));
		dp[0]= true;
		vector<int> v;
		v.push_back(0);
		//update for each cloth
		for(int j=0; j<n; j++){
			//color must match
			if(colors[i].first == clothes[j].second){
				int len = v.size();
				for(int k=0; k<len; k++){
					int tmp = v[k]+clothes[j].first;
					//add into vector
					if((tmp<=upper) && (dp[tmp]==false)){
						dp[tmp] = true;
						v.push_back(tmp);
					}//end internal if				
				}
			}//end external if
		}//end for loop
		//choose the nearest value
		for(int j=upper; j>=0; j--){
			if(dp[j] == true){
				//add the larger time value
				res += (colors[i].second-j);			
				break;
			}
		}//end internal for loop
	}//end external for loop
	cout<<res<<endl;
	}	
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值