• POJ 3211 Washing Clothes 背包题解


    本题是背包问题,可是须要转化成背包的。

    由于是两个人洗衣服,那么就是说一个人仅仅须要洗一半就能够了,由于不能两个人同一时候洗一件衣服,所以就成了01背包问题了。

    思路:

    1 计算洗完同一颜色的衣服须要的总时间totTime

    2 利用动态规划背包法求这些衣服能在那些时间点完毕

    3 求比(totTime+1)/2大的最小时间点

    4 得到洗一种颜色衣服的时间,那么继续求下洗一种颜色衣服的时间

    5 最后加起来就是答案了。

    这个是算法问题。

    剩下来就是考编程功力了。由于给出的数据,须要我们自己把衣服分类,分类之后再对每一种颜色衣服进行动态规划法。

    会使用map就不难了。

    我这里是使用map,然后把颜色转化为整数,然后利用vector容器保存分类结果的。

    用好vector也不用操心数组不够大的问题,只是vector却是比一般数组慢一点点的。

    #include <stdio.h>
    #include <map>
    #include <string>
    #include <vector>
    using std::map;
    using std::string;
    using std::vector;
    
    int bagDP(vector<vector<int> > &cl)
    {
    	int washTime = 0;
    	for (int i = 0; i < (int)cl.size(); i++)
    	{
    		int totTime = 0;
    		for (int j = 0; j < (int)cl[i].size(); j++)
    			totTime += cl[i][j];
    
    		vector<bool> tbl(totTime+1);
    		tbl[0] = true;
    		for (int j = 0; j < (int)cl[i].size(); j++)
    		{
    			for (int t = totTime; t >= cl[i][j]; t--)
    				if (tbl[t-cl[i][j]]) tbl[t] = true;
    		}
    		int t = (totTime+1)>>1;
    		for ( ; t <= totTime && !tbl[t]; t++);
    		washTime += t;
    	}
    	return washTime;
    }
    
    int main()
    {
    	int colorM, clothN, time;
    	char col[12];
    	while (scanf("%d %d", &colorM, &clothN) && colorM)
    	{
    		map<string, int> siMp;//for classifying
    		string s;
    		int c = 0;
    		for (int i = 0; i < colorM; i++)
    		{
    			scanf("%s", col);
    			s = col;
    			siMp[s] = c++;
    		}
    		vector<vector<int> > clothes(siMp.size());
    		for (int i = 0; i < clothN; i++)
    		{
    			scanf("%d %s", &time, col);
    			s = col;
    			clothes[siMp[s]].push_back(time);
    		}
    		siMp.clear();
    		printf("%d
    ", bagDP(clothes));
    	}
    	return 0;
    }


  • 相关阅读:
    “家亡血史,原应叹息”
    SQLite初体验
    两张表数据同步用触发器
    openstack 后期维护(四)--- 删除僵尸卷
    Python3 装逼神器---词云(wordcloud)
    (三)FastDFS 高可用集群架构学习---Client 接口开发
    (四)FastDFS 高可用集群架构学习---后期运维--基础知识及常用命令
    (二)FastDFS 高可用集群架构学习---搭建
    (一)FastDFS 高可用集群架构学习---简介
    Python3使用Print输出彩色字体
  • 原文地址:https://www.cnblogs.com/wzzkaifa/p/6744653.html
Copyright © 2020-2023  润新知