답안 #104594

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
104594 2019-04-08T09:59:07 Z Hassoony Simfonija (COCI19_simfonija) C++17
66 / 110
61 ms 860 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,b[MX];
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{
//        cout<<p1<<" "<<p2<<endl;
        ll ans1=0;
        p1++;
        int x=a[(p1+p2)/2];
        for(int i=p1;i<=p2;i++){
            b[i]=a[i];
            a[i]-=x;
            ans1+=abs(a[i]);
        }
        p1--;p2--;
        x=b[(p1+p2)/2];
        for(int i=p1;i<=p2;i++){
            b[i]-=x;
            ans+=abs(b[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 2 ms 256 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 768 KB Output is correct
2 Correct 38 ms 768 KB Output is correct
3 Correct 40 ms 640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 752 KB Output is correct
2 Correct 42 ms 768 KB Output is correct
3 Correct 35 ms 640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 768 KB Output is correct
2 Correct 61 ms 768 KB Output is correct
3 Correct 39 ms 768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 768 KB Output is correct
2 Correct 34 ms 640 KB Output is correct
3 Correct 41 ms 768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 40 ms 788 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 768 KB Output is correct
2 Correct 56 ms 860 KB Output is correct
3 Incorrect 37 ms 812 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 54 ms 736 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 640 KB Output is correct
2 Incorrect 35 ms 640 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 640 KB Output is correct
2 Correct 33 ms 768 KB Output is correct
3 Correct 45 ms 760 KB Output is correct