Understanding the problem specification is the very key to solve it:
1) ... penalty time is computed ... submission received prior to the correct solution:
So if the AC submission (C) is before any other submission, you can ignore the later submissions to count penalty stuffs.
2) ... lines of input are in the order in which submissions were received:
So for the input
1 1 10 C
1 1 10 I
you should not count penalty... while
1 1 10 I
1 1 10 C
you should count penalty.
Code:
- /*************************************************************************
- * Copyright (C) 2008 by liukaipeng *
- * liukaipeng at gmail dot com *
- *************************************************************************/
- /* @JUDGE_ID 00000 10258 C++ "Contest Scoreboard" */
- #include <algorithm>
- #include <iostream>
- #include <iterator>
- #include <limits>
- #include <string>
- #include <sstream>
- #include <vector>
- using namespace std;
- struct submission
- {
- int contestant;
- int problem;
- int time;
- char status;
- };
- bool operator>>(istream& is, submission& sbm)
- {
- string line;
- getline(is, line);
- if (!line.empty()) {
- stringstream ss;
- ss << line;
- ss >> sbm.contestant
- >> sbm.problem
- >> sbm.time
- >> sbm.status;
- return true;
- } else {
- return false;
- }
- }
- struct submissioncmp
- {
- bool operator()(submission const& s1, submission const& s2)
- {
- return s1.contestant < s2.contestant ? true :
- s1.contestant > s2.contestant ? false :
- s1.problem < s2.problem ? true :
- s1.problem > s2.problem ? false :
- s1.time < s2.time;
- }
- };
- struct score
- {
- int contestant;
- int solved;
- int penalty;
- };
- void operator<<(ostream& os, score const& scr)
- {
- cout << scr.contestant << ' '
- << scr.solved << ' '
- << scr.penalty << '/n';
- }
- struct scorecmp
- {
- bool operator()(score const& s1, score const& s2)
- {
- return s1.solved > s2.solved ? true :
- s1.solved < s2.solved ? false :
- s1.penalty < s2.penalty ? true :
- s1.penalty > s2.penalty ? false :
- s1.contestant < s2.contestant;
- }
- };
- void compute_scores(vector<submission>& submissions,
- vector<score>& scores)
- {
- stable_sort(submissions.begin(), submissions.end(), submissioncmp());
- for (int i = 0, size = submissions.size(); i < size; ) {
- score scr = {submissions[i].contestant, 0, 0};
- for (; i < size && scr.contestant == submissions[i].contestant; ) {
- int problem = submissions[i].problem;
- int penalty = 0;
- bool solved = false;
- for (; i < size && scr.contestant == submissions[i].contestant &&
- problem == submissions[i].problem; ++i) {
- char status = submissions[i].status;
- if (!solved) {
- if (status == 'C') {
- solved = true;
- penalty += submissions[i].time;
- } else if (status == 'I') {
- penalty += 20;
- }
- }
- }
- if (solved) {
- scr.solved += 1;
- scr.penalty += penalty;
- }
- }
- scores.push_back(scr);
- }
- sort(scores.begin(), scores.end(), scorecmp());
- }
- int main(int argc, char *argv[])
- {
- #ifndef ONLINE_JUDGE
- string name = argv[0];
- freopen((name + ".in").c_str(), "r", stdin);
- freopen((name + ".out").c_str(), "w", stdout);
- #endif
- int ncases;
- cin >> ncases;
- cin.ignore(numeric_limits<streamsize>::max(), '/n');
- cin.ignore(numeric_limits<streamsize>::max(), '/n');
- while (ncases-- > 0) {
- submission sbm;
- vector<submission> submissions;
- while (cin >> sbm) submissions.push_back(sbm);
- vector<score> scores;
- compute_scores(submissions, scores);
- copy(scores.begin(), scores.end(), ostream_iterator<score>(cout));
- if (ncases > 0) cout << '/n';
- }
- return 0;
- }
本文介绍了一种用于计算编程竞赛中参赛者得分和罚时的算法。该算法通过解析输入的参赛者提交记录,计算每位参赛者的正确解答数量及罚时,并按得分高低和罚时多少进行排序。

487

被折叠的 条评论
为什么被折叠?



