답안 #1054238

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1054238 2024-08-12T07:54:32 Z Kipras Safety (NOI18_safety) C++17
9 / 100
2000 ms 2616 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pl;

const ll maxC = 5010;

ll n, h;
vector<ll> diffs;

ll res = 1e18;
ll median = 0;
void solve() {
    vector<ll> a;
    for(int i = 0; i < n; i++) {
        ll aa;
        cin>>aa;
        a.push_back(aa);
    }
    sort(a.begin(), a.end());
    median=a[n/2];
    ll lRes=0;
    for(auto i : a) {
        lRes+=abs(i-median);
    }
    if(n%2==0) {
        median=a[(n+2)/2];
        res=0;
        for(auto i : a) {
            res+=abs(i-median);
        }
    }

    cout<<min(lRes, res)<<"\n";
}

int main() {

    ios_base::sync_with_stdio(false);cin.tie(nullptr);

    cin>>n>>h;
    if(h==0) {
        solve();
        return 0;
    }
    ll aa;
    ll a0;
    cin>>aa;
    a0=aa;
    for(int i = 0; i < n-1; i++) {
        ll bb;
        cin>>bb;
        diffs.push_back(bb-aa);
        //cout<<diffs.back()<<" ";
        aa=bb;
    }
    //cout<<"\n\n\n";

    for(int x = -a0; x <= 5*maxC; x++) {
        //cout<<"doing: "<<x<<"\n";
        vector<ll> tmp;
        for(int i = 0; i < n-1; i++) {
            tmp.push_back(diffs[i]);
        }

        ll lRes = abs(x);
        tmp[0]+=x;
        tmp.push_back(0);

        for(int i = 0; i < n-1; i++) {
            if(tmp[i]==0)continue;
            ll v = abs(tmp[i]);
            if(h>=v)continue;
            ll di = v-h;
            if(tmp[i]<0) {
                tmp[i]+=di;
                tmp[i+1]-=di;
            }else if(tmp[i]>0) {
                tmp[i]-=di;
                tmp[i+1]+=di;
            }
            //cout<<i<<" "<<di<<" "<<tmp[i]<<"\n";
            lRes+=di;
        }
        res=min(res, lRes);
    }

    cout<<res<<endl;
    return 0;

}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 344 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 2 ms 456 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
7 Incorrect 2 ms 452 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 3 ms 452 KB Output is correct
3 Correct 3 ms 348 KB Output is correct
4 Correct 2 ms 348 KB Output is correct
5 Correct 3 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2092 ms 348 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 2520 KB Output is correct
2 Correct 20 ms 2616 KB Output is correct
3 Correct 22 ms 2520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 344 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 2 ms 456 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
7 Incorrect 2 ms 452 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 344 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 2 ms 456 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
7 Incorrect 2 ms 452 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 344 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 2 ms 456 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
7 Incorrect 2 ms 452 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 344 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 2 ms 456 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
7 Incorrect 2 ms 452 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 344 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 2 ms 456 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
7 Incorrect 2 ms 452 KB Output isn't correct
8 Halted 0 ms 0 KB -