結果

問題 No.1211 円環はお断り
ユーザー beet
提出日時 2020-08-30 13:45:08
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,592 ms / 2,000 ms
コード長 1,603 bytes
コンパイル時間 2,553 ms
コンパイル使用メモリ 197,908 KB
最終ジャッジ日時 2025-01-13 21:21:18
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using Int = long long;
const char newl = '\n';

template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
template<typename T> void drop(const T &x){cout<<x<<endl;exit(0);}
template<typename T=Int>
vector<T> read(size_t n){
  vector<T> ts(n);
  for(size_t i=0;i<n;i++) cin>>ts[i];
  return ts;
}


template<typename T, typename ...Ts>
vector<T> fusion(vector<T> bs,Ts... ts){
  auto append=[&](auto vs){for(auto v:vs) bs.emplace_back(v);};
  initializer_list<Int>{(void(append(ts)),0)...};
  return bs;
}

//INSERT ABOVE HERE
const Int LOG = 20;
const Int MAX = 1e5 + 100;
Int nx[LOG][3 * MAX];
signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  Int n,m;
  cin>>n>>m;
  auto as=read(n);

  auto rs=fusion(as,as);
  vector<Int> sm(2*n+1);
  for(Int i=0;i<2*n;i++) sm[i+1]=sm[i]+rs[i];

  auto check=[&](Int d)->Int{
    for(Int i=0;i<=2*n;i++)
      nx[0][i]=lower_bound(sm.begin(),sm.end(),sm[i]+d)-sm.begin();

    for(Int k=0;k<LOG;k++) nx[k][2*n+1]=2*n+1;

    for(Int k=0;k+1<LOG;k++)
      for(Int i=0;i<=2*n;i++)
        nx[k+1][i]=nx[k][nx[k][i]];

    for(Int i=0;i<n;i++){    nx[0][2*n]=2*n+1;
      Int j=i;
      for(Int k=0;k<LOG;k++)
        if((m>>k)&1) j=nx[k][j];

      // cout<<d<<':'<<m<<':'<<i<<' '<<j<<newl;
      if(j-i<=n) return 1;
    }
    return 0;
  };

  Int L=0,R=1e15;
  // check(L) : true
  // check(R) : false
  while(L+1<R){
    Int M=(L+R)>>1;
    if(check(M)) L=M;
    else R=M;
  }
  cout<<L<<newl;
  return 0;
}
0