답안 #525556

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
525556 2022-02-12T00:00:05 Z obokaman 커다란 상품 (IOI17_prize) C++17
100 / 100
243 ms 712 KB
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include "prize.h"
#include <random>

using namespace std;


std::default_random_engine generator;

set<int> seen;

typedef pair<int,int> pii;
struct Info {
  int lefthighercount = 0;
  int righthighercount = 0;
};

typedef map<int, Info> Level;
map<int, Level> levels;

void debug() {
  cerr << "Seen: ";
  for (int x: seen) cerr << x << " ";
  cerr << endl;
  cerr << "Levels: " << endl;
  for (auto& s: levels) {
    cerr << "v=" << s.first << ": ";
    for (auto& p: s.second) {
      cerr << p.first << "(" << p.second.lefthighercount << ","
           << p.second.righthighercount << ") ";
    }
    cerr << endl;
  }
  cerr << endl;
}

int count(int a, int b) {
  auto itfrom = seen.lower_bound(a);
  auto itto = seen.lower_bound(b);
  int num=0;
  while (itfrom != itto) {
    ++num;
    ++itfrom;
  }
  return num;
}


// Randomly find some interval in level within [ca,cb) that
// is a good candidate to query.
pii findinterval(int ca, int cb, const Level& l) {
  auto it = l.upper_bound(ca);
  --it;
  //  cerr << "Level" << endl;
  vector<pii> candidates;
  while(it->first + 1 < cb) {
    int a = it->first + 1;
    int lefta = it->second.lefthighercount;
    ++it;
    int b = it->first;
    int leftb = it->second.lefthighercount;
    if (a == b) continue;
    //    int l = b - a;  // #spots
    //   l -= count(seen, a, b);  // l = #empty spots
    int goodones = leftb - lefta;
    //    cerr << "[" << a << "," << b << "): " << goodones << " goodones" << endl;
    if (goodones > 0 && (count(a, b) < b - a)) {
      candidates.push_back({a, b});
    }
  }
  if (candidates.size() == 0) {
    return {-1, -1};
  }
  uniform_int_distribution<int> distribution(0, candidates.size()-1);
  auto p = candidates[distribution(generator)];
  return {max(p.first, ca), min(p.second, cb)};
}


int find_best(int n) {
  while (1) {
    //        debug();
    //    cerr << "XXX" << endl;
    int a = 0, b = n;
    if (!seen.empty()) {
      do {
        a = 0, b = n;
        for (auto it = levels.begin(); it != levels.end(); ++it) {
          tie(a, b) = findinterval(a, b, it->second);
          //          cerr << "Level " << it->first << ": [" << a << "," << b << ")"
          //   << endl;
          if (a == -1 && b == -1) break;
        }
        //        cerr << "." << endl;
      } while (a == -1 && b == -1);
    }
    // find best spot in [a, b)
    int m = (a+b)/2;
    int next = 1;
    int signnext = 1;
    //    cerr << "Checking " << m << "..." << endl;
    while (m < a || m >= b || seen.find(m) != seen.end()) {
      m += signnext * next;
      ++next;
      signnext *= -1;
      //cerr << "Taken. Checking " << m << "..." << endl;
    }
    auto q = ask(m);
    seen.insert(m);
    int v = q[0]+q[1];
    if (v == 0) return m;
    auto& s = levels[v];
    if (s.empty()) {
      s[-1] = Info{0, q[0]+q[1]};
      s[n] = Info{q[0]+q[1], 0};
    }
    s[m] = Info{q[0], q[1]};
  }
}

