4fd800b3a8 2011-02-23 kinaba: #include <iostream> 4fd800b3a8 2011-02-23 kinaba: #include <sstream> 4fd800b3a8 2011-02-23 kinaba: #include <iomanip> 4fd800b3a8 2011-02-23 kinaba: #include <vector> 4fd800b3a8 2011-02-23 kinaba: #include <string> 4fd800b3a8 2011-02-23 kinaba: #include <map> 4fd800b3a8 2011-02-23 kinaba: #include <set> 4fd800b3a8 2011-02-23 kinaba: #include <algorithm> 4fd800b3a8 2011-02-23 kinaba: #include <numeric> 4fd800b3a8 2011-02-23 kinaba: #include <iterator> 4fd800b3a8 2011-02-23 kinaba: #include <functional> 4fd800b3a8 2011-02-23 kinaba: #include <complex> 4fd800b3a8 2011-02-23 kinaba: #include <queue> 4fd800b3a8 2011-02-23 kinaba: #include <stack> 4fd800b3a8 2011-02-23 kinaba: #include <cmath> 4fd800b3a8 2011-02-23 kinaba: #include <cassert> 4fd800b3a8 2011-02-23 kinaba: #include <cstring> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: typedef complex<double> CMP; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class BedroomFloor { public: 4fd800b3a8 2011-02-23 kinaba: LL b[6]; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: long long numberOfSticks(int x1, int y1, int x2, int y2) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: b[1] = b[2] = b[3] = b[4] = b[5] = 0; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: devideX_and_count(x1, x2, y1, y2); 4fd800b3a8 2011-02-23 kinaba: return optimize(); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // utils 4fd800b3a8 2011-02-23 kinaba: LL FLOOR(LL x) { return x/5*5; } 4fd800b3a8 2011-02-23 kinaba: LL CEIL (LL x) { return FLOOR(x+4); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // counting routines 4fd800b3a8 2011-02-23 kinaba: void devideX_and_count(LL x1, LL x2, LL y1, LL y2) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if( FLOOR(x1) == FLOOR(x2) ) 4fd800b3a8 2011-02-23 kinaba: devideY_and_count(x1, x2, y1, y2, 1); 4fd800b3a8 2011-02-23 kinaba: else 4fd800b3a8 2011-02-23 kinaba: devideY_and_count( x1, CEIL(x1), y1, y2, 1 ), 4fd800b3a8 2011-02-23 kinaba: devideY_and_count( CEIL(x1), CEIL(x1)+5, y1, y2, ((FLOOR(x2)-CEIL(x1))/5+1)/2 ), 4fd800b3a8 2011-02-23 kinaba: devideY_and_count( CEIL(x1)+5, CEIL(x1)+10, y1, y2, ((FLOOR(x2)-CEIL(x1))/5+0)/2 ), 4fd800b3a8 2011-02-23 kinaba: devideY_and_count( FLOOR(x2), x2, y1, y2, 1 ); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: void devideY_and_count(LL x1, LL x2, LL y1, LL y2, LL rep) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if( FLOOR(y1) == FLOOR(y2) ) 4fd800b3a8 2011-02-23 kinaba: count(x1, x2, y1, y2, rep); 4fd800b3a8 2011-02-23 kinaba: else 4fd800b3a8 2011-02-23 kinaba: count(x1, x2, y1, CEIL(y1), rep), 4fd800b3a8 2011-02-23 kinaba: count(x1, x2, CEIL(y1), CEIL(y1)+5, rep*(((FLOOR(y2)-CEIL(y1))/5+1)/2) ), 4fd800b3a8 2011-02-23 kinaba: count(x1, x2, CEIL(y1)+5, CEIL(y1)+10, rep*(((FLOOR(y2)-CEIL(y1))/5+0)/2) ), 4fd800b3a8 2011-02-23 kinaba: count(x1, x2, FLOOR(y2), y2 , rep); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: void count(LL x1, LL x2, LL y1, LL y2, LL rep) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if( x1/5%2 == y1/5%2 ) // is_horizontal 4fd800b3a8 2011-02-23 kinaba: b[x2-x1] += rep*(y2-y1); 4fd800b3a8 2011-02-23 kinaba: else 4fd800b3a8 2011-02-23 kinaba: b[y2-y1] += rep*(x2-x1); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // compute minimum number of 1x5 blocks required (by greedily removing larger blocks) 4fd800b3a8 2011-02-23 kinaba: LL optimize() 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: LL total = 0; 4fd800b3a8 2011-02-23 kinaba: for(int K=6*6*6*6*6; --K>0; ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int k[] = {-1, K%6, K/6%6, K/6/6%6, K/6/6/6%6, K/6/6/6/6%6}; 4fd800b3a8 2011-02-23 kinaba: if( 1*k[1] + 2*k[2] + 3*k[3] + 4*k[4] + 5*k[5] <= 5 ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: LL n = 1LL<<62; 4fd800b3a8 2011-02-23 kinaba: for(int i=1; i<=5; ++i) 4fd800b3a8 2011-02-23 kinaba: if( k[i] ) 4fd800b3a8 2011-02-23 kinaba: n = min(n, b[i]/k[i]); 4fd800b3a8 2011-02-23 kinaba: for(int i=1; i<=5; ++i) 4fd800b3a8 2011-02-23 kinaba: b[i] -= n*k[i]; 4fd800b3a8 2011-02-23 kinaba: total += n; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return total; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: #include <ctime> 4fd800b3a8 2011-02-23 kinaba: double start_time; string timer() 4fd800b3a8 2011-02-23 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 4fd800b3a8 2011-02-23 kinaba: { os << "{ "; 4fd800b3a8 2011-02-23 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 4fd800b3a8 2011-02-23 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 4fd800b3a8 2011-02-23 kinaba: void verify_case(const long long& Expected, const long long& Received) { 4fd800b3a8 2011-02-23 kinaba: bool ok = (Expected == Received); 4fd800b3a8 2011-02-23 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 4fd800b3a8 2011-02-23 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 4fd800b3a8 2011-02-23 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 4fd800b3a8 2011-02-23 kinaba: #define END verify_case(_, BedroomFloor().numberOfSticks(x1, y1, x2, y2));} 4fd800b3a8 2011-02-23 kinaba: int main(){ 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: CASE(0) 4fd800b3a8 2011-02-23 kinaba: int x1 = 0; 4fd800b3a8 2011-02-23 kinaba: int y1 = 0; 4fd800b3a8 2011-02-23 kinaba: int x2 = 5; 4fd800b3a8 2011-02-23 kinaba: int y2 = 5; 4fd800b3a8 2011-02-23 kinaba: long long _ = 5LL; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(1) 4fd800b3a8 2011-02-23 kinaba: int x1 = 0; 4fd800b3a8 2011-02-23 kinaba: int y1 = 0; 4fd800b3a8 2011-02-23 kinaba: int x2 = 10; 4fd800b3a8 2011-02-23 kinaba: int y2 = 2; 4fd800b3a8 2011-02-23 kinaba: long long _ = 5LL; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(2) 4fd800b3a8 2011-02-23 kinaba: int x1 = 2; 4fd800b3a8 2011-02-23 kinaba: int y1 = 2; 4fd800b3a8 2011-02-23 kinaba: int x2 = 8; 4fd800b3a8 2011-02-23 kinaba: int y2 = 8; 4fd800b3a8 2011-02-23 kinaba: long long _ = 12LL; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(3) 4fd800b3a8 2011-02-23 kinaba: int x1 = 8; 4fd800b3a8 2011-02-23 kinaba: int y1 = 5; 4fd800b3a8 2011-02-23 kinaba: int x2 = 20; 4fd800b3a8 2011-02-23 kinaba: int y2 = 16; 4fd800b3a8 2011-02-23 kinaba: long long _ = 27LL; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(4) 4fd800b3a8 2011-02-23 kinaba: int x1 = 0; 4fd800b3a8 2011-02-23 kinaba: int y1 = 0; 4fd800b3a8 2011-02-23 kinaba: int x2 = 1000000; 4fd800b3a8 2011-02-23 kinaba: int y2 = 1000000; 4fd800b3a8 2011-02-23 kinaba: long long _ = 200000000000LL; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(5) 4fd800b3a8 2011-02-23 kinaba: int x1 = 1; 4fd800b3a8 2011-02-23 kinaba: int y1 = 4; 4fd800b3a8 2011-02-23 kinaba: int x2 = 20; 4fd800b3a8 2011-02-23 kinaba: int y2 = 19; 4fd800b3a8 2011-02-23 kinaba: long long _ = 58LL; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(5) 4fd800b3a8 2011-02-23 kinaba: int x1 = 49; 4fd800b3a8 2011-02-23 kinaba: int y1 = 51; 4fd800b3a8 2011-02-23 kinaba: int x2 = 64; 4fd800b3a8 2011-02-23 kinaba: int y2 = 70; 4fd800b3a8 2011-02-23 kinaba: long long _ = 58LL; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE