Poj 3211 grouping clothes backpack question

Source: Internet
Author: User

This question is about a backpack, but it needs to be converted into a backpack.

Because two people wash clothes, that is to say, one person only needs to wash half of the clothes, because two people cannot wash one piece of clothing at the same time, so it becomes a problem.

Ideas:

1. Calculate the total time tottime required to wash clothes of the same color.

2. Use the dynamic planning backpack method to find that these clothes can be completed at those time points

3. Calculate the minimum time point (tottime + 1)/2

4. Get the time to wash the clothes of a certain color, then continue to find the time to wash the clothes of another color.

5. The final sum is the answer.

This is an algorithm problem.

The rest is the test of programming skills, because the given data, we need to classify the clothes, classification and then dynamic programming for each color clothes.

It is not difficult to use map.

Here we use map, convert the color to an integer, and then use the vector container to save the classification result.

You don't need to worry about the array size when using a good vector, but the vector is a little slower than the average array.

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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.