TopCoder SRM154 DIV2 1000

Problem Statement

     Some competitions have judges who score one specific area of a performance. Unfortunately, bad judges do exist and can individually cause one group's rating to be affected drastically. In some sports, the highest and lowest scores are dropped. This works well when all judges are scoring the same thing. However, when each judge scores only one aspect of a performance, dropping a score is not an option. 



One way to minimize the effects of a bad judge is to not use the score each judge gives, but to use the relative ranking the judge gives each group. The group that places first has a ranking of 1. The group that places second has a ranking of 2, third has a ranking of 3, and so forth. When multiple groups are given the same score from one judge, then they will all receive the same ranking. For example, if the groups A, B, C, D, and E receive the scores 98, 95, 95, 90, and 83 respectively, then they would receive rankings of 1, 2, 2, 4, and 5, respectively. 



Each group's rankings from all of the judges are then added together to find the group's total ranking. The group with the lowest total ranking places first. The group with the second lowest total ranking places second and so forth. Ties are broken using the sum of all scores the group receives from the judges to find the group's total score. The group with the highest total score wins the tiebreaker. If multiple groups have the same total ranking and the same total score, then the group whose name comes first alphabetically wins. 



Create a class ContestScore with a method sortResults that is given a vector <string> data and will return a vector <string> containing the group names and scores sorted from first to last place. Every string in data will be formatted as: ("<GROUPNAME> NN.N NN.N NN.N") (quotes for clarity) where <GROUPNAME> will be between 1 and 10 characters in length, inclusive, and NN.N is a decimal number. It will have leading zeros if necessary to follow the above format. Each string in the returned vector <string> will be formatted as "<GROUPNAME> <TOTALRANK> <TOTALSCORE>" (quotes for clarity) where <TOTALRANK> is the group's total ranking and is an integer. <TOTALSCORE> is the sum of the scores received from all judges and will always have exactly one decimal place and no extra leading 0's. For example, if a group's <TOTALRANK> is 7 and <TOTALSCORE> is 271, then the string would be "<GROUPNAME> 7 271.0" (quotes for clarity). 



For example, we have three groups. Group A scored a 90.7, a 92.9, and an 87.4. Group B scored a 90.5, a 96.6, and an 88.0. Group C scored a 92.2, a 91.0, and a 95.3. Here is how you would receive the data (quotes for clarity).
  {"A 90.7 92.9 87.4",

   "B 90.5 96.6 88.0",

   "C 92.2 91.0 95.3"}
From judge 1, group C got 1st, group A got 2nd, and group B got 3rd. From judge 2, group B got 1st, group A got 2nd, and group C got 3rd. From judge 3, group C got 1st, group B got 2nd, and group A got 3rd.
   A  2 2 3

   B  3 1 2

   C  1 3 1
So A has a total ranking of 7, B has a total ranking of 6, and C has a total ranking of 5. Since C has the lowest total ranking, they place first. B places second, and A places third. You would return:
  {"C 5 278.5",

   "B 6 275.1",

   "A 7 271.0"}

Definition

    
Class: ContestScore
Method: sortResults
Parameters: vector <string>
Returns: vector <string>
Method signature: vector <string> sortResults(vector <string> data)
(be sure your method is public)
    
 

Notes

- It is possible for a group to receive 00.0 for a score. Remember that leading and trailing zeros will be present if necessary in order to maintain formatting.
- Remember that if a group's total score is an exact integer to display it with one decimal place. Also, if the total score is between 0.0 and 0.9, inclusive, display one zero to the left of the decimal point. The last example shows this.

Constraints

- data will contain between 0 and 50 elements, inclusive.
- Each element of data will contain the exact same number of scores.
- Each element of data will contain between 6 and 50 characters, inclusive.
- There will be at least one judge.
- Each element of data will be formatted as a group name followed by a single space, followed by a single space delimited list of scores.
- Group names will be between 1 and 10 characters in length, and will consist of only of uppercase letters ('A'-'Z').
- No two groups will have the same name.
- The score from each judge will be formatted as NN.N where N is a digit ('0'-'9').
- There will be no leading or trailing spaces in each string of data.

Examples

0)  
    
{"A 90.7 92.9 87.4",

 "B 90.5 96.6 88.0",

 "C 92.2 91.0 95.3"}
Returns: { "C 5 278.5",  "B 6 275.1",  "A 7 271.0" }
Example from above.
1)  
    
