답안 #104590

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
104590 2019-04-08T09:53:21 Z Hassoony Simfonija (COCI19_simfonija) C++17
66 / 110
58 ms 2168 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const ll mod=(1e9+7);
const ll inf=(1ll<<61);
const int MX=1e5+9;
int n,k,a[MX],x;
int main(){
    cin>>n>>k;
    for(int i=0;i<n;i++)scanf("%d",&a[i]);
    for(int i=0;i<n;i++)scanf("%d",&x),a[i]-=x;
    sort(a,a+n);
    int p1=0,p2=n-1;
    ll ans=0;
    bool ok=0;
    while(k--){
        int mid=(p1+p2)/2;
        if(abs(a[mid]-a[p1]) > abs(a[p2]-a[mid]))p1++;
        else if(abs(a[mid]-a[p1]) < abs(a[p2]-a[mid]))p2--;
        else{
            if(k>=1){
                p1++;p2--;
                k--;
            }
            else ok=1;
        }
    }
    if(!ok){
        int x=a[(p1+p2)/2];
        for(int i=p1;i<=p2;i++){
            a[i]-=x;
            ans+=abs(a[i]);
        }
        cout<<ans<<endl;
    }
    else{
        ll ans1=0;
        p1++;
        int x=a[(p1+p2)/2];
        for(int i=p1;i<=p2;i++){
            a[i]-=x;
            ans1+=abs(a[i]);
        }
        p1--;p2--;
        x=a[(p1+p2)/2];
        for(int i=p1;i<=p2;i++){
            a[i]-=x;
            ans+=abs(a[i]);
        }
        cout<<min(ans,ans1)<<endl;
    }
}

Compilation message

simfonija.cpp: In function 'int main()':
simfonija.cpp:11:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(int i=0;i<n;i++)scanf("%d",&a[i]);
                         ~~~~~^~~~~~~~~~~~
simfonija.cpp:12:39: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(int i=0;i<n;i++)scanf("%d",&x),a[i]-=x;
                         ~~~~~~~~~~~~~~^~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 45 ms 632 KB Output is correct
2 Correct 32 ms 768 KB Output is correct
3 Correct 35 ms 768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 39 ms 788 KB Output is correct
2 Correct 48 ms 640 KB Output is correct
3 Correct 36 ms 640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 888 KB Output is correct
2 Correct 35 ms 768 KB Output is correct
3 Correct 46 ms 640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 768 KB Output is correct
2 Correct 33 ms 768 KB Output is correct
3 Correct 33 ms 784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 38 ms 640 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 56 ms 780 KB Output is correct
2 Correct 55 ms 768 KB Output is correct
3 Incorrect 58 ms 2168 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 48 ms 768 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 684 KB Output is correct
2 Incorrect 40 ms 2156 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 780 KB Output is correct
2 Correct 40 ms 2168 KB Output is correct
3 Correct 42 ms 2168 KB Output is correct