Computer Programming Contest Preparation

ToolBox - Source for: 102/10258/x.cpp



/home/toolbox/public_html/solutions/102/10258/x.cpp
    1 /* vim: set ts=4 sts=4 sw=4 si foldmethod=marker: */
    2 // {{{ stock programming challenge garbage
    3 
    4 // IO
    5 #include <string>
    6 #include <iostream>
    7 #include <iomanip>
    8 #include <sstream>
    9 
   10 // Utility
   11 #include <cassert>
   12 #include <algorithm>
   13 
   14 // Data structures
   15 //#include <vector>
   16 #include <list>
   17 //#include <set>
   18 //#include <queue>
   19 //#include <ext/hash_map>
   20 //#include <tr1/unordered_set>
   21 
   22 // Utility typedefs
   23 
   24 // I want easy access to STL and things like hash_map
   25 using namespace std;
   26 //using namespace std::tr1;
   27 using namespace __gnu_cxx;
   28 
   29 // Needed for hash_map<string/X>
   30 //namespace __gnu_cxx
   31 //{
   32 //    template<> struct hash<std::string>
   33 //    {
   34 //        size_t operator()(const std::string& x) const
   35 //        {
   36 //            return hash<const char*>()(x.c_str());
   37 //        }
   38 //    };
   39 //}
   40 
   41 // }}}
   42 
   43 struct Attempt
   44 {
   45     Attempt(char status, int time) : status(status), time(time)
   46     {
   47     }
   48 
   49     int time;
   50     char status; // C = correct, I = incorrect, otherwise ignore
   51 };
   52 
   53 typedef list<Attempt> lA;
   54 
   55 struct Problem
   56 {
   57     Problem() : solved(false), penalty(0)
   58     {
   59     }
   60 
   61     lA attempt;
   62     int penalty;
   63     bool solved;
   64     int solvetime;
   65 
   66     void solve()
   67     {
   68         penalty = 0;
   69         solved = false;
   70         for (lA::iterator i(attempt.begin()); i != attempt.end(); ++i)
   71             if ((*i).status == 'C')
   72                 {
   73                     if (!solved || (*i).time < penalty)
   74                         {
   75                             penalty = (*i).time;
   76                             solvetime = penalty;
   77                             solved = true;
   78                         }
   79                 }
   80 
   81         if (solved)
   82             for (lA::iterator i(attempt.begin()); i != attempt.end(); ++i)
   83                 if ((*i).status == 'I' && (*i).time <= solvetime)
   84                     penalty += 20;
   85     }
   86 };
   87 
   88 struct Contestant
   89 {
   90     Contestant() : solved(0), penalty(0), number(0), present(false)
   91     {
   92     }
   93 
   94     int solved;
   95     int penalty;
   96     int number;
   97     bool present;
   98     Problem problem[10];
   99 
  100     void solve()
  101     {
  102         solved = 0;
  103         penalty = 0;
  104         for (int i(1); i <= 9; i++)
  105             {
  106                 problem[i].solve();
  107                 if (problem[i].solved)
  108                     {
  109                         solved++;
  110                         penalty += problem[i].penalty;
  111                     }
  112 
  113             }
  114 
  115         //for (
  116     }
  117 };
  118 
  119 bool operator<(const Contestant& a, const Contestant& b)
  120 {
  121     if (a.solved == b.solved)
  122         {
  123             if (a.penalty == b.penalty)
  124                 {
  125                     return a.number < b.number;
  126 
  127                 }
  128             else
  129                 {
  130                     return a.penalty < b.penalty;
  131                 }
  132         }
  133     else
  134         return a.solved > b.solved;
  135 
  136 
  137     return (a.solved == b.solved)
  138            ? ((a.penalty == b.penalty)
  139               ? a.number > b.number
  140               : a.penalty > b.penalty)
  141            : a.solved > b.solved;
  142 }
  143 
  144 int main(int argc, char **argv)
  145 {
  146     int cases;
  147     cin >> cases;
  148     cin.ignore(1000, '\n');
  149     cin.ignore(1000, '\n');
  150 
  151     while (cases--)
  152         {
  153             Contestant contestant[101];
  154             for (int i(1); i <= 100; contestant[i].number = i, ++i);
  155 
  156             string line;
  157             while (getline(cin, line), line != "")
  158                 {
  159                     istringstream mystream(line);
  160                     int team, problem, time;
  161                     char status;
  162                     mystream >> team >> problem >> time >> status;
  163                     contestant[team].present = true;
  164                     contestant[team].problem[problem].attempt.push_back(Attempt(status, time));
  165                 }
  166 
  167             for (int i(1); i <= 100; ++i) if (contestant[i].present) contestant[i].solve();
  168             sort(contestant + 1, contestant + 101);
  169             for (int i(1); i <= 100; ++i) if (contestant[i].present)
  170                     {
  171                         cout << contestant[i].number << " " << contestant[i].solved << " " << contestant[i].penalty << endl;
  172                     }
  173 
  174             if (0 != cases) cout << endl;
  175         }
  176 
  177 
  178     return 0;
  179 }
  180 
  181