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