結果

問題 No.545 ママの大事な二人の子供
ユーザー nanophoto12nanophoto12
提出日時 2017-07-16 23:18:33
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 42 ms / 2,000 ms
コード長 1,842 bytes
コンパイル時間 1,095 ms
コンパイル使用メモリ 104,592 KB
実行使用メモリ 9,708 KB
最終ジャッジ日時 2024-04-16 22:39:15
合計ジャッジ時間 2,211 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 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 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 4 ms
5,376 KB
testcase_20 AC 6 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 18 ms
6,400 KB
testcase_23 AC 42 ms
9,576 KB
testcase_24 AC 15 ms
6,400 KB
testcase_25 AC 38 ms
9,708 KB
testcase_26 AC 42 ms
9,704 KB
testcase_27 AC 35 ms
9,708 KB
testcase_28 AC 42 ms
9,704 KB
testcase_29 AC 36 ms
9,584 KB
testcase_30 AC 35 ms
9,708 KB
testcase_31 AC 36 ms
9,580 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <functional>
#include <queue>
#include <iostream>
#include <string.h>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <cstdint>
#include <climits>
#include <unordered_set>
#include <sstream>

using namespace std;

#define ll long long int
#define pii pair<int,int>


std::vector<ll> a;
std::vector<ll> b;

int main()
{
    int n;
    cin >> n;
    a.resize(n);
    b.resize(n);
    for(int i = 0;i < n;i++)
    {
        cin >> a[i] >> b[i];
    }
    std::unordered_set<ll> f;
    f.insert(0);
    for(int i = 0;i < n/2 ;i++)
    {
        auto t = a[i] + b[i];
        std::vector<ll> v;
        for(auto e: f)
        {
            v.push_back(e + t);
        }
        for(auto e : v)
        {
            f.insert(e);
        }
        f.insert(t);
    }
    std::unordered_set<ll> s;
    s.insert(0);
    for(int i = n/2;i < n ;i++)
    {
        auto t = a[i] + b[i];
        std::vector<ll> v;
        for(auto e: s)
        {
            v.push_back(e + t);
        }
        for(auto e : v)
        {
            s.insert(e);
        }
        s.insert(t);
    }
    std::vector<ll> vs;
    
    ll sum = 0;
    for(int i = 0;i < n;i++)
    {
        sum += a[i];
    }
    ll ans = abs(sum);
    for(auto tt : s)
    {
        vs.push_back(tt);
        ans = min(ans, abs(sum - tt));
    }
    std::sort(vs.begin(), vs.end());
    for(auto tt : f)
    {
        auto l = std::lower_bound(vs.begin(), vs.end(), sum - tt);
        auto index = l - vs.begin();
        for(int i = max(0, (int)index-1); i < vs.size(); i++)
        {
            auto c = sum - tt - vs[i];
            ans = min(ans, abs(c));
            if(c < 0)
            {
                break;
            }
            
        }
    }
    cout << ans << endl;
}
0