結果

問題 No.953 席
ユーザー risujirohrisujiroh
提出日時 2019-12-16 01:32:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 192 ms / 2,000 ms
コード長 4,318 bytes
コンパイル時間 3,199 ms
コンパイル使用メモリ 187,160 KB
実行使用メモリ 9,240 KB
最終ジャッジ日時 2023-09-15 16:16:40
合計ジャッジ時間 6,995 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 75 ms
7,192 KB
testcase_03 AC 69 ms
7,148 KB
testcase_04 AC 73 ms
7,164 KB
testcase_05 AC 61 ms
7,060 KB
testcase_06 AC 75 ms
7,132 KB
testcase_07 AC 189 ms
8,864 KB
testcase_08 AC 192 ms
7,892 KB
testcase_09 AC 168 ms
9,240 KB
testcase_10 AC 51 ms
5,004 KB
testcase_11 AC 135 ms
6,700 KB
testcase_12 AC 132 ms
7,032 KB
testcase_13 AC 170 ms
7,884 KB
testcase_14 AC 113 ms
6,864 KB
testcase_15 AC 105 ms
6,756 KB
testcase_16 AC 111 ms
6,772 KB
testcase_17 AC 127 ms
8,068 KB
testcase_18 AC 117 ms
8,108 KB
testcase_19 AC 160 ms
8,720 KB
testcase_20 AC 118 ms
8,096 KB
testcase_21 AC 82 ms
7,524 KB
testcase_22 AC 135 ms
7,268 KB
testcase_23 AC 90 ms
6,896 KB
testcase_24 AC 89 ms
6,976 KB
testcase_25 AC 182 ms
8,036 KB
testcase_26 AC 51 ms
7,016 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

string to_string(string s) { return '"' + s + '"'; }
string to_string(bool b) { return b ? "true" : "false"; }
template <size_t N> string to_string(bitset<N> bs) {
  string res;
  for (size_t i = 0; i < N; ++i) res += '0' + bs[i];
  return res;
}
string to_string(vector<bool> v) {
  string res = "{";
  for (bool e : v) res += to_string(e) + ", ";
  return res += "}";
}

template <class T, class U> string to_string(pair<T, U> p);
template <class C> string to_string(C c) {
  string res = "{";
  for (auto e : c) res += to_string(e) + ", ";
  return res += "}";
}
template <class T, class U> string to_string(pair<T, U> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

void debug() { cerr << '\n'; }
template <class Head, class... Tail> void debug(Head head, Tail... tail) {
  cerr << ' ' << to_string(head), debug(tail...);
}
#ifdef LOCAL
#define DEBUG(...) cerr << "[" << #__VA_ARGS__ << "]:", debug(__VA_ARGS__)
#else
#define DEBUG(...)
#endif

struct Event {
  int type, id;
  long long t;
  bool operator<(Event r) const {
    return make_pair(t, -type) < make_pair(r.t, -r.type);
  };
  bool operator>(Event r) const { return r < *this; }
  friend string to_string(Event e) {
    return "(" + to_string(e.type) + ", " + to_string(e.id + 1) + ", " + to_string(e.t) + ")";
  }
};

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, k0, k1;
  cin >> n >> k0 >> k1;
  --k0, --k1;
  vector<int> i2p(n);
  if (k0 < k1) {
    int t = 1;
    for (int i = 1; i < n; ++i) {
      if (k0 + i < n) {
        i2p[k0 + i] = t++;
      }
      if (k0 - i >= 0) {
        i2p[k0 - i] = t++;
      }
    }
  } else {
    int t = 1;
    for (int i = 1; i < n; ++i) {
      if (k0 - i >= 0) {
        i2p[k0 - i] = t++;
      }
      if (k0 + i < n) {
        i2p[k0 + i] = t++;
      }
    }
  }
  vector<int> p2i(n);
  for (int i = 0; i < n; ++i) {
    p2i[i2p[i]] = i;
  }
  int q;
  cin >> q;
  priority_queue< Event, vector<Event>, greater<Event> > pq;
  vector<int> b(q);
  for (int i = 0; i < q; ++i) {
    int a;
    cin >> a >> b[i];
    pq.emplace(Event{0, i, a});
  }
  DEBUG(i2p);
  set<int> se0, se1;
  for (int i = 0; i < n; ++i) {
    se0.insert(i);
  }
  vector<int> res(q, -1);
  queue<Event> que;
  long long t = 0;
  auto add = [&](auto e) {
    int p = -1;
    if (not se0.empty()) {
      p = *begin(se0);
    } else if (not se1.empty()) {
      p = *begin(se1);
    } else {
      que.push(e);
    }
    if (p == -1) {
      return;
    }
    int i = p2i[p];
    res[e.id] = i;
    if (i and se0.count(i2p[i - 1])) {
      se0.erase(i2p[i - 1]);
      se1.insert(i2p[i - 1]);
    }
    se0.erase(i2p[i]);
    if (i + 1 < n and se0.count(i2p[i + 1])) {
      se0.erase(i2p[i + 1]);
      se1.insert(i2p[i + 1]);
    }
    se1.erase(i2p[i]);
    pq.emplace(Event{1, e.id, t + b[e.id]});
  };
  while (not pq.empty()) {
    auto e = pq.top();
    DEBUG(e);
    DEBUG(se0);
    DEBUG(se1);
    pq.pop();
    t = e.t;
    if (e.type == 0) {
      add(e);
    } else {
      auto  chk = [&](int i) {
        if (i - 1 >= 0 and not se0.count(i2p[i - 1]) and not se1.count(i2p[i - 1])) {
          return false;
        }
        if (i + 1 < n and not se0.count(i2p[i + 1]) and not se1.count(i2p[i + 1])) {
          return false;
        }
        return true;
      };
      int i = res[e.id];
      if (chk(i)) {
        se0.insert(i2p[i]);
      } else {
        se1.insert(i2p[i]);
      }
      for (int j : {i - 1, i + 1}) {
        if (j < 0 or j >= n) {
          continue;
        }
        if (se1.count(i2p[j]) and chk(j)) {
          se1.erase(i2p[j]);
          se0.insert(i2p[j]);
        }
      }
      DEBUG(se0);
      DEBUG(se1);
      if (pq.empty() or e < pq.top()) {
        while (not que.empty()) {
      // if (t == 50 and se0.size() == 2 and se1.size() == 2) {
      //   cerr << "found" << '\n';
      //   cerr << res[que.front().id] << '\n';
      //   DEBUG(se0, se1);
      // }
          if (res[que.front().id] != -1) {
            que.pop();
            continue;
          }
          add(que.front());
          if (res[que.front().id] == -1) {
            break;
          }
          que.pop();
        }
      }
    }
  }
  for (int e : res) {
    cout << e + 1 << '\n';
  }
}
0