Artifact Content
Not logged in

Artifact 22fb8b993e155b65f36d25d70fab88fec2f7de55


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef complex<LD> CMP;

struct UnionFind
{
	vector<int> uf, sz;
	int nc;

	UnionFind(int N): uf(N), sz(N,1), nc(N)
		{ for(int i=0; i<N; ++i) uf[i] = i; }
	int size()
		{ return nc; }
	int size(int a)
		{ return sz[Find(a)]; }
	int Find(int a)
		{ return uf[a]==a ? a : uf[a]=Find(uf[a]); }
	bool Union(int a, int b)
		{
			a = Find(a);
			b = Find(b);
			if( a != b )
			{
				if( sz[a] >= sz[b] ) swap(a, b);
				uf[a] = b;
				sz[b] += sz[a];
				--nc;
			}
			return (a!=b);
		}
};

class NewArenaPassword { public:
	int minChange(string oldPassword, int K)
	{
		int N = oldPassword.size();
		UnionFind uf(N);
		for(int i=0; i<K; ++i)
			uf.Union(i, N-K+i);

		int total = 0;
		for(int r=0; r<N; ++r) if(uf.Find(r) == r) {
			map<char,int> freq;
			for(int i=0; i<N; ++i)
				if(uf.Find(i) == r)
					freq[oldPassword[i]]++;
			vector<int> fff;
			for(map<char,int>::iterator it=freq.begin(); it!=freq.end(); ++it)
				fff.push_back(it->second);
			total += accumulate(fff.begin(), fff.end(), -*max_element(fff.begin(), fff.end()));
		}
		return total;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, NewArenaPassword().minChange(oldPassword, K));}
int main(){

CASE(0)
	string oldPassword = "topcoderopen"; 
	int K = 5; 
	int _ = 3; 
END
CASE(1)
	string oldPassword = "puyopuyo"; 
	int K = 4; 
	int _ = 0; 
END
CASE(2)
	string oldPassword = "loool"; 
	int K = 3; 
	int _ = 1; 
END
CASE(3)
	string oldPassword = "arena"; 
	int K = 5; 
	int _ = 0; 
END
CASE(4)
	string oldPassword = "amavckdkz"; 
	int K = 7; 
	int _ = 5; 
END
/*
CASE(5)
	string oldPassword = ; 
	int K = ; 
	int _ = ; 
END
CASE(6)
	string oldPassword = ; 
	int K = ; 
	int _ = ; 
END
*/
}
// END CUT HERE