USACO : Sorting a Three-Valued Sequence



/*
ID: tausiq11
PROG: sort3
LANG: C++
 */

// @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 100000
#define LL long long

#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(it, x) for(typeof (x.begin()) it = x.begin(); it != x.end (); it++)
#define Pr(x) for(typeof (x.begin()) it = x.begin(); it != x.end (); it++) cout << *it << " "; cout << endl;
#define Set(a, s) memset(a, s, sizeof (a))
#define Rd(r) freopen(r, "r", stdin)
#define Wt(w) freopen(w, "w", stdout)

using namespace std;

int main ()
{
	Rd ("sort3.in");
	Wt ("sort3.out");

	int n; scanf ("%d", &n);
	int a [1000 + 10];
	int freq [3 + 2]; Set (freq, 0);

	for ( int i = 0; i < n; i++ ) {
		scanf ("%d", &a [i]);
		freq [a [i]]++;
	}

	int groupFreq [3 + 2] [3 + 2]; Set (groupFreq, 0);
	int len = 0;

	for ( int i = 1; i <= 3; i++ ) {
		for ( int j = 0; j < freq [i]; j++ ) {
			groupFreq [i] [a [len]]++;
			len++;
		}
	}

	int cnt = 0;
	int mini;

	// reduce '2' from group 1
	mini = min (groupFreq [1] [2], groupFreq [2] [1]);
	groupFreq [1] [2] -= mini;
	groupFreq [2] [1] -= mini;
	groupFreq [1] [1] += mini;
	groupFreq [2] [2] += mini;
	cnt += mini;

	// reduce '3' from group 1
	mini = min (groupFreq [1] [3], groupFreq [3] [1]);
	groupFreq [1] [3] -= mini;
	groupFreq [3] [1] -= mini;
	groupFreq [1] [1] += mini;
	groupFreq [3] [3] += mini;
	cnt += mini;

	mini = freq [1] - groupFreq [1] [1];
	cnt += mini;
	groupFreq [1] [1] = freq [1];
	groupFreq [2] [1] = groupFreq [3] [1] = 0;
	groupFreq [3] [2] += groupFreq [1] [2];
	groupFreq [2] [3] += groupFreq [1] [3];
	groupFreq [1] [2] = groupFreq [1] [3] = 0;
	cnt += groupFreq [2] [3];

	printf ("%d\n", cnt);


	return 0;
}

// @END_OF_SOURCE_CODE

Judge Response

USER: Shadow King [tausiq11]
TASK: sort3
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 3048 KB]
   Test 2: TEST OK [0.000 secs, 3048 KB]
   Test 3: TEST OK [0.000 secs, 3048 KB]
   Test 4: TEST OK [0.000 secs, 3048 KB]
   Test 5: TEST OK [0.000 secs, 3048 KB]
   Test 6: TEST OK [0.000 secs, 3048 KB]
   Test 7: TEST OK [0.000 secs, 3048 KB]
   Test 8: TEST OK [0.000 secs, 3048 KB]

All tests OK.
Your program ('sort3') produced all correct answers!  This is your
submission #4 for this problem.  Congratulations!
Advertisements

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