結果

問題 No.1251 絶対に間違ってはいけない最小化問題
ユーザー finefine
提出日時 2020-10-10 00:24:39
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 232 ms / 2,000 ms
コード長 1,210 bytes
コンパイル時間 1,804 ms
コンパイル使用メモリ 176,236 KB
実行使用メモリ 22,820 KB
最終ジャッジ日時 2023-09-27 20:25:33
合計ジャッジ時間 17,404 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 201 ms
21,720 KB
testcase_19 AC 200 ms
21,048 KB
testcase_20 AC 96 ms
13,700 KB
testcase_21 AC 12 ms
5,008 KB
testcase_22 AC 97 ms
14,376 KB
testcase_23 AC 8 ms
4,560 KB
testcase_24 AC 156 ms
17,820 KB
testcase_25 AC 100 ms
14,172 KB
testcase_26 AC 29 ms
7,040 KB
testcase_27 AC 9 ms
4,400 KB
testcase_28 AC 232 ms
22,640 KB
testcase_29 AC 206 ms
22,632 KB
testcase_30 AC 188 ms
22,572 KB
testcase_31 AC 194 ms
22,620 KB
testcase_32 AC 223 ms
22,568 KB
testcase_33 AC 222 ms
22,616 KB
testcase_34 AC 226 ms
22,820 KB
testcase_35 AC 231 ms
22,620 KB
testcase_36 AC 224 ms
22,640 KB
testcase_37 AC 227 ms
22,640 KB
testcase_38 AC 226 ms
22,516 KB
testcase_39 AC 202 ms
22,624 KB
testcase_40 AC 220 ms
22,560 KB
testcase_41 AC 224 ms
22,524 KB
testcase_42 AC 228 ms
22,620 KB
testcase_43 AC 2 ms
4,376 KB
testcase_44 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

constexpr char newl = '\n';

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;

    map<ll, ll> memo;

    vector<ll> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    
    vector<ll> b(n);
    for (int i = 0; i < n; i++) {
        cin >> b[i];
    }

    ll sum = 0;
    for (int i = 0; i < n; i++) {
        memo[a[i]] += b[i];
        sum += b[i];
    }

    vector<ll> v;
    v.reserve(memo.size());
    for (auto& p : memo) {
        v.push_back(p.first);
    }

    int m = v.size();
    vector<ll> L(m, 0);
    ll cur = memo[v[0]];
    for (int i = 1; i < m; i++) {
        L[i] = L[i - 1] + (v[i] - v[i - 1]) * cur;
        cur += memo[v[i]];
    }
    
    vector<ll> R(m, 0);
    cur = memo[v[m - 1]];
    for (int i = m - 2; i >= 0; i--) {
        R[i] = R[i + 1] + (v[i + 1] - v[i]) * cur;
        cur += memo[v[i]];
    }

    ll ans = 1e18;
    ll x = 1e18;
    for (int i = 0; i < m; i++) {
        ll tmp = L[i] + R[i];
        if (tmp < ans) {
            ans = tmp;
            x = v[i];
        }
    }
    cout << x << " " << ans << newl;
    return 0;
}
0