結果
問題 | No.545 ママの大事な二人の子供 |
ユーザー |
![]() |
提出日時 | 2019-12-27 14:55:14 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 23 ms / 2,000 ms |
コード長 | 2,964 bytes |
コンパイル時間 | 1,054 ms |
コンパイル使用メモリ | 106,928 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-10-08 05:09:32 |
合計ジャッジ時間 | 2,128 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 32 |
ソースコード
/* Author:zeke pass System Test! GET AC!! */ #include <iostream> #include <queue> #include <vector> #include <iostream> #include <vector> #include <string> #include <cassert> #include <algorithm> #include <functional> #include <cmath> #include <queue> #include <set> #include <stack> #include <deque> #include <map> #include <iomanip> #include <utility> #include <stack> #include <bitset> using ll = long long; using ld = long double; using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(x) (x).begin(), (x).end() #define rep3(var, min, max) for (ll(var) = (min); (var) < (max); ++(var)) #define repi3(var, min, max) for (ll(var) = (max)-1; (var) + 1 > (min); --(var)) #define Mp(a, b) make_pair((a), (b)) #define F first #define S second #define Icin(s) \ ll(s); \ cin >> (s); #define Scin(s) \ ll(s); \ cin >> (s); template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } typedef pair<ll, ll> P; typedef vector<ll> V; typedef vector<V> VV; typedef vector<P> VP; ll mod = 1e9 + 7; ll MOD = 1e9 + 7; ll INF = 1e18; vector<vector<ll>> calcNext(const string &S) { int n = (int)S.size(); vector<vector<ll>> res(n + 1, vector<ll>(26, n)); for (int i = n - 1; i >= 0; --i) { for (int j = 0; j < 26; ++j) res[i][j] = res[i + 1][j]; res[i][S[i] - 'a'] = i; } return res; } int main() { cin.tie(0); ios::sync_with_stdio(false); ll n; cin>>n; VP vec(n); VP vec2; rep(i,n)cin>>vec[i].F>>vec[i].S; ll m = n / 2; for(int i=m;i<n;i++){ vec2.push_back(vec[i]); } // cout<<vec2.size()<<endl; V temp; rep(i,(1<<m)){ ll res = 0; rep(j,m){ if(i&(1<<j)){ res += vec[j].F; }else{ res -= vec[j].S; } } temp.push_back(res); } V temp2; // rep(i,vec2.size())cout<<vec2[i].F<<" "<<vec2[i].S<<endl;; // cout<<endl; rep(i,(1<<(vec2.size()))){ ll res1 = 0; rep(j,vec2.size()){ if(i&(1<<j)){ res1 -= vec2[j].F; }else{ res1 += vec2[j].S; } } temp2.push_back(res1); } //rep(i,temp2.size())cout<<temp2[i]<<" "; // cout<<endl; sort(all(temp2)); //cout<<temp2.size()<<endl; //cout<<temp.size()<<endl; ll res = 1e18; rep(i,temp.size()){ ll k=lower_bound(all(temp2),temp[i])-temp2.begin(); // cout<<temp[i]<<" "<<temp2[k]<<endl; if(k>=1){ chmin(res,min(abs(-temp2[k]+temp[i]),abs(-temp2[k-1]+temp[i]))); }else{ chmin(res,abs(-temp2[k]+temp[i])); } // cout<<k<<endl; } cout << res << endl; }