結果
| 問題 | No.1251 絶対に間違ってはいけない最小化問題 | 
| コンテスト | |
| ユーザー |  eve__fuyuki | 
| 提出日時 | 2024-04-13 14:14:03 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 62 ms / 2,000 ms | 
| コード長 | 804 bytes | 
| コンパイル時間 | 2,254 ms | 
| コンパイル使用メモリ | 199,896 KB | 
| 最終ジャッジ日時 | 2025-02-21 01:03:02 | 
| ジャッジサーバーID (参考情報) | judge1 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 42 | 
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:34:34: warning: ‘x’ may be used uninitialized [-Wmaybe-uninitialized]
   34 |         ans += ab[i].second * abs(ab[i].first - x);
      |                               ~~~^~~~~~~~~~~~~~~~~
main.cpp:24:15: note: ‘x’ was declared here
   24 |     long long x;
      |               ^
            
            ソースコード
#include <bits/stdc++.h>
using namespace std;
void fast_io() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
}
int main() {
    fast_io();
    int n;
    cin >> n;
    vector<pair<long long, long long>> ab(n);
    for (int i = 0; i < n; i++) {
        cin >> ab[i].first;
    }
    long long tot = 0;
    for (int i = 0; i < n; i++) {
        cin >> ab[i].second;
        tot += ab[i].second;
    }
    sort(ab.begin(), ab.end());
    long long cur = 0;
    long long x;
    for (int i = 0; i < n; i++) {
        cur += ab[i].second;
        if (cur >= (tot + 1) / 2) {
            x = ab[i].first;
            break;
        }
    }
    long long ans = 0;
    for (int i = 0; i < n; i++) {
        ans += ab[i].second * abs(ab[i].first - x);
    }
    cout << x << " " << ans << endl;
}
            
            
            
        