結果
問題 |
No.1251 絶対に間違ってはいけない最小化問題
|
ユーザー |
|
提出日時 | 2020-10-10 17:27:42 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 154 ms / 2,000 ms |
コード長 | 770 bytes |
コンパイル時間 | 792 ms |
コンパイル使用メモリ | 77,988 KB |
実行使用メモリ | 7,136 KB |
最終ジャッジ日時 | 2024-07-20 16:04:42 |
合計ジャッジ時間 | 14,710 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 42 |
コンパイルメッセージ
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/iostream:39, from main.cpp:1: In member function '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: /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/ostream:202:25: warning: 'piv' may be used uninitialized [-Wmaybe-uninitialized] 202 | { return _M_insert(__n); } | ~~~~~~~~~^~~~~ main.cpp: In function 'int main()': main.cpp:20:17: note: 'piv' was declared here 20 | ll acc = 0, piv; | ^~~
ソースコード
#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; }