結果

問題 No.545 ママの大事な二人の子供
ユーザー myantamyanta
提出日時 2017-07-14 23:43:15
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 68 ms / 2,000 ms
コード長 1,229 bytes
コンパイル時間 654 ms
コンパイル使用メモリ 53,992 KB
実行使用メモリ 10,880 KB
最終ジャッジ日時 2024-04-16 20:41:36
合計ジャッジ時間 1,997 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 3 ms
5,376 KB
testcase_19 AC 5 ms
5,376 KB
testcase_20 AC 8 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 31 ms
7,040 KB
testcase_23 AC 68 ms
10,752 KB
testcase_24 AC 29 ms
7,040 KB
testcase_25 AC 67 ms
10,752 KB
testcase_26 AC 67 ms
10,880 KB
testcase_27 AC 64 ms
10,752 KB
testcase_28 AC 68 ms
10,752 KB
testcase_29 AC 64 ms
10,752 KB
testcase_30 AC 65 ms
10,752 KB
testcase_31 AC 65 ms
10,880 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:65:30: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   65 |                         scanf("%lld%lld", &a[i], &b[i]);
      |                         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

#include<cstdio>
#include<vector>
#include<set>
#include<cstdlib>
#include<algorithm>


using namespace std;

using ll=long long;
using vll=vector<ll>;


void min_u(ll&m, ll v)
{
	if(m<0 || m>v)
	{
		m=v;
	}
}


ll search(vll& u, ll ve)
{
	int s=0, e=u.size()-1, c, r=0;

	while(s<=e)
	{
		c=(s+e)/2;
		ll d=u[c]+ve;

//printf("s=%d  e=%d  c=%d  d=%lld  ve=%lld\n", s, e, c, d, ve);
		if(d==0) return 0;
		if(d>0)
		{
			e=c-1;
			r=c-1;
		}
		else
		{
			s=c+1;
			r=c;
		}
	}

	if(r<0) r=0;
	ll ret=llabs(u[r]+ve);
	if(r+1<u.size()) min_u(ret, llabs(u[r+1]+ve));
	return ret;
}


int main(void)
{
	int n;

	while(scanf("%d", &n)==1)
	{
		vll a(n), b(n);
		set<ll> u, v;
		vll s;

		for(int i=0;i<n;i++)
		{
			scanf("%lld%lld", &a[i], &b[i]);
		}

		u.insert(0);
		for(int i=0;i<n/2;i++)
		{
			auto t=u;
			u.clear();
			for(auto te:t)
			{
				u.insert(te+a[i]);
				u.insert(te-b[i]);
			}
		}

		v.insert(0);
		for(int i=n/2;i<n;i++)
		{
			auto t=v;
			v.clear();
			for(auto te:t)
			{
				v.insert(te+a[i]);
				v.insert(te-b[i]);
			}
		}

		s.reserve(v.size());
		for(auto ve:v)
		{
			s.push_back(ve);
		}

		ll ans=-1;
		for(auto ue:u)
		{
			min_u(ans, search(s, ue));
		}
		printf("%lld\n", ans);
	}

	return 0;
}
0