{"STANFORD 85.3 90.1 82.6 84.6 96.6 94.5 87.3 90.3",

 "MIT 95.5 83.9 80.4 85.5 98.7 98.3 96.7 82.7",

 "PRINCETON 99.2 79.1 87.6 85.1 93.6 96.4 86.0 90.6",

 "HARVARD 83.6 92.0 85.5 94.3 97.5 91.5 92.5 83.0",

 "YALE 99.5 92.6 86.2 82.0 96.4 92.6 84.5 78.6",

 "COLUMBIA 97.2 87.6 81.7 93.7 88.0 86.3 95.9 89.6",

 "BROWN 92.2 95.8 92.1 81.5 89.5 87.0 95.5 86.4",

 "PENN 96.3 80.7 81.2 91.6 85.8 92.2 83.9 87.8",

 "CORNELL 88.0 83.7 85.0 83.8 99.8 92.1 91.0 88.9"}
Returns: 

{ "PRINCETON 34 717.6",

  "MIT 36 721.7",

  "HARVARD 38 719.9",

  "COLUMBIA 39 720.0",

  "STANFORD 39 711.3",

  "YALE 40 712.4",

  "BROWN 41 720.0",

  "CORNELL 42 712.3",

  "PENN 51 699.5" }
 
2)  
    
{}
Returns: { }
 
3)  
    
{"AA 90.0 80.0 70.0 60.0 50.0 40.0",

 "BBB 80.0 70.0 60.0 50.0 40.0 90.0",

 "EEE 70.0 60.0 50.0 40.0 90.0 80.0",

 "AAA 60.0 50.0 40.0 90.0 80.0 70.0",

 "DDD 50.0 40.0 90.0 80.0 70.0 60.0",

 "CCC 40.0 90.0 80.0 70.0 60.0 50.0"}
Returns: 

{ "AA 21 390.0",

  "AAA 21 390.0",

  "BBB 21 390.0",

  "CCC 21 390.0",

  "DDD 21 390.0",

  "EEE 21 390.0" }
 
4)  
    
{"A 00.1", "B 05.2", "C 29.0","D 00.0"}
Returns: { "C 1 29.0",  "B 2 5.2",  "A 3 0.1",  "D 4 0.0" }
Remember that, for numbers between 0.0 and 0.9, inclusive, you must display a 0 before the decimal point. Do not display any extraneous leading zeros if a number is between 1.0 and 9.9, inclusive.

这题做了太长时间了,在自己机子上能pass,但是IDE上不行。

  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 

 44 double stringToDouble(string s) {

 45     double ret = 0.0;

 46     rept(i, L(s)) {

 47         if (s[i] != '.') ret = ret*10 + int(s[i]-'0');

 48         else break;

 49     }

 50     ret += 0.10*(s[L(s)-1]-'0');

 51     int t = int(ret*10+0.5);

 52     ret = double(t)/10;

 53     return ret;

 54 }

 55 

 56 string intToString(int n) {

 57     string ret;

 58     if (n == 0) return "0";

 59     while (n) {

 60         char tmp = char('0' + n%10);

 61         ret += tmp;

 62         n /= 10;

 63     }

 64     reverse(ret.begin(), ret.end());

 65     return ret;

 66 }

 67 

 68 string doubleToString(double n) {

 69     string ret;

 70     int t = int(n*10+0.5);

 71     n = double(t)/10;

 72     if (n == 0.0) return "0.0";

 73     int m = int(n*10);

 74     while (m) {

 75         char tmp = char('0' + m%10);

 76         ret += tmp;

 77         m /= 10;

 78     }

 79     if (n < 1.0) ret += '0';

 80     reverse(ret.begin(), ret.end());

 81     ret.insert(L(ret)-1, ".");

 82     return ret;

 83 }

 84 

 85 void getInfo(vector<string> &data, vector<string> &name, vector<vector<double> > &score) {

 86     rept(i, L(data)) {

 87         vector<double> ps;

 88         int cur = 0;

 89         string savetmp = "";

 90         while (data[i][cur] != ' ') {

 91             savetmp += data[i][cur];

 92             cur++;

 93         }

 94         name.pb(savetmp);

 95         string tmp;

 96         while (cur != L(data[i])) {

 97             tmp = "";

 98             cur++;

 99             while (data[i][cur] != ' ') {

100                 tmp += data[i][cur];

101                 cur++;

102                 if (cur == L(data[i])) break;

103             }

104             ps.pb(stringToDouble(tmp));

105         }

106         score.pb(ps);

107     }

108 }

109 struct node {

110     int ranknum;

111     double sco;

112     string name;

113     node(int a, double b, string c) : ranknum(a), sco(b), name(c) { }

114     node() : ranknum(0), sco(0.0), name("") { }

115 };

116 

117 bool cmp(const double a, const double b) {return a > b;}

118 bool comp(const struct node a, const struct node b) {

119     if (a.ranknum == b.ranknum) return a.name < b.name;

120     else return a.ranknum < b.ranknum;

121 }

122 

123 

124 

125 class ContestScore

