結果
問題 |
No.2139 K Consecutive Sushi
|
ユーザー |
|
提出日時 | 2024-10-09 23:15:49 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 139 ms / 2,000 ms |
コード長 | 2,570 bytes |
コンパイル時間 | 1,239 ms |
コンパイル使用メモリ | 117,748 KB |
最終ジャッジ日時 | 2025-02-24 17:17:27 |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 31 |
ソースコード
#include<iostream> #include<string> #include<queue> #include<vector> #include<cassert> #include<random> #include<set> #include<map> #include<cassert> #include<unordered_map> #include<bitset> #include<numeric> #include<algorithm> using namespace std; using ll = long long; const int inf=1<<30; const ll INF=1LL<<62; typedef pair<int,ll> P; typedef pair<int,P> PP; const ll MOD=998244353; ll op(ll x, ll y){ //(x,y)を比較する任意の演算子.今回はmaxとした return min(x,y); } ll e(){ //initialize value return INF; } template<class Type, Type (*op)(Type,Type), Type (*e)() > class segment_tree{ public: std::vector<Type> dat; int n=1; segment_tree(int n_){ while(n < n_){ n*=2; } dat = std::vector<Type>(2*n-1,e());// 0,1,2,...,2*n-1,2*n-2 } ~segment_tree(){ std::vector<Type>().swap(dat); } void set(int k,Type a){ update(k,a); } void update(int k,Type a){ k+=n-1; dat[k] = a; while(k>0){ k = (k-1)/2; dat[k]=op(dat[2*k+1],dat[2*k+2]); } } //[a,b) Type query(int a,int b,int k,int l,int r){ if(r<=a || b<=l) return e(); if(a<=l && r<=b) return dat[k]; else{ Type vl = query(a,b,2*k+1,l,(l+r)/2); Type vr = query(a,b,2*k+2,(l+r)/2,r); return op(vl,vr); } } //[a,b)の範囲でのmax Type query(int a,int b){ return query(a,b,0,0,n); } Type operator[](int index){ return get(index); } Type get(int index){ index += n-1; return dat[index]; } void add(int index,Type val){ update(index,op(get(index),val)); } }; int main(){ int N,K; cin>>N>>K; vector<ll> A(N+2); ll totA=0; for(int i=1;i<=N;i++){ cin>>A[i]; totA+=A[i]; } vector<ll> dp(N+2); segment_tree<ll,op,e> seg(N+2); dp[0]=0; seg.update(0,0); for(int i=1;i<=N+1;i++){ //[i-K,i) //A[i]を必ずとる dp[i]=seg.query(max(0,i-K),i)+A[i]; seg.update(i,dp[i]); } // for(int i=1;i<=N+1;i++){ // cout<<"dp["<<i<<"]="<<dp[i]<<endl; // } // cout<<"totA="<<totA<<endl; ll ans=totA-dp[N+1]; cout<<ans<<endl; }