题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12948
代码如下:
#include <algorithm> #include <iostream> #include <sstream> #include <string> #include <vector> #include <stack> #include <deque> #include <queue> #include <set> #include <map> #include <cstdio> #include <cstdlib> #include <cctype> #include <cmath> #include <cstring> #include <ctime> using namespace std; /*************** Program Begin **********************/ bool cmp(pair<int, int> a, pair<int, int> b) { return a.second > b.second; } class AlienAndHamburgers { public: int getNumber(vector <int> type, vector <int> taste) { int A = 0; int n = type.size(); vector < pair<int, int> > tt; for (int i = 0; i < n; i++) { tt.push_back( make_pair(type[i], taste[i]) ); } sort(tt.begin(), tt.end(), cmp); set <int> kinds; bool v[60]; for (int i = 0; i < 60; i++) { v[i] = false; } for (int i = 0; i < n; i++) { if (tt[i].second >= 0) { kinds.insert(tt[i].first); A += tt[i].second; v[i] = true; } } for (int i = 0; i < n; i++) { if (!v[i]) { set <int>::iterator it; it = kinds.find(tt[i].first); if (it != kinds.end()) { v[i] = true; } } } int Y = kinds.size(); int ans = Y * A; for (int i = 0; i < n; i++) { if (!v[i]) { int m = tt[i].second; kinds.insert(tt[i].first); ++Y; if (Y == kinds.size()) { A += m; ans = max(ans, Y * A); v[i] = true; } else { --Y; } } } return ans; } }; /************** Program End ************************/