結果

問題 No.953 席
ユーザー risujirohrisujiroh
提出日時 2019-12-16 02:15:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 4,926 bytes
コンパイル時間 2,569 ms
コンパイル使用メモリ 186,148 KB
実行使用メモリ 10,452 KB
最終ジャッジ日時 2023-09-15 16:31:29
合計ジャッジ時間 47,769 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 665 ms
9,272 KB
testcase_03 AC 426 ms
9,716 KB
testcase_04 AC 579 ms
9,544 KB
testcase_05 AC 345 ms
10,096 KB
testcase_06 AC 683 ms
9,356 KB
testcase_07 TLE -
testcase_08 TLE -
testcase_09 TLE -
testcase_10 AC 1,963 ms
5,272 KB
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 TLE -
testcase_15 TLE -
testcase_16 TLE -
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 AC 1,730 ms
10,136 KB
testcase_22 TLE -
testcase_23 AC 1,988 ms
8,688 KB
testcase_24 TLE -
testcase_25 TLE -
testcase_26 AC 344 ms
10,012 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 Stopwatch {
  clock_t t = clock();
  void restart() { t = clock(); }
  int elapsed() const { return (clock() - t) * 1000 / CLOCKS_PER_SEC; }
  friend string to_string(Stopwatch sw) {
    return "Time: " + to_string(sw.elapsed()) + " ms";
  }
} sw;

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(i2p[i]);
  }
  vector<int> res(q, -1);
  set<int> que;
  long long t = 0;
  auto add = [&](int id) {
    if (sw.elapsed() < 1900) {
      vector<bool> used(n, true);
      for (int p : se0) {
        int i = p2i[p];
        assert(used[i]);
        used[i] = false;
      }
      for (int p : se1) {
        int i = p2i[p];
        assert(used[i]);
        used[i] = false;
      }
      for (int p : se0) {
        int i = p2i[p];
        assert(i == 0 or not used[i - 1]);
        assert(i == n - 1 or not used[i + 1]);
      }
      for (int p : se1) {
        int i = p2i[p];
        assert(
          (i and used[i - 1]) or
          (i + 1 < n and used[i + 1])
        );
      }
    }
    int p = -1;
    if (not se0.empty()) {
      p = *begin(se0);
    } else if (not se1.empty()) {
      p = *begin(se1);
    } else {
      que.insert(id);
    }
    if (p == -1) {
      return;
    }
    int i = p2i[p];
    res[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, id, t + b[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.id);
    } 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()) {
          int id = *begin(que);
          assert(res[id] == -1);
          add(id);
          if (res[id] == -1) {
            break;
          }
          que.erase(id);
        }
      }
    }
  }
  for (int e : res) {
    cout << e + 1 << '\n';
  }
}
0