Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

贴一组 hack 数据及 AC 代码~

Posted by Jerrywang2009 at 2023-07-30 15:49:50 on Problem 3977 and last updated at 2023-07-30 15:51:35
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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator