TopCoder: SRM153 DIV2 1000

Problem Statement

     A large company is trying to put together a team of people to work on some task. In order for any team to work well, the people involved must be able to get along with each other. The managers at the company have put together a list of potential candidates for the team, and have assigned a number to each pair of candidates, representing how well those two people get along. The more positive a number, the better two people get along, while the more negative a number, the more they dislike each other. The candidates are all well qualified, so at this point the only consideration is how well the team will get along. You are to pick which candidates to put on the team to maximize the sum of the numbers assigned to all of the pairs of people on the team. You will be given a vector <string>, people, each of whose elements represents a single candidate, and an int, teamSize, representing the number of people to place on the team. Each element of people will be formatted as (quotes and angle brackets for clarity only):

"<name> <number0> <number1> <number2> ... <numberN>"

In other words, the element will start with the person's name, which will consist only of uppercase letters ('A'-'Z'), and will be followed by a single space delimited list of integers. The jth integer in the ith element will represent how well person i gets along with person j and will be the same as the ith integer in the jth element. The ith integer in the ith element will always be 0 (how well a person gets along with himself). Your task is to return a vector <string>, which contains the names of the people to put on the team (if more than one person on the team has the same name, that name must be in the returned vector <string> once for every member of the team with that name). The returned vector <string> should be sorted in ascending order. To make things simpler, there will only be one team which causes the maximum sum.



For example, if teamSize = 3 and people =
{"ALICE 0 1 -1 3",

 "BOB 1 0 2 -4",

 "CAROL -1 2 0 2",

 "DAVID 3 -4 2 0"}

There are four ways to create a team with 3 people. They give rise to the following sums:

ALICE, BOB, and CAROL: 1 + -1 + 2 = 2

ALICE, BOB, and DAVID: 1 + 3 + -4 = 0

ALICE, CAROL, and DAVID: -1 + 3 + 2 = 4

BOB, CAROL, and DAVID: 2 + -4 + 2 = 0

Of these, ALICE, CAROL and DAVID gives the highest sum, so your method would return {"ALICE","CAROL","DAVID"}.

Definition

    
Class: PickTeam
Method: pickPeople
Parameters: int, vector <string>
Returns: vector <string>
Method signature: vector <string> pickPeople(int teamSize, vector <string> people)
(be sure your method is public)
    
 

Constraints

- people will contain between 3 and 20 elements, inclusive.
- Each element of people will contain between 7 and 50 characters, inclusive.
- teamSize will be between 2 and the number of elements in people - 1, inclusive.
- Each element of people will be formatted as a sequence of uppercase letters ('A'-'Z'), followed by a single space delimited list of integers.
- Each element of people will contain the same number of integers as there are elements in people.
- Each integer in each element of people will be between -999 and 999, inclusive and will not have any extra leading zeros.
- The ith integer of the jth element of people will be the same as the jth integer of the ith element of people.
- The ith integer of the ith element will always be 0.
- There will be only 1 way to make a team with the highest sum.

Examples

0)  
    
3
{"ALICE 0 1 -1 3",

 "BOB 1 0 2 -4",

 "CAROL -1 2 0 2",

 "DAVID 3 -4 2 0"}
Returns: { "ALICE",  "CAROL",  "DAVID" }
The example from above.
1)  
    
2
{"ALICE 0 1 -1 3",

 "BOB 1 0 2 -4",

 "CAROL -1 2 0 2",

 "DAVID 3 -4 2 0"}
Returns: { "ALICE",  "DAVID" }
 
2)  
    
2
{"A 0 -2 -2","B -2 0 -1","C -2 -1 0"}
Returns: { "B",  "C" }
 
3)  
    
