c0b96abfe5 2013-03-15 kinaba: #include <iostream> c0b96abfe5 2013-03-15 kinaba: #include <sstream> c0b96abfe5 2013-03-15 kinaba: #include <iomanip> c0b96abfe5 2013-03-15 kinaba: #include <vector> c0b96abfe5 2013-03-15 kinaba: #include <string> c0b96abfe5 2013-03-15 kinaba: #include <map> c0b96abfe5 2013-03-15 kinaba: #include <set> c0b96abfe5 2013-03-15 kinaba: #include <algorithm> c0b96abfe5 2013-03-15 kinaba: #include <numeric> c0b96abfe5 2013-03-15 kinaba: #include <iterator> c0b96abfe5 2013-03-15 kinaba: #include <functional> c0b96abfe5 2013-03-15 kinaba: #include <complex> c0b96abfe5 2013-03-15 kinaba: #include <queue> c0b96abfe5 2013-03-15 kinaba: #include <stack> c0b96abfe5 2013-03-15 kinaba: #include <cmath> c0b96abfe5 2013-03-15 kinaba: #include <cassert> c0b96abfe5 2013-03-15 kinaba: using namespace std; c0b96abfe5 2013-03-15 kinaba: typedef long long LL; c0b96abfe5 2013-03-15 kinaba: typedef long double LD; c0b96abfe5 2013-03-15 kinaba: typedef complex<LD> CMP; c0b96abfe5 2013-03-15 kinaba: c0b96abfe5 2013-03-15 kinaba: struct UnionFind c0b96abfe5 2013-03-15 kinaba: { c0b96abfe5 2013-03-15 kinaba: vector<int> uf, sz; c0b96abfe5 2013-03-15 kinaba: int nc; c0b96abfe5 2013-03-15 kinaba: c0b96abfe5 2013-03-15 kinaba: UnionFind(int N): uf(N), sz(N,1), nc(N) c0b96abfe5 2013-03-15 kinaba: { for(int i=0; i<N; ++i) uf[i] = i; } c0b96abfe5 2013-03-15 kinaba: int size() c0b96abfe5 2013-03-15 kinaba: { return nc; } c0b96abfe5 2013-03-15 kinaba: int size(int a) c0b96abfe5 2013-03-15 kinaba: { return sz[Find(a)]; } c0b96abfe5 2013-03-15 kinaba: int Find(int a) c0b96abfe5 2013-03-15 kinaba: { return uf[a]==a ? a : uf[a]=Find(uf[a]); } c0b96abfe5 2013-03-15 kinaba: bool Union(int a, int b) c0b96abfe5 2013-03-15 kinaba: { c0b96abfe5 2013-03-15 kinaba: a = Find(a); c0b96abfe5 2013-03-15 kinaba: b = Find(b); c0b96abfe5 2013-03-15 kinaba: if( a != b ) c0b96abfe5 2013-03-15 kinaba: { c0b96abfe5 2013-03-15 kinaba: if( sz[a] >= sz[b] ) swap(a, b); c0b96abfe5 2013-03-15 kinaba: uf[a] = b; c0b96abfe5 2013-03-15 kinaba: sz[b] += sz[a]; c0b96abfe5 2013-03-15 kinaba: --nc; c0b96abfe5 2013-03-15 kinaba: } c0b96abfe5 2013-03-15 kinaba: return (a!=b); c0b96abfe5 2013-03-15 kinaba: } c0b96abfe5 2013-03-15 kinaba: }; c0b96abfe5 2013-03-15 kinaba: c0b96abfe5 2013-03-15 kinaba: class NewArenaPassword { public: c0b96abfe5 2013-03-15 kinaba: int minChange(string oldPassword, int K) c0b96abfe5 2013-03-15 kinaba: { c0b96abfe5 2013-03-15 kinaba: int N = oldPassword.size(); c0b96abfe5 2013-03-15 kinaba: UnionFind uf(N); c0b96abfe5 2013-03-15 kinaba: for(int i=0; i<K; ++i) c0b96abfe5 2013-03-15 kinaba: uf.Union(i, N-K+i); c0b96abfe5 2013-03-15 kinaba: c0b96abfe5 2013-03-15 kinaba: int total = 0; c0b96abfe5 2013-03-15 kinaba: for(int r=0; r<N; ++r) if(uf.Find(r) == r) { c0b96abfe5 2013-03-15 kinaba: map<char,int> freq; c0b96abfe5 2013-03-15 kinaba: for(int i=0; i<N; ++i) c0b96abfe5 2013-03-15 kinaba: if(uf.Find(i) == r) c0b96abfe5 2013-03-15 kinaba: freq[oldPassword[i]]++; c0b96abfe5 2013-03-15 kinaba: vector<int> fff; c0b96abfe5 2013-03-15 kinaba: for(map<char,int>::iterator it=freq.begin(); it!=freq.end(); ++it) c0b96abfe5 2013-03-15 kinaba: fff.push_back(it->second); c0b96abfe5 2013-03-15 kinaba: total += accumulate(fff.begin(), fff.end(), -*max_element(fff.begin(), fff.end())); c0b96abfe5 2013-03-15 kinaba: } c0b96abfe5 2013-03-15 kinaba: return total; c0b96abfe5 2013-03-15 kinaba: } c0b96abfe5 2013-03-15 kinaba: }; c0b96abfe5 2013-03-15 kinaba: c0b96abfe5 2013-03-15 kinaba: // BEGIN CUT HERE c0b96abfe5 2013-03-15 kinaba: #include <ctime> c0b96abfe5 2013-03-15 kinaba: double start_time; string timer() c0b96abfe5 2013-03-15 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } c0b96abfe5 2013-03-15 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) c0b96abfe5 2013-03-15 kinaba: { os << "{ "; c0b96abfe5 2013-03-15 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) c0b96abfe5 2013-03-15 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } c0b96abfe5 2013-03-15 kinaba: void verify_case(const int& Expected, const int& Received) { c0b96abfe5 2013-03-15 kinaba: bool ok = (Expected == Received); c0b96abfe5 2013-03-15 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; c0b96abfe5 2013-03-15 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } c0b96abfe5 2013-03-15 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); c0b96abfe5 2013-03-15 kinaba: #define END verify_case(_, NewArenaPassword().minChange(oldPassword, K));} c0b96abfe5 2013-03-15 kinaba: int main(){ c0b96abfe5 2013-03-15 kinaba: c0b96abfe5 2013-03-15 kinaba: CASE(0) c0b96abfe5 2013-03-15 kinaba: string oldPassword = "topcoderopen"; c0b96abfe5 2013-03-15 kinaba: int K = 5; c0b96abfe5 2013-03-15 kinaba: int _ = 3; c0b96abfe5 2013-03-15 kinaba: END c0b96abfe5 2013-03-15 kinaba: CASE(1) c0b96abfe5 2013-03-15 kinaba: string oldPassword = "puyopuyo"; c0b96abfe5 2013-03-15 kinaba: int K = 4; c0b96abfe5 2013-03-15 kinaba: int _ = 0; c0b96abfe5 2013-03-15 kinaba: END c0b96abfe5 2013-03-15 kinaba: CASE(2) c0b96abfe5 2013-03-15 kinaba: string oldPassword = "loool"; c0b96abfe5 2013-03-15 kinaba: int K = 3; c0b96abfe5 2013-03-15 kinaba: int _ = 1; c0b96abfe5 2013-03-15 kinaba: END c0b96abfe5 2013-03-15 kinaba: CASE(3) c0b96abfe5 2013-03-15 kinaba: string oldPassword = "arena"; c0b96abfe5 2013-03-15 kinaba: int K = 5; c0b96abfe5 2013-03-15 kinaba: int _ = 0; c0b96abfe5 2013-03-15 kinaba: END c0b96abfe5 2013-03-15 kinaba: CASE(4) c0b96abfe5 2013-03-15 kinaba: string oldPassword = "amavckdkz"; c0b96abfe5 2013-03-15 kinaba: int K = 7; c0b96abfe5 2013-03-15 kinaba: int _ = 5; c0b96abfe5 2013-03-15 kinaba: END c0b96abfe5 2013-03-15 kinaba: /* c0b96abfe5 2013-03-15 kinaba: CASE(5) c0b96abfe5 2013-03-15 kinaba: string oldPassword = ; c0b96abfe5 2013-03-15 kinaba: int K = ; c0b96abfe5 2013-03-15 kinaba: int _ = ; c0b96abfe5 2013-03-15 kinaba: END c0b96abfe5 2013-03-15 kinaba: CASE(6) c0b96abfe5 2013-03-15 kinaba: string oldPassword = ; c0b96abfe5 2013-03-15 kinaba: int K = ; c0b96abfe5 2013-03-15 kinaba: int _ = ; c0b96abfe5 2013-03-15 kinaba: END c0b96abfe5 2013-03-15 kinaba: */ c0b96abfe5 2013-03-15 kinaba: } c0b96abfe5 2013-03-15 kinaba: // END CUT HERE