// int find_best(int n) {
//   int m = n/2;
//   auto q = ask(m);
//   seen.insert(m);
//   int v = q[0]+q[1];
//   if (v == 0) return m;
//   {
//     auto& s = intervals[v];
//     s[-1] = Info{0, q[0]+q[1]};
//     s[m] = Info{q[0], q[1]};
//     s[n] = Info{q[0]+q[1], 0};
//   }
//   while (1) {
//     debug();
//     auto its = intervals.end();
//     --its;
//     auto& s = its->second;
//     double bestscore = 0;
//     auto bestit = s.end();
//     for (auto it = s.begin(); it->first != n;) {
//       int a = it->first + 1;
//       int lefta = it->second.lefthighercount;
//       ++it;
//       int b = it->first;
//       int leftb = it->second.lefthighercount;
//       int l = b - a;  // #spots
//       l -= count(seen, a, b);  // l = #empty spots
//       cerr << "[" << a << "," << b << "): " << l << " empty   ";
//       int goodones = leftb - lefta;
//       cerr << goodones << " goodones" << endl;
//       if (l > 0 || goodones > 0) {
//         if (goodones > 0) {
//           double score = ((double)goodones)/(l+1);
//           if (score > bestscore) {
//             bestit = it;
//             bestscore = score;
//             cerr << "bestscore: " << bestscore << endl;
//           }
//         }
//       }
//     }
//     // find best spot in [ita, itb)
//     auto ita = bestit, itb = bestit;
//     --ita;
//     int a = ita->first+1;
//     int b = itb->first;
//     int m = (a+b)/2;
//     int next = 1;
//     int signnext = 1;
//     while (m < a || m >= b || seen.find(m) != seen.end()) {
//       m += signnext * next;
//       ++next;
//       signnext *= -1;
//     }
//     auto q = ask(m);
//     seen.insert(m);
//     int v = q[0]+q[1];
//     if (v == 0) return m;
//     auto& s2 = intervals[v];
//     if (s2.empty()) {
//       s2[-1] = Info{0, q[0]+q[1]};
//       s2[n] = Info{q[0]+q[1], 0};
//     }
//     s2[m] = Info{q[0], q[1]};
//   }
// }



