結果

問題 No.545 ママの大事な二人の子供
ユーザー azyobuzin
提出日時 2017-07-16 21:15:27
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,172 bytes
コンパイル時間 3,595 ms
コンパイル使用メモリ 140,724 KB
最終ジャッジ日時 2025-01-05 01:40:51
ジャッジサーバーID
(参考情報)
judge2 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 18 WA * 1 TLE * 13
権限があれば一括ダウンロードができます
コンパイルメッセージ
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.4.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.4.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;
      |                 ^~~~~~~~

ソースコード

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;
}
0