結果
| 問題 |
No.545 ママの大事な二人の子供
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-07-16 21:14:47 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,172 bytes |
| コンパイル時間 | 853 ms |
| コンパイル使用メモリ | 95,736 KB |
| 実行使用メモリ | 326,016 KB |
| 最終ジャッジ日時 | 2024-10-08 04:30:57 |
| 合計ジャッジ時間 | 4,713 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 17 WA * 1 TLE * 1 -- * 13 |
コンパイルメッセージ
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/iostream:39,
from main.cpp:3:
In member function 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char; _Traits = std::char_traits<char>]',
inlined from 'int main()' at main.cpp:65:15:
/home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/ostream:167:25: warning: 'min_diff' may be used uninitialized [-Wmaybe-uninitialized]
167 | { return _M_insert(__n); }
| ~~~~~~~~~^~~~~
main.cpp: In function 'int main()':
main.cpp:37:17: note: 'min_diff' was declared here
37 | int64_t min_diff;
| ^~~~~~~~
ソースコード
#include <algorithm>
#include <cmath>
#include <iostream>
#include <unordered_map>
#include <vector>
struct Item {
int64_t a;
int64_t b;
};
struct Sums {
int64_t sum_a;
int64_t sum_b;
};
int64_t get_diff(Sums& s) {
return std::abs(s.sum_a - s.sum_b);
}
int main()
{
int n;
std::cin >> n;
std::vector<Item> items;
items.reserve(n);
for (int i = 0; i < n; i++) {
int64_t a, b;
std::cin >> a >> b;
items.push_back({ a,b });
}
std::unordered_map<int64_t, Sums> dp;
dp.insert({ 0,{0,0} });
int64_t min_diff;
for (int i = 0; i < n; i++) {
min_diff = INT64_MAX;
std::unordered_map<int64_t, Sums> new_map;
new_map.reserve(dp.size() * 2);
for (std::pair<const int64_t, Sums>& si : dp) {
{
Sums s = si.second;
s.sum_a += items[i].a;
int64_t diff = get_diff(s);
new_map[diff] = s;
min_diff = std::min(min_diff, diff);
}
{
Sums s = si.second;
s.sum_b += items[i].b;
int64_t diff = get_diff(s);
new_map[diff] = s;
min_diff = std::min(min_diff, diff);
}
}
dp = std::move(new_map);
}
std::cout << min_diff << std::endl;
}