题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12556
#include <iostream> #include <vector> #include <algorithm> using namespace std; int F[50]; class SpaceWarDiv2 { public: int minimalFatigue(vector <int> magicalGirlStrength, vector <int> enemyStrength, vector <int> enemyCount); }; int SpaceWarDiv2::minimalFatigue(vector<int> magicalGirlStrength, vector<int> enemyStrength, vector<int> enemyCount) { int maxMS, maxES; int i, j; maxMS = *max_element(magicalGirlStrength.begin(), magicalGirlStrength.end()); maxES = *max_element(enemyStrength.begin(), enemyStrength.end()); if (maxMS < maxES) { return -1; } for (i = 0; i < 50; i++) { F[i] = 0; } sort(magicalGirlStrength.begin(), magicalGirlStrength.end()); for (i = 0; i < enemyStrength.size()-1; i++) { for (j = i+1; j < enemyStrength.size(); j++) { if (enemyStrength[i] > enemyStrength[j]) { swap(enemyStrength[i], enemyStrength[j]); swap(enemyCount[i], enemyCount[j]); } } } while (!enemyCount.empty()) { for (i = 0; i < magicalGirlStrength.size(); i++) { j = enemyStrength.size() - 1; while (j >= 0 && magicalGirlStrength[i] < enemyStrength[j]) { --j; } if (j >= 0) { ++F[i]; --enemyCount[j]; if (0 == enemyCount[j]) { enemyCount.erase(enemyCount.begin() + j); enemyStrength.erase(enemyStrength.begin() + j); } } } } return *max_element(F, F + magicalGirlStrength.size()); }