4c0c9a228b 2013-07-01 kinaba: #include <iostream> 4c0c9a228b 2013-07-01 kinaba: #include <sstream> 4c0c9a228b 2013-07-01 kinaba: #include <iomanip> 4c0c9a228b 2013-07-01 kinaba: #include <vector> 4c0c9a228b 2013-07-01 kinaba: #include <string> 4c0c9a228b 2013-07-01 kinaba: #include <map> 4c0c9a228b 2013-07-01 kinaba: #include <set> 4c0c9a228b 2013-07-01 kinaba: #include <algorithm> 4c0c9a228b 2013-07-01 kinaba: #include <numeric> 4c0c9a228b 2013-07-01 kinaba: #include <iterator> 4c0c9a228b 2013-07-01 kinaba: #include <functional> 4c0c9a228b 2013-07-01 kinaba: #include <complex> 4c0c9a228b 2013-07-01 kinaba: #include <queue> 4c0c9a228b 2013-07-01 kinaba: #include <stack> 4c0c9a228b 2013-07-01 kinaba: #include <cmath> 4c0c9a228b 2013-07-01 kinaba: #include <cassert> 4c0c9a228b 2013-07-01 kinaba: using namespace std; 4c0c9a228b 2013-07-01 kinaba: typedef long long LL; 4c0c9a228b 2013-07-01 kinaba: typedef long double LD; 4c0c9a228b 2013-07-01 kinaba: typedef complex<double> CMP; 4c0c9a228b 2013-07-01 kinaba: 4c0c9a228b 2013-07-01 kinaba: static const int INF = 999, UNK = -1; 4c0c9a228b 2013-07-01 kinaba: 4c0c9a228b 2013-07-01 kinaba: int bitcnt(int x) 4c0c9a228b 2013-07-01 kinaba: { 4c0c9a228b 2013-07-01 kinaba: int c = 0; 4c0c9a228b 2013-07-01 kinaba: for(; x; x>>=1) 4c0c9a228b 2013-07-01 kinaba: c += x&1; 4c0c9a228b 2013-07-01 kinaba: return c; 4c0c9a228b 2013-07-01 kinaba: } 4c0c9a228b 2013-07-01 kinaba: 4c0c9a228b 2013-07-01 kinaba: class AntlerSwapping { public: 4c0c9a228b 2013-07-01 kinaba: int getmin(vector <int> a, vector <int> b, int capacity) 4c0c9a228b 2013-07-01 kinaba: { 4c0c9a228b 2013-07-01 kinaba: int N = a.size(); 4c0c9a228b 2013-07-01 kinaba: vector<bool> tbl(1<<N); 4c0c9a228b 2013-07-01 kinaba: for(int mask=1; mask<(1<<N); ++mask) 4c0c9a228b 2013-07-01 kinaba: { 4c0c9a228b 2013-07-01 kinaba: vector<int> c; 4c0c9a228b 2013-07-01 kinaba: for(int i=0; (1<<i)<=mask; ++i) 4c0c9a228b 2013-07-01 kinaba: if((1<<i) & mask) { 4c0c9a228b 2013-07-01 kinaba: c.push_back(a[i]); 4c0c9a228b 2013-07-01 kinaba: c.push_back(b[i]); 4c0c9a228b 2013-07-01 kinaba: } 4c0c9a228b 2013-07-01 kinaba: sort(c.begin(), c.end()); 4c0c9a228b 2013-07-01 kinaba: bool can = true; 4c0c9a228b 2013-07-01 kinaba: for(int i=0; i+1<c.size(); i+=2) 4c0c9a228b 2013-07-01 kinaba: if(abs(c[i]-c[i+1]) > capacity) 4c0c9a228b 2013-07-01 kinaba: can = false; 4c0c9a228b 2013-07-01 kinaba: tbl[mask] = can; 4c0c9a228b 2013-07-01 kinaba: } 4c0c9a228b 2013-07-01 kinaba: vector<int> memo(1<<N, UNK); 4c0c9a228b 2013-07-01 kinaba: int best = rec((1<<N)-1, tbl, memo); 4c0c9a228b 2013-07-01 kinaba: return best>=INF ? -1 : best; 4c0c9a228b 2013-07-01 kinaba: } 4c0c9a228b 2013-07-01 kinaba: 4c0c9a228b 2013-07-01 kinaba: int rec(int mask, const vector<bool>& tbl, vector<int>& memo) 4c0c9a228b 2013-07-01 kinaba: { 4c0c9a228b 2013-07-01 kinaba: if(memo[mask] != UNK) 4c0c9a228b 2013-07-01 kinaba: return memo[mask]; 4c0c9a228b 2013-07-01 kinaba: if(mask == 0) 4c0c9a228b 2013-07-01 kinaba: return 0; 4c0c9a228b 2013-07-01 kinaba: int best = INF; 4c0c9a228b 2013-07-01 kinaba: for(int m2=mask; m2; m2=(m2-1)&mask) 4c0c9a228b 2013-07-01 kinaba: { 4c0c9a228b 2013-07-01 kinaba: int bc = bitcnt(m2); 4c0c9a228b 2013-07-01 kinaba: if(tbl[m2]) 4c0c9a228b 2013-07-01 kinaba: best = min(best, (bc-1) + rec(mask&~m2, tbl, memo)); 4c0c9a228b 2013-07-01 kinaba: } 4c0c9a228b 2013-07-01 kinaba: return memo[mask] = best; 4c0c9a228b 2013-07-01 kinaba: } 4c0c9a228b 2013-07-01 kinaba: }; 4c0c9a228b 2013-07-01 kinaba: 4c0c9a228b 2013-07-01 kinaba: // BEGIN CUT HERE 4c0c9a228b 2013-07-01 kinaba: #include <ctime> 4c0c9a228b 2013-07-01 kinaba: double start_time; string timer() 4c0c9a228b 2013-07-01 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4c0c9a228b 2013-07-01 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 4c0c9a228b 2013-07-01 kinaba: { os << "{ "; 4c0c9a228b 2013-07-01 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 4c0c9a228b 2013-07-01 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 4c0c9a228b 2013-07-01 kinaba: void verify_case(const int& Expected, const int& Received) { 4c0c9a228b 2013-07-01 kinaba: bool ok = (Expected == Received); 4c0c9a228b 2013-07-01 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 4c0c9a228b 2013-07-01 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 4c0c9a228b 2013-07-01 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 4c0c9a228b 2013-07-01 kinaba: #define END verify_case(_, AntlerSwapping().getmin(antler1, antler2, capacity));} 4c0c9a228b 2013-07-01 kinaba: int main(){ 4c0c9a228b 2013-07-01 kinaba: 4c0c9a228b 2013-07-01 kinaba: CASE(0) 4c0c9a228b 2013-07-01 kinaba: int antler1_[] = {3, 2, 2}; 4c0c9a228b 2013-07-01 kinaba: vector <int> antler1(antler1_, antler1_+sizeof(antler1_)/sizeof(*antler1_)); 4c0c9a228b 2013-07-01 kinaba: int antler2_[] = {3, 5, 5}; 4c0c9a228b 2013-07-01 kinaba: vector <int> antler2(antler2_, antler2_+sizeof(antler2_)/sizeof(*antler2_)); 4c0c9a228b 2013-07-01 kinaba: int capacity = 0; 4c0c9a228b 2013-07-01 kinaba: int _ = 1; 4c0c9a228b 2013-07-01 kinaba: END 4c0c9a228b 2013-07-01 kinaba: CASE(1) 4c0c9a228b 2013-07-01 kinaba: int antler1_[] = {4, 2, 6, 4, 8, 5, 2, 3}; 4c0c9a228b 2013-07-01 kinaba: vector <int> antler1(antler1_, antler1_+sizeof(antler1_)/sizeof(*antler1_)); 4c0c9a228b 2013-07-01 kinaba: int antler2_[] = {3, 4, 5, 2, 8, 5, 7, 6}; 4c0c9a228b 2013-07-01 kinaba: vector <int> antler2(antler2_, antler2_+sizeof(antler2_)/sizeof(*antler2_)); 4c0c9a228b 2013-07-01 kinaba: int capacity = 1; 4c0c9a228b 2013-07-01 kinaba: int _ = 2; 4c0c9a228b 2013-07-01 kinaba: END 4c0c9a228b 2013-07-01 kinaba: CASE(2) 4c0c9a228b 2013-07-01 kinaba: int antler1_[] = {12, 34, 56, 78}; 4c0c9a228b 2013-07-01 kinaba: vector <int> antler1(antler1_, antler1_+sizeof(antler1_)/sizeof(*antler1_)); 4c0c9a228b 2013-07-01 kinaba: int antler2_[] = {1234, 2345, 3456, 4567}; 4c0c9a228b 2013-07-01 kinaba: vector <int> antler2(antler2_, antler2_+sizeof(antler2_)/sizeof(*antler2_)); 4c0c9a228b 2013-07-01 kinaba: int capacity = 100; 4c0c9a228b 2013-07-01 kinaba: int _ = -1; 4c0c9a228b 2013-07-01 kinaba: END 4c0c9a228b 2013-07-01 kinaba: CASE(3) 4c0c9a228b 2013-07-01 kinaba: int antler1_[] = {47, 58, 2013}; 4c0c9a228b 2013-07-01 kinaba: vector <int> antler1(antler1_, antler1_+sizeof(antler1_)/sizeof(*antler1_)); 4c0c9a228b 2013-07-01 kinaba: int antler2_[] = {49, 55, 2013}; 4c0c9a228b 2013-07-01 kinaba: vector <int> antler2(antler2_, antler2_+sizeof(antler2_)/sizeof(*antler2_)); 4c0c9a228b 2013-07-01 kinaba: int capacity = 3; 4c0c9a228b 2013-07-01 kinaba: int _ = 0; 4c0c9a228b 2013-07-01 kinaba: END 4c0c9a228b 2013-07-01 kinaba: CASE(4) 4c0c9a228b 2013-07-01 kinaba: int antler1_[] = {4, 1, 7, 5, 7, 8, 2, 1, 3, 1, 7, 5, 9, 4, 9, 1}; 4c0c9a228b 2013-07-01 kinaba: vector <int> antler1(antler1_, antler1_+sizeof(antler1_)/sizeof(*antler1_)); 4c0c9a228b 2013-07-01 kinaba: int antler2_[] = {10, 6, 5, 3, 1, 8, 4, 4, 4, 7, 1, 4, 6, 5, 10, 10}; 4c0c9a228b 2013-07-01 kinaba: vector <int> antler2(antler2_, antler2_+sizeof(antler2_)/sizeof(*antler2_)); 4c0c9a228b 2013-07-01 kinaba: int capacity = 1; 4c0c9a228b 2013-07-01 kinaba: int _ = 7; 4c0c9a228b 2013-07-01 kinaba: END 4c0c9a228b 2013-07-01 kinaba: /* 4c0c9a228b 2013-07-01 kinaba: CASE(5) 4c0c9a228b 2013-07-01 kinaba: int antler1_[] = ; 4c0c9a228b 2013-07-01 kinaba: vector <int> antler1(antler1_, antler1_+sizeof(antler1_)/sizeof(*antler1_)); 4c0c9a228b 2013-07-01 kinaba: int antler2_[] = ; 4c0c9a228b 2013-07-01 kinaba: vector <int> antler2(antler2_, antler2_+sizeof(antler2_)/sizeof(*antler2_)); 4c0c9a228b 2013-07-01 kinaba: int capacity = ; 4c0c9a228b 2013-07-01 kinaba: int _ = ; 4c0c9a228b 2013-07-01 kinaba: END 4c0c9a228b 2013-07-01 kinaba: CASE(6) 4c0c9a228b 2013-07-01 kinaba: int antler1_[] = ; 4c0c9a228b 2013-07-01 kinaba: vector <int> antler1(antler1_, antler1_+sizeof(antler1_)/sizeof(*antler1_)); 4c0c9a228b 2013-07-01 kinaba: int antler2_[] = ; 4c0c9a228b 2013-07-01 kinaba: vector <int> antler2(antler2_, antler2_+sizeof(antler2_)/sizeof(*antler2_)); 4c0c9a228b 2013-07-01 kinaba: int capacity = ; 4c0c9a228b 2013-07-01 kinaba: int _ = ; 4c0c9a228b 2013-07-01 kinaba: END 4c0c9a228b 2013-07-01 kinaba: */ 4c0c9a228b 2013-07-01 kinaba: } 4c0c9a228b 2013-07-01 kinaba: // END CUT HERE