結果

問題 No.545 ママの大事な二人の子供
ユーザー 👑 horiesinitihoriesiniti
提出日時 2016-06-27 19:41:14
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,416 bytes
コンパイル時間 603 ms
コンパイル使用メモリ 69,400 KB
実行使用メモリ 11,264 KB
最終ジャッジ日時 2024-04-16 20:32:56
合計ジャッジ時間 2,081 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 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 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 1 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 4 ms
5,376 KB
testcase_20 AC 7 ms
5,376 KB
testcase_21 AC 1 ms
5,376 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 51 ms
11,136 KB
testcase_26 AC 50 ms
11,136 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:15:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   15 |         scanf("%d",&n);
      |         ~~~~~^~~~~~~~~
main.cpp:18:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   18 |                 scanf("%d %d",&a,&b);
      |                 ~~~~~^~~~~~~~~~~~~~~

ソースコード

diff #

#include<stdio.h>
#include<set>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
long long int myabs(long long int n){
	if(n<0)return -n;
	return n;
}
const int LIMIT=33;
int main(){
	long long int sumAB[LIMIT];
	long long int sumA=0;
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		int a,b;
		scanf("%d %d",&a,&b);
		sumAB[i]=a+b;
		sumA+=a;
	}
	std::set<long long int> dpR,dpL;
	long long int ans=sumA;
	dpL.insert(0);
	std::set<long long int>::iterator it;
	for(int i=0;i<n/2;i++){
		std::set<long long int> add;
		for(it=dpL.begin();it!=dpL.end();it++){
			long long int s=(*it);
			add.insert(s+sumAB[i]);
		}
		dpL.insert(add.begin(),add.end());
	}
	dpR.insert(0);
	for(int i=n/2;i<n;i++){
		std::set<long long int> add;
		for(it=dpR.begin();it!=dpR.end();it++){
			long long int s=(*it);
			add.insert(s+sumAB[i]);
		}
		dpR.insert(add.begin(),add.end());
	}
	it=dpR.begin();
	if(it!=dpR.end()){
		long long int b=(*it);
		ans=std::min(ans,myabs(sumA-b));
	}
	
	for(it=dpL.begin();it!=dpL.end();it++){
		long long int a=(*it);
		long long int b=sumA-a;
		ans=std::min(ans,myabs(b));
		if(b>0){
			std::set<long long int>::iterator itR=dpR.lower_bound(b);
			if(itR!=dpR.end()){
				long long int ab=a+(*itR);
				ans=std::min(ans,myabs(sumA-ab));
			}
			if(itR!=dpR.begin()){
				itR--;
				long long int ab=a+(*itR);
				ans=std::min(ans,myabs(sumA-ab));
			}
		}
	}
	std::cout<<ans<<std::endl;
}
0