POJ 3211 Washing Clothes 背包题解
2024-09-12 04:49:35
本题是背包问题,可是须要转化成背包的。
由于是两个人洗衣服,那么就是说一个人仅仅须要洗一半就能够了,由于不能两个人同一时候洗一件衣服,所以就成了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\n", bagDP(clothes));
}
return 0;
}
最新文章
- linux下c程序的链接、装载和库(3)
- import this, Python 之禅
- 开源战棋 SLG 游戏框架设计思考(一)简介和游戏引擎
- Python-2 print
- jQuery_01之选择器
- [IT新应用]农民朋友的电子商务
- fixed兼容IE6
- bzoj1857
- Leetcode 226 Invert Binary Tree python
- Call an activity method from a fragment
- ES6之Promise学习与实践
- react dnd demo2
- django+javascrpt+python实现私有云盘代码
- STM32_DAC之软件触发(Trigger)
- Python自学:第三章 弹出列表中任何位置处的元素
- 负载均衡(nginx、dubbo、zookeeper)
- [转]python3字符串与文本处理
- U盘权限不足,只读文件系统
- elasticsearch+logstash+redis+kibana 实时分析nginx日志
- android tools相关
热门文章
- mkdir命令的-p和-m
- MVC4实现AJAX需要引用的2个文件
- 【Android】12.5 利用Intent读取和更新通讯录
- angular学习笔记(十九)-指令修改dom
- weblogic连接池问题总结(转载)
- OFFLINE
- SSL/TLS原理详解2
- hive &; hive beeline常用参数
- hdu3065 病毒侵袭持续中 AC自动机入门题 N(N <;= 1000)个长度不大于50的模式串(保证所有的模式串都不相同), 一个长度不大于2000000的待匹配串,求模式串在待匹配串中的出现次数。
- Mac中Java 配置maven及阿里云镜像