Hatena::Grouptopcoder

TopCoderの問題を解く

解いた問題の一覧表

2009-07-23

ContestScore

| 07:50

問題文

合計ランクと合計スコアを計算して、結果をソートする。

bool compGroup(const string& a, const string& b)
{
    string aName, bName;
    int aRank, bRank;
    double aScore, bScore;
    istringstream isa(a), isb(b);
    isa >> aName >> aRank >> aScore;
    isb >> bName >> bRank >> bScore;
    if (aRank < bRank) {
        return true;
    } else if (aRank > bRank) {
        return false;
    } else {
        if (aScore > bScore) {
            return true;
        } else if (aScore < bScore) {
            return false;
        } else {
            return aName < bName;
        }
    }
}

class ContestScore {
public:
    vector <string> sortResults(vector <string> data) {
        const int numGroups = data.size();
        vector<vector<double> > scores(numGroups);
        vector<string> names(numGroups);
        vector<double> totalScores(numGroups);
        for (int i = 0; i < numGroups; i++) {
            istringstream iss(data[i]);
            iss >> names[i];
            double score;
            while (iss >> score) scores[i].push_back(score);
            totalScores[i] = accumulate(scores[i].begin(),scores[i].end(),0.0l);
        }

        int numScores = scores[0].size();
        vector<int> totalRanks(numGroups);
        for (int i = 0; i < numScores; i++) {
            double pre = 100.0;
            for (int rank = 1; rank <= numGroups; ) {
                double maxScore = -1.0;
                for (int j = 0; j < numGroups; j++) {
                    if (scores[j][i] < pre && scores[j][i] > maxScore) {
                        maxScore = scores[j][i];
                    }
                }
                int sameScores = 0;
                for (int j = 0; j < numGroups; j++) {
                    if (scores[j][i] == maxScore) {
                        totalRanks[j] += rank;
                        sameScores++;
                    }
                }
                rank += sameScores;
                pre = maxScore;
            }
        }

        vector<string> result(numGroups);
        for (int i = 0; i < numGroups; i++) {
            ostringstream oss;
            oss.setf(ios_base::fixed);
            oss.precision(1);
            oss << names[i] << " " << totalRanks[i] << " " << totalScores[i];
            result[i] = oss.str();
        }
        sort(result.begin(), result.end(), compGroup);
        return result;
    }
};