Timus: 1005 (Stone Pile)



// http://acm.timus.ru/problem.aspx?space=1&num=1005
// Runtime: 0.109s
// Tag: adhoc, dp, brute force 

// @BEGIN_OF_SOURCE_CODE

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>

#define INF_MAX 2147483647
#define INF_MIN -2147483647
#define pi acos(-1.0)
#define N 1000000
#define LL long long

#define For(i, a, b) for( int i = (a); i < (b); i++ )
#define Fors(i, sz) for( size_t i = 0; i < sz.size (); i++ )
#define Fore(it, x) for(typeof (x.begin()) it = x.begin(); it != x.end (); it++)
#define Set(a, s) memset(a, s, sizeof (a))
#define Read(r) freopen(r, "r", stdin)
#define Write(w) freopen(w, "w", stdout)

using namespace std;

int main ()
{
    int n;
    int arr [25];

    while ( scanf ("%d", &n) != EOF ) {
        for ( int i = 0; i < n; i++ ) scanf ("%d", &arr [i]);

        int lim = 1 << n;
        int mini = INF_MAX;

        while ( lim ) {
            int sum = 0;
            for ( int i = 0; i < n; i++ ) {
                if ( lim & (1 << i) ) sum += arr [i];
                else sum -= arr [i];
            }
            if ( sum >= 0 && mini > sum ) mini = sum;
            lim--;
        }

        printf ("%d\n", mini);
    }

	return 0;
}

// @END_OF_SOURCE_CODE
Advertisements

2 thoughts on “Timus: 1005 (Stone Pile)

  1. I reduced the problem into 0-1 Knapsack problem and then solved it with DP. Its execution time is 0.046s. Here’s the link :http://acm.timus.ru/status.aspx?space=1&num=1005&author=me
    Here’s the source code.

    #include
    #include
    using namespace std;

    int64_t Stone[22][1000000];
    int64_t inum;

    int64_t stone_pile(int64_t i, int64_t C, int64_t A[])
    {
    if (i>=inum)
    return Stone[i][C];
    else if (A[i]>C)
    {
    if (Stone[i][C]==0)
    {
    Stone[i][C]=stone_pile(i+1,C,A);
    return Stone[i][C];
    }
    else return Stone[i][C];
    }
    else
    {
    if (Stone[i][C]==0)
    {
    Stone[i][C]=max(stone_pile(i+1,C,A),stone_pile(i+1,C-A[i],A)+A[i]);
    return Stone[i][C];
    }
    else return Stone[i][C];
    }
    }

    int main()
    {
    int64_t sum=0 , mid,optimal;
    cin>>inum;
    int64_t A[inum];
    for (int i=0; i>A[i];
    sum=sum+A[i];
    }
    if (sum%2==0) mid=sum/2;
    else mid=sum/2+1;
    optimal=stone_pile(0,mid,A);
    cout<<abs(sum-optimal*2);
    return 0;
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s