結果

問題 No.1251 絶対に間違ってはいけない最小化問題
ユーザー face4face4
提出日時 2020-10-10 17:27:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 140 ms / 2,000 ms
コード長 770 bytes
コンパイル時間 766 ms
コンパイル使用メモリ 77,900 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2023-09-27 21:33:44
合計ジャッジ時間 17,635 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 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 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 133 ms
6,532 KB
testcase_19 AC 127 ms
6,588 KB
testcase_20 AC 73 ms
4,816 KB
testcase_21 AC 12 ms
4,380 KB
testcase_22 AC 80 ms
4,940 KB
testcase_23 AC 9 ms
4,380 KB
testcase_24 AC 104 ms
6,264 KB
testcase_25 AC 77 ms
5,028 KB
testcase_26 AC 29 ms
4,376 KB
testcase_27 AC 9 ms
4,376 KB
testcase_28 AC 139 ms
6,948 KB
testcase_29 AC 138 ms
6,728 KB
testcase_30 AC 139 ms
6,776 KB
testcase_31 AC 139 ms
6,652 KB
testcase_32 AC 138 ms
6,772 KB
testcase_33 AC 139 ms
6,648 KB
testcase_34 AC 139 ms
6,600 KB
testcase_35 AC 138 ms
6,680 KB
testcase_36 AC 137 ms
6,616 KB
testcase_37 AC 137 ms
6,720 KB
testcase_38 AC 137 ms
6,608 KB
testcase_39 AC 138 ms
6,772 KB
testcase_40 AC 137 ms
6,772 KB
testcase_41 AC 140 ms
6,944 KB
testcase_42 AC 138 ms
6,600 KB
testcase_43 AC 2 ms
4,376 KB
testcase_44 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
次のファイルから読み込み:  /usr/local/gcc7/include/c++/12.2.0/iostream:39,
         次から読み込み:  main.cpp:1:
メンバ関数 ‘std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char; _Traits = std::char_traits<char>]’ 内,
    inlined from ‘int main()’ at main.cpp:32:13:
/usr/local/gcc7/include/c++/12.2.0/ostream:202:25: 警告: ‘piv’ may be used uninitialized [-Wmaybe-uninitialized]
  202 |       { return _M_insert(__n); }
      |                ~~~~~~~~~^~~~~
main.cpp: 関数 ‘int main()’ 内:
main.cpp:20:17: 備考: ‘piv’ はここで定義されています
   20 |     ll acc = 0, piv;
      |                 ^~~

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
#include<numeric>
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;

int main(){
    int n;
    cin >> n;
    int a[n], b[n];
    for(int i = 0; i < n; i++)  cin >> a[i];
    for(int i = 0; i < n; i++)  cin >> b[i];
    vector<pii> vp;
    for(int i = 0; i < n; i++)  vp.push_back({a[i], b[i]});
    sort(vp.begin(), vp.end());
    ll sum = accumulate(b, b+n, 0ll);
    ll acc = 0, piv;
    for(int i = 0; i < n; i++){
        acc += vp[i].second;
        if(acc >= (sum+1)/2){
            piv = vp[i].first;
            break;
        }
    }
    ll ans = 0;
    for(int i = 0; i < n; i++){
        ans += (ll)b[i] * abs(piv-a[i]);
    }
    cout << piv << " " << ans << endl;
    return 0;
}
0