Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 27th, 2012  |  syntax: C++  |  size: 1.28 KB  |  hits: 21  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <fstream>
  2. #include <algorithm>
  3. #include <cstdio>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. ifstream cin("input.txt");
  9. ofstream cout("output.txt");
  10.  
  11. int t, n, a[1500], b[1500], solved[1500];
  12.  
  13. void solve(int c) {
  14.     int stars = 0, k = 0;
  15.     for (int i = 0; i < n; ++i) solved[i] = 0;
  16.     cin >> n;
  17.     for (int i = 0; i < n; ++i) cin >> a[i] >> b[i];
  18.     while (true) {
  19.         int v = -1, g = -1;
  20.         for (int i = 0; i < n; ++i) {
  21.             if ((solved[i] != 2) && (stars >= b[i])) g = i;
  22.             else if ((solved[i] == 0) && (stars >= a[i]) && ((v == -1) || (b[i] > b[v]))) v = i;
  23.         }
  24.         if (g != -1) {
  25.             stars += (2 - solved[g]);
  26.             ++k;
  27.             solved[g] = 2;
  28.             continue;
  29.         }
  30.         if (v != -1) {
  31.             ++stars;
  32.             ++k;
  33.             solved[v] = 1;
  34.             continue;
  35.         } else {
  36.             for (int j = 0; j < n; ++j) {
  37.                 if (solved[j] != 2) {
  38.                     cout << "Case #" << c << ": Too bad\n";
  39.                     return;
  40.                 }
  41.             }
  42.             cout << "Case #" << c << ": " << k << endl;
  43.             return;
  44.         }
  45.     }
  46. }
  47.  
  48. int main() {
  49.     cin >> t;
  50.     for (int i = 0; i < t; ++i) solve(i+1);
  51.  
  52.     return 0;
  53. }