結果
問題 | No.704 ゴミ拾い Medium |
ユーザー |
![]() |
提出日時 | 2018-06-16 08:24:09 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 307 ms / 1,500 ms |
コード長 | 3,191 bytes |
コンパイル時間 | 1,987 ms |
コンパイル使用メモリ | 182,916 KB |
実行使用メモリ | 51,368 KB |
最終ジャッジ日時 | 2024-06-30 16:08:19 |
合計ジャッジ時間 | 10,245 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 44 |
ソースコード
#include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<b;i++) #define rrep(i,a,b) for(int i=a;i>=b;i--) #define fore(i,a) for(auto &i:a) #define all(x) (x).begin(),(x).end() #pragma GCC optimize ("-O3") using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); } typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60; template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; } //--------------------------------------------------------------------------------------------------- #define def infl template<class V, int NV> struct SegTree { //[l,r) V comp(V& l, V& r) { return min(l,r); }; vector<V> val; SegTree() { val = vector<V>(NV * 2, def); } V get(int x,int y,int l=0,int r=NV,int k=1){if(r<=x||y<=l)return def;if(x<=l&&r<=y)return val[k]; auto a=get(x,y,l,(l+r)/2,k*2);auto b=get(x,y,(l+r)/2,r,k*2+1);return comp(a,b);} void update(int i, V v) { i+=NV;val[i]=v;while(i>1)i>>=1,val[i]=comp(val[i*2],val[i*2+1]); } void add(int i, V v) { update(i, val[i + NV] + v); } V operator[](int x) { return get(x, x + 1); }}; /*--------------------------------------------------------------------------------------------------- ∧_∧ ∧_∧ (´<_` ) Welcome to My Coding Space! ( ´_ゝ`) / ⌒i / \ | | / / ̄ ̄ ̄ ̄/ | __(__ニつ/ _/ .| .|____ \/____/ (u ⊃ ---------------------------------------------------------------------------------------------------*/ int N; ll A[301010], X[301010], Y[301010]; ll dp[301010]; SegTree<ll, 1 << 20> st1, st2; vector<int> dic; //--------------------------------------------------------------------------------------------------- void _main() { cin >> N; rep(i, 1, N + 1) cin >> A[i]; rep(i, 1, N + 1) cin >> X[i]; rep(i, 1, N + 1) cin >> Y[i]; rep(i, 0, N + 1) dp[i] = infl; dp[0] = 0; /* O(N^2) ver1 rep(i, 1, N + 1) { rep(j, 0, i) { ll dx = abs(A[i] - X[j + 1]); ll dy = Y[j + 1]; ll z = dp[j] + dx + dy; chmin(dp[i], z); } }*/ /* O(N^2) ver2 rep(i, 1, N + 1) { rep(j, 0, i) if (A[i] < X[j + 1]) chmin(dp[i], dp[j] + X[j + 1] - A[i] + Y[j + 1]); rep(j, 0, i) if (A[i] >= X[j + 1]) chmin(dp[i], dp[j] + A[i] - X[j + 1] + Y[j + 1]); }*/ rep(i, 1, N + 1) dic.push_back(A[i]); rep(i, 1, N + 1) dic.push_back(X[i]); sort(all(dic)); dic.erase(unique(all(dic)), dic.end()); rep(i, 1, N + 1) { int x = lower_bound(all(dic), X[i]) - dic.begin(); int a = lower_bound(all(dic), A[i]) - dic.begin(); st1.update(x, min(st1[x], dp[i - 1] + X[i] + Y[i])); st2.update(x, min(st2[x], dp[i - 1] - X[i] + Y[i])); chmin(dp[i], st1.get(a, 1 << 20) - A[i]); chmin(dp[i], st2.get(0, a) + A[i]); } cout << dp[N] << endl; }