126 {

127         public:

128 

129         vector <string> sortResults(vector <string> data)

130         {

131             vector<vector<double> > score;

132             vector<string> name;

133             getInfo(data, name, score);

134             map<double, int> S;

135             vector<double> T(L(score));

136             vector<vector<int> > rankrec(L(score), vector<int>(L(score[0])));

137             for (int j = 0; j < score[0].size(); j++) {

138                 rept(i, L(score)) {

139                     S[score[i][j]] = i;

140                     T[i] = score[i][j];

141                 }

142                 sort(T.begin(), T.end(), cmp);

143                 rept(i, L(score)) rankrec[S[T[i]]][j] = i + 1;

144             }

145             vector<node> res;

146             for (int i = 0; i < L(score); i++) {

147                 node tmp(0, 0.0, name[i]);

148                 for (int j = 0; j < L(score[0]); j++) {

149                     tmp.ranknum += rankrec[i][j];

150                     tmp.sco += score[i][j];

151                 }

152                 res.pb(tmp);

153             }

154             sort(res.begin(), res.end(), comp);

155             vector<string> ret;

156             rept(i, L(score)) {

157                 string tmp;

158                 tmp += res[i].name;

159                 tmp += ' ';

160                 tmp += intToString(res[i].ranknum);

161                 tmp += ' ';

162                 tmp += doubleToString(res[i].sco);

163                 ret.pb(tmp);

164             }

165             return ret;

166         }

167 

168 // BEGIN CUT HERE

169     public:

170     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(); if ((Case == -1) || (Case == 4)) test_case_4(); }

171     private:

172     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(); }

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

174     void test_case_0() { string Arr0[] = {"A 90.7 92.9 87.4",

175  "B 90.5 96.6 88.0",

176  "C 92.2 91.0 95.3"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = { "C 5 278.5",  "B 6 275.1",  "A 7 271.0" }; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(0, Arg1, sortResults(Arg0)); }

177     void test_case_1() { string Arr0[] = {"STANFORD 85.3 90.1 82.6 84.6 96.6 94.5 87.3 90.3",

178  "MIT 95.5 83.9 80.4 85.5 98.7 98.3 96.7 82.7",

179  "PRINCETON 99.2 79.1 87.6 85.1 93.6 96.4 86.0 90.6",

180  "HARVARD 83.6 92.0 85.5 94.3 97.5 91.5 92.5 83.0",

181  "YALE 99.5 92.6 86.2 82.0 96.4 92.6 84.5 78.6",

182  "COLUMBIA 97.2 87.6 81.7 93.7 88.0 86.3 95.9 89.6",

183  "BROWN 92.2 95.8 92.1 81.5 89.5 87.0 95.5 86.4",

184  "PENN 96.3 80.7 81.2 91.6 85.8 92.2 83.9 87.8",

185  "CORNELL 88.0 83.7 85.0 83.8 99.8 92.1 91.0 88.9"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = { "PRINCETON 34 717.6",  "MIT 36 721.7",  "HARVARD 38 719.9",  "COLUMBIA 39 720.0",  "STANFORD 39 711.3",  "YALE 40 712.4",  "BROWN 41 720.0",  "CORNELL 42 712.3",  "PENN 51 699.5" }; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(1, Arg1, sortResults(Arg0)); }

186     void test_case_2() { string Arr0[] = {"A 00.1", "B 05.2", "C 29.0","D 00.0"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = { "C 1 29.0",  "B 2 5.2",  "A 3 0.1",  "D 4 0.0" }; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(2, Arg1, sortResults(Arg0)); }

187     void test_case_3() { string Arr0[] = {"AA 90.0 80.0 70.0 60.0 50.0 40.0",

188  "BBB 80.0 70.0 60.0 50.0 40.0 90.0",

189  "EEE 70.0 60.0 50.0 40.0 90.0 80.0",

190  "AAA 60.0 50.0 40.0 90.0 80.0 70.0",

191  "DDD 50.0 40.0 90.0 80.0 70.0 60.0",

192  "CCC 40.0 90.0 80.0 70.0 60.0 50.0"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = { "AA 21 390.0",  "AAA 21 390.0",  "BBB 21 390.0",  "CCC 21 390.0",  "DDD 21 390.0",  "EEE 21 390.0" }; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(3, Arg1, sortResults(Arg0)); }

193     void test_case_4() { string Arr0[] = {"A 00.1", "B 05.2", "C 29.0","D 00.0"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = { "C 1 29.0",  "B 2 5.2",  "A 3 0.1",  "D 4 0.0" }; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(4, Arg1, sortResults(Arg0)); }

194 

195 // END CUT HERE

196 

197 };

198 // BEGIN CUT HERE

199 int main()

200 {

201         ContestScore ___test;

202         ___test.run_test(-1);

203         return 0;

204 }

205 // END CUT HERE

 

你可能感兴趣的:(topcoder)