답안 #990549

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
990549 2024-05-30T15:54:55 Z LOLOLO Simfonija (COCI19_simfonija) C++17
44 / 110
498 ms 6196 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
 
#define           f     first
#define           s     second
#define           pb    push_back
#define           ep    emplace
#define           eb    emplace_back
#define           lb    lower_bound
#define           ub    upper_bound
#define       all(x)    x.begin(), x.end()
#define      rall(x)    x.rbegin(), x.rend()
#define   uniquev(v)    sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define     mem(f,x)    memset(f , x , sizeof(f))
#define        sz(x)    (ll)(x).size()
#define  __lcm(a, b)    (1ll * ((a) / __gcd((a), (b))) * (b))
#define          mxx    *max_element
#define          mnn    *min_element
#define    cntbit(x)    __builtin_popcountll(x)
#define       len(x)    (int)(x.length())
 
const int N = 2e5 + 10;
ll a[N], b[N];

ll cost(int n, int k, ll add) {
    vector <ll> c;
    for (int i = 1; i <= n; i++) {
        c.pb(b[i] + add);
    }

    ll ans = 0;
    sort(all(c), [&] (ll a, ll b) {return abs(a) < abs(b);});
    for (int i = 0; i < n - k; i++)
        ans += abs(c[i]);

    return ans;
}

ll solve() {
    int n, k;
    cin >> n >> k;

    for (int i = 1; i <= n; i++)
        cin >> a[i];

    for (int i = 1; i <= n; i++) {
        cin >> b[i];
        b[i] = a[i] - b[i];
    }

    ll l = -1e9, r = 1e9, m = 0, ans = 1e18;
    while (l <= r) {
        m = (l + r) / 2;
        ll L = cost(n, k, m), R = cost(n, k, m + 1);
        if (L > R) {
            l = m + 1;
        } else {
            r = m - 1;
        }

        ans = min(ans, min(L, R));
    }

    return ans;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
 
    int t = 1;
    //cin >> t;
 
    while (t--) {
        cout << solve() << '\n';
    }
 
    return 0;
} 
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 470 ms 6064 KB Output is correct
2 Correct 366 ms 6032 KB Output is correct
3 Correct 256 ms 6060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 438 ms 6028 KB Output is correct
2 Correct 359 ms 6040 KB Output is correct
3 Correct 256 ms 6040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 456 ms 6032 KB Output is correct
2 Correct 366 ms 6196 KB Output is correct
3 Correct 259 ms 6064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 209 ms 5424 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 498 ms 6140 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 230 ms 6040 KB Output is correct
2 Incorrect 294 ms 6132 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 477 ms 6032 KB Output is correct
2 Incorrect 332 ms 6180 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 433 ms 6064 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 290 ms 6060 KB Output isn't correct
2 Halted 0 ms 0 KB -