답안 #548771

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
548771 2022-04-14T11:47:33 Z Belgutei Safety (NOI18_safety) C++17
29 / 100
2000 ms 3728 KB
#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;
ll mn1,mx1;
 
void build_tree(){
    zereg[0] = 1;
    for(int i = 0; i <= 30; i++){
        if(zereg[i] >= 5000){
            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];
        dp[i] = 1e9;
    }
    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]);
        }
    }
}
 
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);
}
 
 
int main() {
    IOS
    cin >> n >> h;
    mn1 = 1e9;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
        mn1 = min(mn1,a[i]);
        mx1 = max(mx1,a[i]);
    }
    if(h==0){
        sort(a+1,a+n+1);
        ll ans = 1e18;
        ll sum = 0;
        for(int i = 1; i <= n; i++){
            sum += a[i];
        }
        ll pre_sum = 0;
        for(int i = 1; i <= n; i++){
            sum -= a[i];
            ll tur = a[i] * (i-1);
            tur = tur - pre_sum;
            ll tur1 = a[i] * (n-i);
            tur1 = sum - tur1;
            ans=min(ans,tur + tur1);
            pre_sum += a[i];
        }
        cout << ans;
        return 0;
    }
    build_tree();
    for(int i = 0; i <= 8192; i++){
        dp[i] = abs(i - a[1]);
    }
    for(int i = 2; i <= n; i++){
        tree();
        for(int j = mn1; j <= mx1; j++){
            // (j-h) -> (j+h)

            l = max(j-h,mn1);
            r = min(j+h,mx1);
            mn=1e9;
            dfs(0,0);
            dp[j] = min(dp[j],mn+abs(a[i]-j));
        }
    }
    ll ans = 1e9;
    for(int i=0; i<=5000; i++){
        ans=min(ans,dp[i]);
    }
    cout << ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 2 ms 1108 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 1236 KB Output is correct
2 Correct 33 ms 3488 KB Output is correct
3 Correct 37 ms 3728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 52 ms 512 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 44 ms 568 KB Output is correct
16 Correct 50 ms 468 KB Output is correct
17 Correct 34 ms 532 KB Output is correct
18 Correct 31 ms 512 KB Output is correct
19 Correct 43 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 52 ms 512 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 44 ms 568 KB Output is correct
16 Correct 50 ms 468 KB Output is correct
17 Correct 34 ms 532 KB Output is correct
18 Correct 31 ms 512 KB Output is correct
19 Correct 43 ms 468 KB Output is correct
20 Correct 437 ms 572 KB Output is correct
21 Correct 572 ms 576 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 273 ms 572 KB Output is correct
24 Correct 484 ms 572 KB Output is correct
25 Correct 278 ms 588 KB Output is correct
26 Correct 539 ms 572 KB Output is correct
27 Correct 314 ms 576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 52 ms 512 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 44 ms 568 KB Output is correct
16 Correct 50 ms 468 KB Output is correct
17 Correct 34 ms 532 KB Output is correct
18 Correct 31 ms 512 KB Output is correct
19 Correct 43 ms 468 KB Output is correct
20 Correct 437 ms 572 KB Output is correct
21 Correct 572 ms 576 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 273 ms 572 KB Output is correct
24 Correct 484 ms 572 KB Output is correct
25 Correct 278 ms 588 KB Output is correct
26 Correct 539 ms 572 KB Output is correct
27 Correct 314 ms 576 KB Output is correct
28 Execution timed out 2071 ms 596 KB Time limit exceeded
29 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Runtime error 2 ms 1108 KB Execution killed with signal 11
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Runtime error 2 ms 1108 KB Execution killed with signal 11
14 Halted 0 ms 0 KB -