結果

問題 No.1251 絶対に間違ってはいけない最小化問題
ユーザー MisterMister
提出日時 2020-10-09 22:18:11
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 129 ms / 2,000 ms
コード長 1,289 bytes
コンパイル時間 1,074 ms
コンパイル使用メモリ 100,848 KB
実行使用メモリ 21,124 KB
最終ジャッジ日時 2023-09-27 17:53:42
合計ジャッジ時間 16,051 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 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 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,500 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 124 ms
19,076 KB
testcase_19 AC 118 ms
18,600 KB
testcase_20 AC 66 ms
12,004 KB
testcase_21 AC 10 ms
4,696 KB
testcase_22 AC 73 ms
12,760 KB
testcase_23 AC 7 ms
4,380 KB
testcase_24 AC 96 ms
15,704 KB
testcase_25 AC 69 ms
12,456 KB
testcase_26 AC 25 ms
6,424 KB
testcase_27 AC 8 ms
4,384 KB
testcase_28 AC 129 ms
21,124 KB
testcase_29 AC 129 ms
19,884 KB
testcase_30 AC 128 ms
19,808 KB
testcase_31 AC 129 ms
19,880 KB
testcase_32 AC 128 ms
20,376 KB
testcase_33 AC 129 ms
20,708 KB
testcase_34 AC 129 ms
20,012 KB
testcase_35 AC 129 ms
20,752 KB
testcase_36 AC 129 ms
19,844 KB
testcase_37 AC 128 ms
20,284 KB
testcase_38 AC 128 ms
20,388 KB
testcase_39 AC 129 ms
20,808 KB
testcase_40 AC 129 ms
19,708 KB
testcase_41 AC 129 ms
20,852 KB
testcase_42 AC 128 ms
19,900 KB
testcase_43 AC 2 ms
4,380 KB
testcase_44 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <map>

using lint = long long;

template <class T>
std::map<T, int> compress(std::vector<T>& v) {
    std::sort(v.begin(), v.end());
    v.erase(std::unique(v.begin(), v.end()), v.end());

    std::map<T, int> rev;
    for (int i = 0; i < (int)v.size(); ++i) rev[v[i]] = i;
    return rev;
}

constexpr lint XINF = 1 << 20;

void solve() {
    int n;
    std::cin >> n;

    std::vector<lint> xs(n), ys(n);
    for (auto& x : xs) std::cin >> x;
    for (auto& y : ys) std::cin >> y;

    auto eval = [&](lint x) {
        lint ret = 0;
        for (int i = 0; i < n; ++i) {
            ret += std::abs(xs[i] - x) * ys[i];
        }
        return ret;
    };

    auto zs = xs;
    zs.push_back(-XINF);
    zs.push_back(XINF);
    compress(zs);

    int m = zs.size();
    int ok = -1, ng = m;
    while (ng - ok > 1) {
        int mid = (ok + ng) / 2;
        if (eval(zs[mid] - 1) > eval(zs[mid])) {
            ok = mid;
        } else {
            ng = mid;
        }
    }

    std::cout << zs[ok] << " " << eval(zs[ok]) << "\n";
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    std::cout << std::fixed << std::setprecision(10);

    solve();

    return 0;
}
0