結果

問題 No.545 ママの大事な二人の子供
ユーザー 👑 horiesinitihoriesiniti
提出日時 2016-06-27 20:07:05
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 53 ms / 2,000 ms
コード長 1,420 bytes
コンパイル時間 554 ms
コンパイル使用メモリ 68,060 KB
実行使用メモリ 11,264 KB
最終ジャッジ日時 2024-04-16 20:32:59
合計ジャッジ時間 1,886 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 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 2 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 1 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 3 ms
5,376 KB
testcase_19 AC 4 ms
5,376 KB
testcase_20 AC 7 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 26 ms
7,296 KB
testcase_23 AC 52 ms
11,136 KB
testcase_24 AC 23 ms
7,168 KB
testcase_25 AC 51 ms
11,136 KB
testcase_26 AC 51 ms
11,136 KB
testcase_27 AC 49 ms
11,264 KB
testcase_28 AC 53 ms
11,136 KB
testcase_29 AC 52 ms
11,136 KB
testcase_30 AC 48 ms
11,136 KB
testcase_31 AC 48 ms
11,136 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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);
      |         ~~~~~^~~~~~~~~

ソースコード

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=34;
int main(){
	long long int sumAB[LIMIT];
	long long int sumA=0;
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		long long int a,b;
		std::cin>>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