답안 #100337

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
100337 2019-03-10T12:52:52 Z pzdba Simfonija (COCI19_simfonija) C++14
110 / 110
60 ms 3832 KB
#include <bits/stdc++.h>
using namespace std;
int a[100001], b[100001], c[100001];
long long sum[100001];
int main(){
    int n, k;
    scanf("%d%d", &n, &k);
    for(int i=1;i<=n;i++) scanf("%d", &a[i]);
    for(int i=1;i<=n;i++) scanf("%d", &b[i]);
    for(int i=1;i<=n;i++) c[i] = a[i]-b[i];
    sort(c+1, c+1+n);
    if(k == n){
        printf("%d\n", 0);
        return 0;
    }
    for(int i=1;i<=n;i++) sum[i] = sum[i-1] + c[i];
    int l = 1, r = n-k, p = 1;
    long long ans = 1e18;
    for(int x=1000000;x>=-1000000;x--){
        while(r < n && abs(c[l]+x) > abs(c[r+1]+x)) l++, r++;
        p = max(p, l);
        while(p <= r && c[p]+x < 0) p++;
        long long add1 = (sum[r] - sum[p-1]) + (r-p+1)*1ll*x;
        long long add2 = abs((sum[p-1] - sum[l-1]) + ((p-1)-l+1)*1ll*x);
        ans = min(ans, add1+add2);
    }
    printf("%lld\n", ans);
}

Compilation message

simfonija.cpp: In function 'int main()':
simfonija.cpp:7:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d", &n, &k);
     ~~~~~^~~~~~~~~~~~~~~~
simfonija.cpp:8:32: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(int i=1;i<=n;i++) scanf("%d", &a[i]);
                           ~~~~~^~~~~~~~~~~~~
simfonija.cpp:9:32: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(int i=1;i<=n;i++) scanf("%d", &b[i]);
                           ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 256 KB Output is correct
2 Correct 9 ms 384 KB Output is correct
3 Correct 10 ms 256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 2296 KB Output is correct
2 Correct 44 ms 2296 KB Output is correct
3 Correct 51 ms 2268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 55 ms 2336 KB Output is correct
2 Correct 46 ms 2352 KB Output is correct
3 Correct 55 ms 2340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 2296 KB Output is correct
2 Correct 41 ms 2304 KB Output is correct
3 Correct 41 ms 2312 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 2296 KB Output is correct
2 Correct 42 ms 2296 KB Output is correct
3 Correct 51 ms 2296 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 54 ms 2340 KB Output is correct
2 Correct 53 ms 3672 KB Output is correct
3 Correct 52 ms 3824 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 2296 KB Output is correct
2 Correct 60 ms 2268 KB Output is correct
3 Correct 56 ms 3740 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 59 ms 2296 KB Output is correct
2 Correct 44 ms 3696 KB Output is correct
3 Correct 53 ms 3704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 57 ms 2296 KB Output is correct
2 Correct 51 ms 3708 KB Output is correct
3 Correct 49 ms 3720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 2296 KB Output is correct
2 Correct 57 ms 3704 KB Output is correct
3 Correct 56 ms 3832 KB Output is correct