結果
問題 |
No.1077 Noelちゃんと星々4
|
ユーザー |
![]() |
提出日時 | 2020-08-26 11:45:16 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,671 ms / 2,000 ms |
コード長 | 1,964 bytes |
コンパイル時間 | 1,041 ms |
コンパイル使用メモリ | 93,400 KB |
実行使用メモリ | 263,552 KB |
最終ジャッジ日時 | 2024-11-07 02:42:07 |
合計ジャッジ時間 | 18,333 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 20 |
ソースコード
#include<iostream> #include<algorithm> #include<vector> #include<string> #include<utility> #include<map> #include<set> #include<queue> #include<stack> #include<functional> #include<math.h> using namespace std; #define N (1000000000+7) #define M (998244353) #define INF 1e16 typedef long long ll; typedef pair<int,int> P; template <typename T> struct SegmentTree{ typedef function<T(T,T)> F; int n; F f; T unit; vector<T> dat; SegmentTree(){}; SegmentTree(int newn,F f,T t):f(f),unit(t){ init(newn); } SegmentTree(const vector<T> &v,F f,T t):f(f),unit(t){ int n_ = v.size(); init(n_); for(int i=0;i<n_;i++)dat[n+i]=v[i]; for(int i=n-1;i;i--){ dat[i]=f(dat[(i<<1)|0],dat[(i<<1)|1]); } } void init(int n_){ n=1; while(n<n_)n<<=1; dat.assign(n<<1,unit); } void update(int k,T x){ dat[k+=n]=x; while(k>>=1) dat[k]=f(dat[(k<<1)|0],dat[(k<<1)|1]); } T query(int a,int b){ T vl=unit,vr=unit; for(int l=a+n,r=b+n;l<r;l>>=1,r>>=1){ if(l&1)vl=f(vl,dat[l++]); if(r&1)vr=f(dat[--r],vr); } return f(vl,vr); } }; SegmentTree<ll> dp[1010]; int main(void){ int n; cin>>n; vector<int>a(n); for(int i=0;i<n;i++)cin>>a[i]; auto f = [](ll a,ll b){ return min(a,b); }; for(int i=0;i<=n;i++){ if(i==0) { dp[i] = SegmentTree<ll>(10001,f,(ll)INF); dp[i].update(0,0); } else dp[i] = SegmentTree<ll>(10001,f,(ll)INF); } for(int i=0;i<n;i++){ for(int j=0;j<10001;j++){ ll A = dp[i].query(0,j+1); if(dp[i+1].query(j,j+1)>A+abs(a[i]-j)){ dp[i+1].update(j,A+abs(a[i]-j)); } } } ll res = (ll)INF; for(int i=0;i<10001;i++)res = min(dp[n].query(i,i+1),res); cout<<res<<endl; }