Submission #548720

#TimeUsernameProblemLanguageResultExecution timeMemory
548720BelguteiSafety (NOI18_safety)C++17
7 / 100
2060 ms1484 KiB
#include<bits/stdc++.h> using namespace std; #define ll long long #define ff first #define ss second #define pb push_back #define mk make_pair #define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); void debug_out() { cerr << endl; } template<typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << ' ' << H; debug_out(T...); } #ifdef BE_DEBUG #define debug(...) cerr << "\033[1;31m(" << #__VA_ARGS__ << "):\033[0m", debug_out(__VA_ARGS__) #else #define debug(...) #endif ll n,h; ll a[200005]; ll dp[10005]; ll tur[10005]; ll level,zereg[30]; vector<ll> v[30]; ll l,r; ll mn; void build_tree(){ zereg[0] = 1; for(int i = 0; i <= 30; i++){ if(zereg[i] >= 16){ level = i; break; } zereg[i+1] = zereg[i] * 2; } for(int i=0; i<=level; i++){ for(int j=0; j<zereg[i]; j++){ v[i].pb(1e9); } } } void tree(){ for(int i=0; i<=5000; i++){ v[level][i] = dp[i]; } for(int i=level-1; i>=0; i--){ for(int j=0; j<zereg[i]; j++){ v[i][j]=min(v[i+1][j*2],v[i+1][j*2+1]); } } /* for(int i=0; i<=level; i++){ for(int j=0; j<v[i].size(); j++){ cout<<v[i][j]<<" "; } cout<<"\n"; } cout<<'\n'; */ } void dfs(int k, int x){ int y=zereg[level-k]*x; int z=zereg[level-k]*(x+1)-1; if(l<=y && z<=r){ mn = min(mn,v[k][x]); return; } if(z<l || y>r || k==level) return; dfs(k+1,x*2); dfs(k+1,x*2+1); } void copy(){ for(int i=0; i<=5000; i++){ tur[i] = dp[i]; dp[i] = 1e9; } } int main() { IOS cin >> n >> h; for(int i = 1; i <= n; i++){ cin >> a[i]; } build_tree(); for(int i = 0; i <= 8192; i++){ dp[i] = abs(i - a[1]); } for(int i = 2; i <= n; i++){ tree(); copy(); for(int j = 0; j <= 5000; j++){ // (j-h) -> (j+h) ll zero = 0; l = max(j-h,zero); ll fivek = 5000; r = min(j+h,fivek); mn=1e9; dfs(0,0); dp[j] = min(dp[j],mn+abs(a[i]-j)); } } ll ans = 1e18; for(int i=0; i<=5000; i++){ ans=min(ans,dp[i]); } cout << ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...