結果

問題 No.1251 絶対に間違ってはいけない最小化問題
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2020-10-09 22:09:09
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,775 bytes
コンパイル時間 1,931 ms
コンパイル使用メモリ 174,604 KB
実行使用メモリ 9,480 KB
最終ジャッジ日時 2023-09-27 17:26:34
合計ジャッジ時間 17,045 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 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,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,380 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 135 ms
9,140 KB
testcase_19 AC 131 ms
8,768 KB
testcase_20 AC 74 ms
6,260 KB
testcase_21 AC 12 ms
4,376 KB
testcase_22 AC 82 ms
6,880 KB
testcase_23 AC 9 ms
4,380 KB
testcase_24 AC 106 ms
7,788 KB
testcase_25 AC 78 ms
6,568 KB
testcase_26 AC 29 ms
4,376 KB
testcase_27 AC 10 ms
4,376 KB
testcase_28 WA -
testcase_29 AC 142 ms
9,324 KB
testcase_30 AC 139 ms
9,308 KB
testcase_31 AC 141 ms
9,480 KB
testcase_32 AC 140 ms
9,252 KB
testcase_33 AC 141 ms
9,316 KB
testcase_34 AC 142 ms
9,424 KB
testcase_35 AC 140 ms
9,300 KB
testcase_36 AC 142 ms
9,324 KB
testcase_37 AC 142 ms
9,252 KB
testcase_38 AC 141 ms
9,252 KB
testcase_39 AC 141 ms
9,240 KB
testcase_40 AC 142 ms
9,412 KB
testcase_41 AC 141 ms
9,384 KB
testcase_42 AC 141 ms
9,392 KB
testcase_43 AC 2 ms
4,380 KB
testcase_44 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2020.10.09 22:03:53
**/

#include "bits/stdc++.h" 
using namespace std; 
typedef long long ll;

int main() {
    int n;cin >> n;
    vector<pair<ll,ll>> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i].first;
    }
    for (int i = 0; i < n; i++) {
        cin >> a[i].second;
    }
    sort(a.begin(), a.end());
    vector<ll> sum(n+1),sum_b(n+1);
    for (int i = 0; i < n; i++) {
        sum[i+1] = sum[i] + a[i].first * a[i].second;
        sum_b[i+1] = sum_b[i] + a[i].second;
    }
    int l = -1, r = n;
    while(r - l > 2) {
        int mid1 = (r + 2*l) / 3;
        int mid2 = (2*r + l) / 3;
        
        ll sum1 = -sum[mid1]+sum[n]-sum[mid1+1]+(sum_b[mid1]-sum_b[n]+sum_b[mid1+1])*a[mid1].first;
        ll sum2 = -sum[mid2]+sum[n]-sum[mid2+1]+(sum_b[mid2]-sum_b[n]+sum_b[mid2+1])*a[mid2].first;
        if (sum1 > sum2) {
            l = mid1;
        }
        else {
            r = mid2;
        }
    }
    int k = (l+r)/2;
    ll ans1 = 0;
    ll ans2 = 0;
    ll ans3 = 0;
    constexpr long long LLINF = 1e18 + 1;
    if (l != -1) for (int i = 0; i < n; i++) {
        ans1 += abs(a[i].first - a[l].first) * a[i].second;
    }
    else ans1 = LLINF;
    for (int i = 0; i < n; i++) {
        ans2 += abs(a[i].first - a[k].first) * a[i].second;
    }
    if (r != n) for (int i = 0; i < n; i++) {
        ans3 += abs(a[i].first - a[r].first) * a[i].second;
    }
    else ans3 = LLINF;
    if (l != -1 && min({ans1,ans2,ans3}) == ans1) {
        cout << a[l].first << " " << ans1 << endl;
    }
    else if (min({ans1,ans2,ans3}) == ans2) {
        cout << a[k].first << " " << ans2 << endl;
    }
    else {
        cout << a[r].first << " " << ans3 << endl;
    }
    return 0;
}
0