13
{"A 0 2 8 7 1 -4 -3 1 8 2 8 2 1 -3 7 1 3 9 -6 -5",

 "A 2 0 0 7 -7 -5 8 -8 -9 -9 6 -9 -4 -8 8 1 7 0 3 3",

 "A 8 0 0 -5 -5 -7 1 -3 1 9 -6 6 1 5 6 -9 8 6 -8 -8",

 "A 7 7 -5 0 7 -5 3 9 8 3 -6 -5 -5 2 -6 2 -2 -1 -2 8",

 "B 1 -7 -5 7 0 7 -2 -9 3 7 0 -2 1 1 -9 -3 -2 2 1 -2",

 "B -4 -5 -7 -5 7 0 4 8 6 0 -1 4 1 -2 -9 4 0 -7 6 -2",

 "B -3 8 1 3 -2 4 0 8 -1 1 -2 -7 5 0 -6 9 0 -3 1 3",

 "B 1 -8 -3 9 -9 8 8 0 0 -2 5 0 5 8 3 5 2 4 5 4",

 "C 8 -9 1 8 3 6 -1 0 0 8 -3 8 -6 -4 7 -4 1 -5 5 4",

 "C 2 -9 9 3 7 0 1 -2 8 0 -9 -6 6 5 -8 -3 -8 2 2 4",

 "C 8 6 -6 -6 0 -1 -2 5 -3 -9 0 2 7 8 2 -6 -4 -6 4 4",

 "C 2 -9 6 -5 -2 4 -7 0 8 -6 2 0 0 -3 6 7 -1 2 -4 -2",

 "D 1 -4 1 -5 1 1 5 5 -6 6 7 0 0 -7 -4 8 -6 -3 4 -6",

 "D -3 -8 5 2 1 -2 0 8 -4 5 8 -3 -7 0 7 -3 5 -8 5 1",

 "D 7 8 6 -6 -9 -9 -6 3 7 -8 2 6 -4 7 0 9 -5 -5 -8 3",

 "D 1 1 -9 2 -3 4 9 5 -4 -3 -6 7 8 -3 9 0 -2 -7 8 -7",

 "E 3 7 8 -2 -2 0 0 2 1 -8 -4 -1 -6 5 -5 -2 0 6 0 5",

 "E 9 0 6 -1 2 -7 -3 4 -5 2 -6 2 -3 -8 -5 -7 6 0 4 8",

 "E -6 3 -8 -2 1 6 1 5 5 2 4 -4 4 5 -8 8 0 4 0 1",

 "E -5 3 -8 8 -2 -2 3 4 4 4 4 -2 -6 1 3 -7 5 8 1 0"}
Returns: { "A",  "A",  "B",  "B",  "B",  "B",  "C",  "C",  "C",  "D",  "D",  "D",  "E" }

