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<int> CMP; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: double outer_prod(const CMP& a, const CMP& b) { return imag(conj(a)*b); } 4fd800b3a8 2011-02-23 kinaba: double inner_prod(const CMP& a, const CMP& b) { return real(conj(a)*b); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int ccw(const CMP& a, CMP b, CMP c) { 4fd800b3a8 2011-02-23 kinaba: b -= a; c -= a; 4fd800b3a8 2011-02-23 kinaba: if( outer_prod(b,c) > 0 ) return +1; // counter clockwise 4fd800b3a8 2011-02-23 kinaba: if( outer_prod(b,c) < 0 ) return -1; // clockwise 4fd800b3a8 2011-02-23 kinaba: if( inner_prod(b,c) < 0 ) return +2; // c--a--b on line 4fd800b3a8 2011-02-23 kinaba: if( norm(b) < norm(c) ) return -2; // a--b--c on line 4fd800b3a8 2011-02-23 kinaba: return 0; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: bool byX( const CMP& a, const CMP& b ) { 4fd800b3a8 2011-02-23 kinaba: if( a.real() != b.real() ) 4fd800b3a8 2011-02-23 kinaba: return a.real() < b.real(); 4fd800b3a8 2011-02-23 kinaba: return a.imag() < b.imag(); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector<CMP> convex_hull( vector<CMP> p ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: //#define IS_RIGHT <0 // skip on-line verts 4fd800b3a8 2011-02-23 kinaba: #define IS_RIGHT ==-1 // take all 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: sort(p.begin(), p.end(), &byX); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector<CMP> ans; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<p.size(); ans.push_back(p[i++])) // left-to-right 4fd800b3a8 2011-02-23 kinaba: while( ans.size()>=2 && ccw(ans[ans.size()-2], ans[ans.size()-1], p[i]) IS_RIGHT ) 4fd800b3a8 2011-02-23 kinaba: ans.pop_back(); 4fd800b3a8 2011-02-23 kinaba: if( ans.size() == p.size() ) 4fd800b3a8 2011-02-23 kinaba: return ans; 4fd800b3a8 2011-02-23 kinaba: for(int i=p.size()-2; i>=0; ans.push_back(p[i--])) // right-to-left 4fd800b3a8 2011-02-23 kinaba: while( ans.size()>=2 && ccw(ans[ans.size()-2], ans[ans.size()-1], p[i]) IS_RIGHT ) 4fd800b3a8 2011-02-23 kinaba: ans.pop_back(); 4fd800b3a8 2011-02-23 kinaba: ans.pop_back(); 4fd800b3a8 2011-02-23 kinaba: return ans; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int area(const CMP& a, const CMP& b, const CMP& c) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: return (int) abs( outer_prod(b-a,c-a) ); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class ExtendableTriangles { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: int getCount(vector <string> grid) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: vector<CMP> r, g, b; 4fd800b3a8 2011-02-23 kinaba: for(int y=0; y<grid.size(); ++y) 4fd800b3a8 2011-02-23 kinaba: for(int x=0; x<grid[0].size(); ++x) 4fd800b3a8 2011-02-23 kinaba: (grid[y][x]=='R' ? r : grid[y][x]=='G' ? g : b).push_back( CMP(y,x) ); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int cnt = r.size() * g.size() * b.size(); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: r = convex_hull(r); 4fd800b3a8 2011-02-23 kinaba: g = convex_hull(g); 4fd800b3a8 2011-02-23 kinaba: b = convex_hull(b); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: bool (*ext)[200][200] = (bool(*)[200][200])calloc(200*200*200, 1); 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<r.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: for(int j=0; j<g.size(); ++j) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int ma = -1; 4fd800b3a8 2011-02-23 kinaba: for(int k=0; k<b.size(); ++k) 4fd800b3a8 2011-02-23 kinaba: ma = max(ma, area(r[i], g[j], b[k])); 4fd800b3a8 2011-02-23 kinaba: for(int k=0; k<b.size(); ++k) 4fd800b3a8 2011-02-23 kinaba: if( area(r[i], g[j], b[k]) < ma ) 4fd800b3a8 2011-02-23 kinaba: ext[i][j][k] = true; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: for(int j=0; j<g.size(); ++j) 4fd800b3a8 2011-02-23 kinaba: for(int k=0; k<b.size(); ++k) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int ma = -1; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<r.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: ma = max(ma, area(r[i], g[j], b[k])); 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<r.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: if( area(r[i], g[j], b[k]) < ma ) 4fd800b3a8 2011-02-23 kinaba: ext[i][j][k] = true; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: for(int k=0; k<b.size(); ++k) 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<r.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int ma = -1; 4fd800b3a8 2011-02-23 kinaba: for(int j=0; j<g.size(); ++j) 4fd800b3a8 2011-02-23 kinaba: ma = max(ma, area(r[i], g[j], b[k])); 4fd800b3a8 2011-02-23 kinaba: for(int j=0; j<g.size(); ++j) 4fd800b3a8 2011-02-23 kinaba: if( area(r[i], g[j], b[k]) < ma ) 4fd800b3a8 2011-02-23 kinaba: ext[i][j][k] = true; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<r.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: for(int j=0; j<g.size(); ++j) 4fd800b3a8 2011-02-23 kinaba: for(int k=0; k<b.size(); ++k) 4fd800b3a8 2011-02-23 kinaba: if( !ext[i][j][k] ) 4fd800b3a8 2011-02-23 kinaba: --cnt; 4fd800b3a8 2011-02-23 kinaba: free(ext); 4fd800b3a8 2011-02-23 kinaba: return cnt; 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() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: int verify_case(const int &Expected, const int &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;} 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> struct Case_ { Case_(){start_time=clock();} }; 4fd800b3a8 2011-02-23 kinaba: char Test_(...); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<0>) { 4fd800b3a8 2011-02-23 kinaba: string grid_[] = {"RGB"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 0; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, ExtendableTriangles().getCount(grid)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<1>) { 4fd800b3a8 2011-02-23 kinaba: string grid_[] = {"RGB", 4fd800b3a8 2011-02-23 kinaba: "RGB"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 6; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, ExtendableTriangles().getCount(grid)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<2>) { 4fd800b3a8 2011-02-23 kinaba: string grid_[] = {"RRRRRRRR", 4fd800b3a8 2011-02-23 kinaba: "GGGGBBBB", 4fd800b3a8 2011-02-23 kinaba: "RRRRRRRR"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 240; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, ExtendableTriangles().getCount(grid)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<3>) { 4fd800b3a8 2011-02-23 kinaba: string grid_[] = {"RBRBRBRB", 4fd800b3a8 2011-02-23 kinaba: "BRBRBRBR", 4fd800b3a8 2011-02-23 kinaba: "RBRBRBRB", 4fd800b3a8 2011-02-23 kinaba: "BRBRBRBR", 4fd800b3a8 2011-02-23 kinaba: "RRRRRRRR", 4fd800b3a8 2011-02-23 kinaba: "BBBBBBBB", 4fd800b3a8 2011-02-23 kinaba: "RRRRBBBB", 4fd800b3a8 2011-02-23 kinaba: "BBBBRRRR"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 0; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, ExtendableTriangles().getCount(grid)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<4>) { 4fd800b3a8 2011-02-23 kinaba: string grid_[] = {"RGB", 4fd800b3a8 2011-02-23 kinaba: "RBG", 4fd800b3a8 2011-02-23 kinaba: "GRB", 4fd800b3a8 2011-02-23 kinaba: "GBR", 4fd800b3a8 2011-02-23 kinaba: "BRG", 4fd800b3a8 2011-02-23 kinaba: "BGR"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 208; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, ExtendableTriangles().getCount(grid)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<5>) { 4fd800b3a8 2011-02-23 kinaba: string grid_[] = {"BBRRBBGRBG"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 0; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, ExtendableTriangles().getCount(grid)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<6>) { 4fd800b3a8 2011-02-23 kinaba: string grid_[] = {"RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR", "RGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGR", "RGBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBGR", "RGBBBBBGRGBRBRGBBBBGBRBRBRGRGBBBBRRBGGRGRBGBBBBBGR", "RGBBBGRGRBRGBRGBRGBRGBGGBRBRBRGBRGBRGGBRBBBBBBBBGR", "RGBBBRGBRBRBRGBRGBRBRBRBRGBRGGGGRRBBRGBBBGRRGRBBGR", "RGBBRRGBRBRBRGGBRGRRGRGBRGBRBRGRGBRGRGRGRBRBGRGBGR", "RGBBBBBGRGBRBRGBBBBGBRBRBRGRGBBBBRRBGGRGRBGBBBBBGR", "RGBBBGRGRBRGBRGBRGBRGBGGBRBRBRGBRGBRGGBRBBBBBBBBGR", "RGBBBRGBRBRBRGBRGBRBRBRBRGBRGGGGRRBBRGBBBGRRGRBBGR", "RGBBRRGBRBRBRGGBRGRRGRGBRGBRBRGRGBRGRGRGRBRBGRGBGR", "RGBBBBBGRGBRBRGBBBBGBRBRBRGRGBBBBRRBGGRGRBGBBBBBGR", "RGBBBGRGRBRGBRGBRGBRGBGGBRBRB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8 2011-02-23 kinaba: return verify_case(RetVal, ExtendableTriangles().getCount(grid)); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); } 4fd800b3a8 2011-02-23 kinaba: template<> void Run_<-1>() {} 4fd800b3a8 2011-02-23 kinaba: int main() { Run_<0>(); } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE 4fd800b3a8 2011-02-23 kinaba: