UVa : 10487 (Closest Sums)



// http://uva.onlinejudge.org/external/104/10487.html
// Linear Search 
// Runtime : 0.048s

// @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 INT_MAX 2147483647
#define INT_MIN -2147483647
#define pi acos(-1.0)
#define N 1000000
#define LL long long
using namespace std;

int sum [1000 * 1000 + 10];

int main ()
{
    int n;
    int cases = 0;

    while ( scanf ("%d", &n) && n ) {
        int nums [1000 + 10];

        for ( int i = 0; i < n; i++ ) scanf ("%d", &nums [i]);

        int sum_len = 0;

        for ( int i = 0; i < n; i++ ) {
            for ( int j = i + 1; j < n; j++ ) sum [sum_len++] = nums [i] + nums [j];
        }

        int m;
        scanf ("%d", &m);

        printf ("Case %d:\n", ++cases);

        for ( int i = 0; i < m; i++ ) {
            int query;
            scanf ("%d", &query);
            int min_diff = INT_MAX;
            int closest_sum;

            for ( int j = 0; j < sum_len; j++ ) {
                int diff = abs (query - sum [j]);
                if ( diff < min_diff ) { min_diff = diff; closest_sum = sum [j];}
            }

            printf ("Closest sum to %d is %d.\n", query, closest_sum);
        }
    }

	return 0;
}

// @END_OF_SOURCE_CODE

// Binary Search 
// Runtime : 0.104s

// @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 INT_MAX 2147483647
#define INT_MIN -2147483647
#define pi acos(-1.0)
#define N 1000000
#define LL long long
using namespace std;

int sum [1000 * 1000 + 10];

int binarySearch (int l, int h, int q)
{
    if ( h - l == 1 ) {
        int diff1 = abs (sum [l] - q);
        int diff2 = abs (sum [h] - q);
        if ( diff1 < diff2 ) return sum [l];
        return sum [h];
    }
    
    int mid = (l + h) / 2;

    if ( q <= sum [mid] ) return binarySearch (l, mid, q);
    else return binarySearch(mid, h, q);
}

int main ()
{
    int n;
    int cases = 0;

    while ( scanf ("%d", &n) && n ) {
        int nums [1000 + 10];

        for ( int i = 0; i < n; i++ ) scanf ("%d", &nums [i]);

        int sum_len = 0;

        for ( int i = 0; i < n; i++ ) {
            for ( int j = i + 1; j < n; j++ ) sum [sum_len++] = nums [i] + nums [j];
        }

        sort (sum, sum + sum_len);

        int m;
        scanf ("%d", &m);

        printf ("Case %d:\n", ++cases);

        for ( int i = 0; i < m; i++ ) {
            int query;
            scanf ("%d", &query);

            if ( sum_len == 1 )
                printf ("Closest sum to %d is %d.\n", query, sum [0]);
            else
                printf ("Closest sum to %d is %d.\n", query, binarySearch (0, sum_len - 1, query));
        }
    }

	return 0;
}

// @END_OF_SOURCE_CODE

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