// int find_best(int n) {
//   if (segments.empty()) {
//     int m = n/2;
//     auto q = ask(m);
//     int v = q[0]+q[1];
//     if (v == 0) return m;
//     segments[v][{0, m}] = Info{q[0], 0, q[1]};
//     segments[v][{m+1, n}] = Info{q[1], q[0], 0};
//   }
//   while (1) {
//     auto its = segments.end();
//     --its;
//     auto& s = its->second;
//     double bestp = 0;
//     auto bestit = s.end();
//     for (auto it = s.begin(); it != s.end(); ++it) {
//       int l = it->first.second - it->first.first;
//       if (l > 0) {
//         double p = ((double)it->second.higher)/l;
//         if (p>bestp) {
//           bestit = it;
//           bestp = p;
//         }
//       }
//     }
//     auto x = bestit->first;
//     auto oldinfo = bestit->second;
//     int m = (x.first+x.second)/2;
//     auto q = ask(m);
//     int v = q[0] + q[1];
//     if (v == 0) return m;
//     // fix here, different v.
//     s.erase(bestit);
//     s[{x.first, m}] = Info{q[0]-oldinfo.leftsum, oldinfo.leftsum, q[1]};
//     s[{m+1, x.second}] = Info{q[1]-oldinfo.rightsum, q[0], oldinfo.rightsum};
//   }
//   return -1;
// }
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 200 KB Output is correct
3 Correct 1 ms 284 KB Output is correct
4 Correct 1 ms 200 KB Output is correct
5 Correct 1 ms 280 KB Output is correct
6 Correct 1 ms 200 KB Output is correct
7 Correct 1 ms 280 KB Output is correct
8 Correct 0 ms 200 KB Output is correct
9 Correct 1 ms 200 KB Output is correct
10 Correct 2 ms 200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 200 KB Output is correct
2 Correct 1 ms 200 KB Output is correct
3 Correct 1 ms 292 KB Output is correct
4 Correct 1 ms 200 KB Output is correct
5 Correct 1 ms 200 KB Output is correct
6 Correct 1 ms 200 KB Output is correct
7 Correct 0 ms 200 KB Output is correct
8 Correct 1 ms 200 KB Output is correct
9 Correct 1 ms 200 KB Output is correct
10 Correct 1 ms 200 KB Output is correct
11 Correct 3 ms 284 KB Output is correct
12 Correct 4 ms 300 KB Output is correct
13 Correct 5 ms 328 KB Output is correct
14 Correct 10 ms 284 KB Output is correct
15 Correct 8 ms 268 KB Output is correct
16 Correct 74 ms 712 KB Output is correct
17 Correct 38 ms 424 KB Output is correct
18 Correct 47 ms 428 KB Output is correct
19 Correct 12 ms 320 KB Output is correct
20 Correct 27 ms 404 KB Output is correct
21 Correct 40 ms 448 KB Output is correct
22 Correct 21 ms 388 KB Output is correct
23 Correct 1 ms 276 KB Output is correct
24 Correct 5 ms 260 KB Output is correct
25 Correct 77 ms 420 KB Output is correct
26 Correct 35 ms 404 KB Output is correct
27 Correct 1 ms 280 KB Output is correct
28 Correct 13 ms 332 KB Output is correct
29 Correct 51 ms 404 KB Output is correct
30 Correct 41 ms 408 KB Output is correct
31 Correct 26 ms 396 KB Output is correct
32 Correct 7 ms 280 KB Output is correct
33 Correct 1 ms 200 KB Output is correct
34 Correct 39 ms 376 KB Output is correct
35 Correct 2 ms 200 KB Output is correct
36 Correct 59 ms 448 KB Output is correct
37 Correct 3 ms 276 KB Output is correct
38 Correct 1 ms 276 KB Output is correct
39 Correct 27 ms 284 KB Output is correct
40 Correct 6 ms 280 KB Output is correct
41 Correct 124 ms 480 KB Output is correct
42 Correct 112 ms 504 KB Output is correct
43 Correct 72 ms 536 KB Output is correct
44 Correct 103 ms 448 KB Output is correct
45 Correct 26 ms 276 KB Output is correct
46 Correct 28 ms 404 KB Output is correct
47 Correct 16 ms 352 KB Output is correct
48 Correct 102 ms 508 KB Output is correct
49 Correct 69 ms 468 KB Output is correct
50 Correct 29 ms 320 KB Output is correct
51 Correct 94 ms 408 KB Output is correct
52 Correct 103 ms 448 KB Output is correct
53 Correct 1 ms 260 KB Output is correct
54 Correct 3 ms 280 KB Output is correct
55 Correct 14 ms 272 KB Output is correct
56 Correct 40 ms 392 KB Output is correct
57 Correct 30 ms 404 KB Output is correct
58 Correct 155 ms 464 KB Output is correct
59 Correct 67 ms 388 KB Output is correct
60 Correct 32 ms 320 KB Output is correct
61 Correct 1 ms 200 KB Output is correct
62 Correct 2 ms 200 KB Output is correct
63 Correct 1 ms 260 KB Output is correct
64 Correct 1 ms 200 KB Output is correct
65 Correct 1 ms 200 KB Output is correct
66 Correct 1 ms 200 KB Output is correct
67 Correct 1 ms 200 KB Output is correct
68 Correct 1 ms 268 KB Output is correct
69 Correct 1 ms 264 KB Output is correct
70 Correct 1 ms 200 KB Output is correct
71 Correct 243 ms 672 KB Output is correct
72 Correct 9 ms 312 KB Output is correct
73 Correct 218 ms 624 KB Output is correct
74 Correct 67 ms 424 KB Output is correct
75 Correct 2 ms 200 KB Output is correct
76 Correct 51 ms 488 KB Output is correct
77 Correct 99 ms 460 KB Output is correct
78 Correct 7 ms 212 KB Output is correct
79 Correct 64 ms 380 KB Output is correct
80 Correct 132 ms 584 KB Output is correct
81 Correct 172 ms 448 KB Output is correct
82 Correct 46 ms 392 KB Output is correct
83 Correct 2 ms 200 KB Output is correct
84 Correct 45 ms 436 KB Output is correct
85 Correct 160 ms 592 KB Output is correct
86 Correct 4 ms 456 KB Output is correct
87 Correct 2 ms 296 KB Output is correct
88 Correct 3 ms 200 KB Output is correct
89 Correct 2 ms 284 KB Output is correct
90 Correct 1 ms 200 KB Output is correct
91 Correct 2 ms 200 KB Output is correct
92 Correct 1 ms 200 KB Output is correct
93 Correct 4 ms 200 KB Output is correct
94 Correct 7 ms 308 KB Output is correct
95 Correct 4 ms 200 KB Output is correct
96 Correct 3 ms 292 KB Output is correct
97 Correct 1 ms 200 KB Output is correct