Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
贴一组 hack 数据及 AC 代码~Input: 5 3 -3 2 -1 -1 5 2 -1 -1 3 -3 0 Output: 0 2 0 2 附上 AC 代码: #include <stdio.h> #include <map> #include <vector> #include <algorithm> #define abs(x) (x<0?-(x):x) #define rep(i, s, t) for(int i=(s); i<(t); ++i) #define F first #define S second #define ll long long const int N=40; ll inf=1e18; using namespace std; int n; ll a[N]; map<ll, ll> mp; typedef map<ll, ll>::iterator mpit; pair<ll, ll> ans; void Solve() { mp.clear(); ans.F=ans.S=inf; rep(i, 0, n) scanf("%I64d", a+i); int n1=n/2, n2=n-n1; rep(msk, 1, 1<<n1) { ll w=0, s=0; rep(i, 0, n1) if(msk>>i&1) w++, s+=a[i]; ans=min(ans, make_pair(abs(s), w)); if(!mp[s]) mp[s]=w; else mp[s]=min(mp[s], w); } rep(msk, 1, 1<<n2) { ll w=0, s=0; rep(i, 0, n2) if(msk>>i&1) w++, s+=a[i+n1]; ans=min(ans, make_pair(abs(s), w)); mpit i=mp.lower_bound(-s); if(i!=mp.end()) ans=min(ans, make_pair(abs(i->F+s), i->S+w)); if(i!=mp.begin()) i--, ans=min(ans, make_pair(abs(i->F+s), i->S+w)); } printf("%I64d %I64d\n", ans.F, ans.S); } signed main() { while(scanf("%d", &n) && n) Solve(); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator