結果

問題 No.78 クジ付きアイスバー
ユーザー le_panda_noirle_panda_noir
提出日時 2020-07-25 11:55:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,686 bytes
コンパイル時間 1,135 ms
コンパイル使用メモリ 87,704 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-09 08:32:28
合計ジャッジ時間 2,713 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 1 ms
4,504 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 2 ms
4,384 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <set>
using namespace std;
using ll = long long;
using vi = vector<int>;
#define rep(i,n) for(int i=0,_i=(n);i<_i;++i)

// === debug ===
template<class T>ostream& operator<<(ostream& os,const vector<T>& vec){os<<"{";for(size_t i=0;i<vec.size();++i)os<<(i?", ":"")<<vec[i];os<<"}";return os;}
ostream& operator<<(ostream& os,const vector<char>&v){for(size_t i=0;i<v.size();++i)os<<v[i];return os;}
template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>& rhs){os<<"("<<rhs.first<<", "<<rhs.second<<")";return os;}

#ifdef LOCAL
void debug() {cerr << "\n";}
template<class First> void debug(const First& first) {cerr<<first<<"\n";}
template<class First, class... Rest> void debug(const First& first, const Rest&... rest) {cerr<<first<<",";debug(rest...);}
void debug2() {cerr << "\n";}
template<class First> void debug2(const First& first) {cerr<<first<<" ";}
template<class First, class... Rest> void debug2(const First& first, const Rest&... rest) {cerr<<first<<" ";debug2(rest...);}
#else
#define debug(...) 42
#define debug2(...) 42
#endif
// === end ===

int INF = 1e9;
int main() {
  int N, K; cin >> N >> K;
  string S; cin >> S;

  vi G(N, 0);
  rep(i, N) {
    if (S[i] == '0') {
      G[i] = (i+1) % N;
      continue;
    }
    // アタリを持っている
    int atari = S[i] - '0', j = 0;
    while (atari > 0) {
      // アタリがある間すすんで、進めなくなったら止まる
      --atari;
      ++j;
      atari += S[(i+j)%N] - '0';
      if (j > 2*N)
        break;
    }
    if (j > 2*N) {
      G[i] = INF;
    } else {
      G[i] = (i+j+1)%N;
    }
  }
  if (G[0] == INF) {
    cout << 1 << endl;
    return 0;
  }
  set<int> st;
  int next = 0;
  vector<pair<int, int>> v;
  int ans = 0;
  debug(G);
  while (true) {
    int cur = next; next = G[cur];
    ++ans;
    if (next == INF) {
      cout << ans << endl;
      return 0;
    }
    int distance = (next > cur ? next - cur : N + next - cur);
    K -= distance;
    if (K <= 0) {
      cout << ans << endl;
      return 0;
    }
    if (st.find(cur) == st.end()) {
      v.emplace_back(cur, distance);
      st.insert(cur);
      continue;
    }
    K += distance;
    --ans;
    vi circle;
    ll sum = 0;
    rep(i, v.size()) {
      if (v[i].first != cur)
        continue;
      while(i < (int)v.size()) {
        circle.push_back(v[i].second);
        sum += v[i].second;
        ++i;
      }
      break;
    }
    ans += (K / sum) * circle.size();
    K %= sum;
    rep(i, circle.size()) {
      if (K <= 0)
        break;
      K -= circle[i];
      ++ans;
    }
    cout << ans << endl;
    break;
  }

  return 0;
}
0