結果

問題 No.2694 The Early Bird Catches The Worm
ユーザー MM
提出日時 2024-02-16 23:25:15
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 153 ms / 2,000 ms
コード長 1,466 bytes
コンパイル時間 1,636 ms
コンパイル使用メモリ 171,128 KB
実行使用メモリ 8,064 KB
最終ジャッジ日時 2024-09-28 22:36:40
合計ジャッジ時間 8,648 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 72
権限があれば一括ダウンロードができます

ソースコード

diff #

// バリデーション用なのでWA想定です
#include<bits/stdc++.h>
#define chmin(x,y) (x) = min((x),(y))
#define chmax(x,y) (x) = max((x),(y))
using namespace std;
using ll = long long;

void validate(ll x, ll m){
	if(x < 1 || m < x) cout << x << endl;
	// assert(1 <= x && x <= m);
}

int main(){
  // input
  int N; ll H;
  cin >> N >> H;
  validate(N,200000);
  validate(H,1000000000);
  vector<ll> A(N),B(N),S(N+1); // S[i]: Bの累積和
  for(int i = 0; i < N; i++){
    cin >> A[i];
    validate(A[i],1000000000);
  }
  for(int i = 0; i < N; i++){
    cin >> B[i];
    validate(B[i],1000000000);
    S[i+1] = S[i] + B[i];
  }
  
  // solve: 尺取法
  ll ans = 0;
  ll tmp = 0, need_hp = 0;
  int r = 0;
  for(int l = 0; l < N; l++){
    // right < n && (right を 1 個進めたときに条件を満たす)
    while(r < N && need_hp + B[r] * (r-l+1) <= H){
      tmp += A[r];
      need_hp += B[r] * (r-l+1);
      r++;
      // if(need_hp == H) cout<< l+1 << " " << r << endl; // for debug
    }
    
    // break した状態で right は条件を満たす最大なので、何かする
    // cout << l << " " << r << " " << need_hp << " " << tmp << endl;
    chmax(ans,tmp);
    
    // left をインクリメントする準備
    if(l == r) r++;
    else{
      tmp -= A[l];
      need_hp -= S[r]-S[l]; // 累積和で差分をO(1)更新できることに気付くのが本問のポイント
    }
  }
  
  // output
  cout << ans << endl;
}
0