题目思路很明白了,DFS,实际写起来出了两次错,第一次是将输入的vector<string> 转换成vector<vector<int> >的时候其中的一个while循环里没有考虑到循环到最后那个数字时是‘\0'的时候,这样编译就是出错。第二个问题是在dfs循环里忘记写cur > vector<vector<int> >.size()要return了,这样一漏也导致了编译出错。记住编译出错肯定是程序运行到一个不可能的或者是忘记加终止条件的情况,不会是因为逻辑错误。整个程序写下来没有逻辑上的错误。

  1 // BEGIN CUT HERE

  2 

  3 // END CUT HERE

  4 #include <functional>

  5 #include <algorithm>

  6 #include <stdexcept>

  7 #include <iostream>

  8 #include <sstream>

  9 #include <fstream>

 10 #include <iomanip>

 11 #include <cstdlib>

 12 #include <cstring>

 13 #include <utility>

 14 #include <cctype>

 15 #include <vector>

 16 #include <string>

 17 #include <bitset>

 18 #include <queue>

 19 #include <stack>

 20 #include <ctime>

 21 #include <list>

 22 #include <map>

 23 #include <set>

 24 #include <math.h>

 25 

 26 using namespace std;

 27 

 28 #define pb push_back

 29 #define INF 100000000000

 30 #define L(s) (int)((s).size())

 31 #define FOR(i,a,b) for (int _n(b), i(a); i<=_n; i++)

 32 #define rep(i,n) FOR(i,1,(n))

 33 #define rept(i,n) FOR(i,0,(n)-1)

 34 #define rept2(i, m, j, n) FOR(i, 0, (m)-1) FOR(j, 0, (n)-1)

 35 #define rep2(i, m, j, n) FOR(i, 1, (m)) FOR(j, 1, (n))

 36 #define C(a) memset((a), 0, sizeof(a))

 37 #define ll long long

 38 #define VI vector<int>

 39 #define ppb pop_back

 40 #define mp make_pair

 41 #define MOD 1000000007

 42 int toInt(string s){ istringstream sin(s); int t; sin>>t;return t;}

 43 int stringToInt(string s) {

 44     int ret = 0;

 45     rept(i, L(s)) {

 46         ret = ret*10 + int(s[i]-'0');

 47     }

 48     return ret;

 49 }

 50 bool cmp(const string s1, const string s2) {

 51     int cur = 0;

 52     while (L(s1) > cur && L(s2) > cur) {

 53         if (s1[cur] < s2[cur]) return true;

 54         else if (s1[cur] > s2[cur]) return false;

 55         else cur++;

 56     }

 57     if (s1[cur] == '\0') return true;

 58     else return false;

 59 }

 60 

 61 class PickTeam

 62 {

 63         public:

 64         void dfs(vector<vector<int> > &p, int cur, int &ans, VI &tmp, int dep, int teamSize, int &res, VI &result) {

 65             if (dep == teamSize) {

 66                 if (res > ans) {

 67                     ans = res;

 68                     result = tmp;

 69                     return;

 70                 }

 71             }

 72             if (cur >= L(p)) return;

 73             dfs(p, cur+1, ans, tmp, dep, teamSize, res, result);

 74             int pre = res;

 75             for (int j = 0; j < L(tmp); j++) {

 76                 res += p[cur][tmp[j]];

 77             }

 78             tmp.pb(cur);

 79             dfs(p, cur+1, ans, tmp, dep+1, teamSize, res, result);

 80             res = pre;

 81             tmp.ppb();

 82         }

 83         vector <string> pickPeople(int teamSize, vector <string> people)

 84         {

 85             vector<vector<int> > p;

 86             vector<string> save;

 87             rept(i, L(people)) {

 88                 VI ps;

 89                 int cur = 0;

 90                 string savetmp = "";

 91                 while (people[i][cur] != ' ') {

 92                     savetmp += people[i][cur];

 93                     cur++;

 94                 }

 95                 save.pb(savetmp);

 96                 string tmp;

 97                 while (people[i][cur] != '\0') {

 98                     tmp = "";

 99                     cur++;

100                     while (people[i][cur] != ' ') {

101                         tmp += people[i][cur];

102                         cur++;

103                         if (people[i][cur] == '\0') break;

104                     }

105                     if (tmp[0] == '-') {

106                         tmp.erase(0, 1);

107                         int x = stringToInt(tmp);

108                         ps.pb(-x);

109                     }

110                     else ps.pb(stringToInt(tmp));

111                 }

112                 p.pb(ps);

113             }

114             int ans = INT_MIN;

115             VI tmp;

116             VI result;

117             int res = 0;

118             dfs(p, 0, ans, tmp, 0, teamSize, res, result);

119             vector<string> ret;

120             rept(i, teamSize) {

121                 ret.pb(save[result[i]]);

122             }

123             sort(ret.begin(), ret.end(), cmp);

124             return ret;

125         }

126 

127 // BEGIN CUT HERE

128     public:

129     void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); }

130     private:

131     template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }

132     void verify_case(int Case, const vector <string> &Expected, const vector <string> &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: " << print_array(Expected) << endl; cerr << "\tReceived: " << print_array(Received) << endl; } }

133     void test_case_0() { int Arg0 = 3; string Arr1[] = {"ALICE 0 1 -1 3",

134  "BOB 1 0 2 -4",

135  "CAROL -1 2 0 2",

136  "DAVID 3 -4 2 0"}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); string Arr2[] = { "ALICE",  "CAROL",  "DAVID" }; vector <string> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); verify_case(0, Arg2, pickPeople(Arg0, Arg1)); }

