結果

問題 No.545 ママの大事な二人の子供
ユーザー beetbeet
提出日時 2018-12-15 19:51:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 44 ms / 2,000 ms
コード長 1,147 bytes
コンパイル時間 2,625 ms
コンパイル使用メモリ 209,492 KB
実行使用メモリ 6,704 KB
最終ジャッジ日時 2023-10-25 21:55:56
合計ジャッジ時間 4,318 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 3 ms
4,348 KB
testcase_14 AC 4 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 7 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 3 ms
4,348 KB
testcase_20 AC 5 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 19 ms
5,168 KB
testcase_23 AC 44 ms
6,704 KB
testcase_24 AC 16 ms
5,168 KB
testcase_25 AC 35 ms
6,704 KB
testcase_26 AC 39 ms
6,704 KB
testcase_27 AC 29 ms
6,704 KB
testcase_28 AC 42 ms
6,704 KB
testcase_29 AC 33 ms
6,704 KB
testcase_30 AC 39 ms
6,704 KB
testcase_31 AC 36 ms
6,704 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

//INSERT ABOVE HERE
signed main(){
  Int n;
  cin>>n;
  vector<Int> x(n),y(n);
  for(Int i=0;i<n;i++) cin>>x[i]>>y[i];

  const Int INF = 1e18;
  
  if(n<=16){
    Int ans=INF;
    for(Int b=0;b<(1<<n);b++){
      Int c=0,d=0;
      for(Int i=0;i<n;i++){
        if((b>>i)&1) c+=x[i];
        else d+=y[i];
      }
      chmin(ans,abs(c-d));
    }
    cout<<ans<<endl;
    return 0;
  }

  Int h=n/2,r=n-h;
  set<Int> ss;
  for(Int b=0;b<(1<<h);b++){
    Int c=0,d=0;
    for(Int i=0;i<h;i++){
      if((b>>i)&1) c+=x[i];
      else d+=y[i];
    }
    ss.emplace(d-c);
  }
  
  Int ans=INF;
  for(Int b=0;b<(1<<r);b++){
    Int c=0,d=0;
    for(Int i=0;i<r;i++){
      if((b>>i)&1) c+=x[h+i];
      else d+=y[h+i];
    }
    auto it=ss.lower_bound(c-d);
    if(it!=ss.end()) chmin(ans,abs(*it-(c-d)));
    if(it!=ss.begin()) --it;
    if(it!=ss.end()) chmin(ans,abs(*it-(c-d)));    
  }
  cout<<ans<<endl;  
  return 0;
}
0