Google Code Jam: Store Credit



// http://code.google.com/codejam/contest/351101/dashboard#s=p0
// correct solution for both small and large dataset 

// @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>
#include <ctime>

#define Inf 2147483647
#define Pi acos(-1.0)
#define N 1000000
#define LL long long

const double EPS = 1e-9;
inline bool Equal(double a, double b) { return abs(a-b) < EPS; }
inline LL Power(int b, int p) { LL ret = 1; for ( int i = 1; i <= p; i++ ) ret *= b; return ret; }
const int dr [] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int dc [] = {0, 1, 1, 1, 0, -1, -1, -1};

#define F(i, a, b) for( int i = (a); i < (b); i++ )
#define Fs(i, sz) for( size_t i = 0; i < sz.size (); i++ )
#define Fe(i, x) for(typeof (x.begin()) i = x.begin(); i != x.end (); i++)
#define Set(a, s) memset(a, s, sizeof (a))
#define max(a, b)  (a < b ? b : a)
#define min(a, b)  (a > b ? b : a)

using namespace std;


int main ()
{
    freopen ("in.txt", "r", stdin);
    freopen ("out.txt", "w", stdout);

    int testCase; scanf ("%d", &testCase);
    int cases = 0;

    while ( testCase-- ) {
        int c, i; scanf ("%d %d", &c, &i);
        int p [2000 + 10];
        map <int, int> freq;

        F(j, 0, i) {
            scanf ("%d", &p [j]);
            freq [p [j]]++;
        }

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

        F(j, 0, i) {
            freq [p [j]]--;
            if ( freq [c - p [j]] ) {
                printf ("%d ", j + 1);

                F(k, j + 1, i) {
                    if ( p [k] == c - p [j] ) { printf ("%d\n", k + 1); break; }
                }

                break;
            }

            freq [p [j]]++;
        }
    }

	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