137     void test_case_1() { int Arg0 = 2; string Arr1[] = {"ALICE 0 1 -1 3",

138  "BOB 1 0 2 -4",

139  "CAROL -1 2 0 2",

140  "DAVID 3 -4 2 0"}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); string Arr2[] = { "ALICE",  "DAVID" }; vector <string> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); verify_case(1, Arg2, pickPeople(Arg0, Arg1)); }

141     void test_case_2() { int Arg0 = 2; string Arr1[] = {"A 0 -2 -2","B -2 0 -1","C -2 -1 0"}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); string Arr2[] = { "B",  "C" }; vector <string> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); verify_case(2, Arg2, pickPeople(Arg0, Arg1)); }

142     void test_case_3() { int Arg0 = 13; string Arr1[] = {"A 0 2 8 7 1 -4 -3 1 8 2 8 2 1 -3 7 1 3 9 -6 -5",

143  "A 2 0 0 7 -7 -5 8 -8 -9 -9 6 -9 -4 -8 8 1 7 0 3 3",

144  "A 8 0 0 -5 -5 -7 1 -3 1 9 -6 6 1 5 6 -9 8 6 -8 -8",

145  "A 7 7 -5 0 7 -5 3 9 8 3 -6 -5 -5 2 -6 2 -2 -1 -2 8",

146  "B 1 -7 -5 7 0 7 -2 -9 3 7 0 -2 1 1 -9 -3 -2 2 1 -2",

147  "B -4 -5 -7 -5 7 0 4 8 6 0 -1 4 1 -2 -9 4 0 -7 6 -2",

148  "B -3 8 1 3 -2 4 0 8 -1 1 -2 -7 5 0 -6 9 0 -3 1 3",

149  "B 1 -8 -3 9 -9 8 8 0 0 -2 5 0 5 8 3 5 2 4 5 4",

150  "C 8 -9 1 8 3 6 -1 0 0 8 -3 8 -6 -4 7 -4 1 -5 5 4",

151  "C 2 -9 9 3 7 0 1 -2 8 0 -9 -6 6 5 -8 -3 -8 2 2 4",

152  "C 8 6 -6 -6 0 -1 -2 5 -3 -9 0 2 7 8 2 -6 -4 -6 4 4",

153  "C 2 -9 6 -5 -2 4 -7 0 8 -6 2 0 0 -3 6 7 -1 2 -4 -2",

154  "D 1 -4 1 -5 1 1 5 5 -6 6 7 0 0 -7 -4 8 -6 -3 4 -6",

155  "D -3 -8 5 2 1 -2 0 8 -4 5 8 -3 -7 0 7 -3 5 -8 5 1",

156  "D 7 8 6 -6 -9 -9 -6 3 7 -8 2 6 -4 7 0 9 -5 -5 -8 3",

157  "D 1 1 -9 2 -3 4 9 5 -4 -3 -6 7 8 -3 9 0 -2 -7 8 -7",

158  "E 3 7 8 -2 -2 0 0 2 1 -8 -4 -1 -6 5 -5 -2 0 6 0 5",

159  "E 9 0 6 -1 2 -7 -3 4 -5 2 -6 2 -3 -8 -5 -7 6 0 4 8",

160  "E -6 3 -8 -2 1 6 1 5 5 2 4 -4 4 5 -8 8 0 4 0 1",

161  "E -5 3 -8 8 -2 -2 3 4 4 4 4 -2 -6 1 3 -7 5 8 1 0"}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); string Arr2[] = { "A",  "A",  "B",  "B",  "B",  "B",  "C",  "C",  "C",  "D",  "D",  "D",  "E" }; vector <string> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); verify_case(3, Arg2, pickPeople(Arg0, Arg1)); }

162 

163 // END CUT HERE

164 

165 };

166 // BEGIN CUT HERE

167 int main()

168 {

169         PickTeam ___test;

170         ___test.run_test(-1);

171         return 0;

172 }

173 // END CUT HERE

 

你可能感兴趣的:(topcoder)