Scores of Final Examination
On-Screen Keyboard
Tally Counters
Balance Scale
#pragma GCC optimize(3,"Ofast","inline") #include <bits/stdc++.h> using namespace std; typedef long long ll; unordered_map<ll,bool> m1; int n,m,cnt; const int maxn=6e4; ll a[maxn],tot1,tot2,ans; ll w[105],b[105]; inline void dfs(int now,ll sum) { if(now>n) { m1[sum]=1; return; } dfs(now+1,sum); dfs(now+1,sum+w[now]); dfs(now+1,sum-w[now]); } inline ll read() { ll res=0,f=1; char ch=getchar(); while (!isdigit(ch)) { if (ch==‘-‘) { f=-f; } ch=getchar(); } while (isdigit(ch)) { res=(res<<3)+(res<<1)+ch-‘0‘; ch=getchar(); } return f*res; } int main() { while (1) { m=read();n=read(); if (n == 0 && m == 0) break; m1.clear(); cnt = tot1 = tot2 = 0; ans = 0x3f3f3f3f3f3f3f3f; for (int i = 1; i <= m; ++i) a[i]=read(); for (int i = 1; i <= n; ++i) w[i]=read(); dfs(1, 0); unordered_map<ll, bool>::iterator it; for (int i = 1; i <= m; ++i) { if (m1[a[i]]) cnt++; else { b[tot2++] = a[i]; m1.erase(a[i]); } } for (it = m1.begin(); it != m1.end(); ++it) a[tot1++] = it->first; if (cnt == m) { printf("0\n"); continue; } for (int i = 0; i < tot1; ++i) { cnt = 1; ll x = abs(a[i] - b[0]); for (int j = 1; j < tot2; ++j) { if (m1.count(b[j] - x) || m1.count(b[j] + x)) { cnt++; } } if (cnt == tot2) { ans = min(ans, x); } } if (ans == 0x3f3f3f3f3f3f3f3f) printf("-1\n"); else printf("%lld\n", ans); } }
原文地址:https://www.cnblogs.com/Accpted/p/11396087.html
时间: 2024-10-11 18:24:46