Submission #101742

# Submission time Handle Problem Language Result Execution time Memory
101742 2019-03-19T17:34:00 Z mayhoubsaleh Simfonija (COCI19_simfonija) C++14
110 / 110
126 ms 4264 KB
#include <bits/stdc++.h>
#define ll long long
#define pb push_back

using namespace std;

ll n,k;
ll dif[100005];
ll suf[100005],pre[100005];
ll x;
bool com(ll a,ll b){
    return abs(a-x)>abs(b-x);
}
ll presum(ll l,ll r){
    return pre[r]-pre[l-1];
}
ll sufsum(ll l,ll r){
    return suf[l]-suf[r+1];
}
int main(){
    cin>>n>>k;
    for(ll i=1;i<=n;i++){
        cin>>dif[i];
    }
    for(ll i=1;i<=n;i++){
        ll x;
        cin>>x;
        dif[i]=x-dif[i];
    }

    sort(dif+1,dif+n+1);

    for(ll i=1;i<=n;i++){
        pre[i]=pre[i-1]+dif[i];
    }
    for(ll i=n;i>=1;i--){
        suf[i]=suf[i+1]+dif[i];
    }
    ll ans=1e18;
    ll l=0,r=n-k+1;
    //for(ll i=1;i<=n;i++)cout<<dif[i]<<' ';cout<<endl;
    for(ll i=1;i<=n;i++){
        //cout<<dif[i]*(i-1)-pre[i-1]+suf[i+1]-dif[i]*(n-i)<<endl;
        while(abs(dif[l+1]-dif[i])>abs(dif[r]-dif[i])&&r<=n&&l+1<=n){
            l++;
            r++;
        }
        //cout<<l<<' '<<r<<endl;
        ans=min(ans,dif[i]*(i-1-l)-presum(l+1,i-1)+sufsum(i+1,r-1)-dif[i]*(r-1-i));
        /*if(ans>dif[i]*(i-1)-pre[i-1]+suf[i+1]-dif[i]*(n-i)){
            ans=dif[i]*(i-1)-pre[i-1]+suf[i+1]-dif[i]*(n-i);
            x=dif[i];
        }*/
    }
    cout<<ans<<endl;
    return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 77 ms 2688 KB Output is correct
2 Correct 79 ms 2704 KB Output is correct
3 Correct 88 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 84 ms 2652 KB Output is correct
2 Correct 95 ms 2680 KB Output is correct
3 Correct 88 ms 2712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 111 ms 2624 KB Output is correct
2 Correct 95 ms 2776 KB Output is correct
3 Correct 84 ms 2664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 56 ms 2588 KB Output is correct
2 Correct 76 ms 2708 KB Output is correct
3 Correct 92 ms 2660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 85 ms 2680 KB Output is correct
2 Correct 94 ms 4088 KB Output is correct
3 Correct 92 ms 4088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 87 ms 2680 KB Output is correct
2 Correct 88 ms 2652 KB Output is correct
3 Correct 126 ms 4088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 112 ms 2716 KB Output is correct
2 Correct 84 ms 4168 KB Output is correct
3 Correct 89 ms 4216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 87 ms 2596 KB Output is correct
2 Correct 100 ms 4264 KB Output is correct
3 Correct 93 ms 4188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 95 ms 2680 KB Output is correct
2 Correct 89 ms 4104 KB Output is correct
3 Correct 102 ms 4120 KB Output is correct