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 <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: 4fd800b3a8 2011-02-23 kinaba: struct edge { 4fd800b3a8 2011-02-23 kinaba: int y, x1, x2; 4fd800b3a8 2011-02-23 kinaba: edge(){} 4fd800b3a8 2011-02-23 kinaba: edge(int y, int x1, int x2) :y(y),x1(min(x1,x2)),x2(max(x1,x2)) {} 4fd800b3a8 2011-02-23 kinaba: bool operator<( const edge& rhs ) const { 4fd800b3a8 2011-02-23 kinaba: if( y != rhs.y ) return y<rhs.y; 4fd800b3a8 2011-02-23 kinaba: if( x1 != rhs.x1 ) return x1<rhs.x1; 4fd800b3a8 2011-02-23 kinaba: return x2 < rhs.x2; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class RestoringPolygon { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: int restore(vector <int> x1, vector <int> x2, vector <int> y) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int N = x1.size(); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector<edge> e; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i!=N; ++i) 4fd800b3a8 2011-02-23 kinaba: e.push_back( edge(y[i], x1[i], x2[i]) ); 4fd800b3a8 2011-02-23 kinaba: sort(e.begin(), e.end()); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int ans = 0; 4fd800b3a8 2011-02-23 kinaba: for(int m=1; m<(1<<N); ++m) 4fd800b3a8 2011-02-23 kinaba: ans = max(ans, calc(e,m)); 4fd800b3a8 2011-02-23 kinaba: return ans; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int calc(vector<edge>& e_, int m) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: vector<edge> e; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; (1<<i)<=m; ++i) 4fd800b3a8 2011-02-23 kinaba: if( m & (1<<i) ) 4fd800b3a8 2011-02-23 kinaba: e.push_back( e_[i] ); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: if( e.size() <= 1 ) 4fd800b3a8 2011-02-23 kinaba: return 0; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: typedef pair<int,int> point; 4fd800b3a8 2011-02-23 kinaba: typedef map< point, vector<point> > graph; 4fd800b3a8 2011-02-23 kinaba: graph G; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<e.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int y = e[i].y; 4fd800b3a8 2011-02-23 kinaba: int xx[] = {e[i].x1, e[i].x2}; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: G[ point(e[i].x1,y) ].push_back( point(e[i].x2,y) ); 4fd800b3a8 2011-02-23 kinaba: G[ point(e[i].x2,y) ].push_back( point(e[i].x1,y) ); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: for(int k=0; k<2; ++k) { 4fd800b3a8 2011-02-23 kinaba: int x = xx[k]; 4fd800b3a8 2011-02-23 kinaba: for(int j=i-1; j>=0; --j) 4fd800b3a8 2011-02-23 kinaba: if( e[j].x1 == x ) { 4fd800b3a8 2011-02-23 kinaba: point p1(x, y); 4fd800b3a8 2011-02-23 kinaba: point p2(e[j].x1, e[j].y); 4fd800b3a8 2011-02-23 kinaba: if( G[p2].size() < 2 ) { 4fd800b3a8 2011-02-23 kinaba: G[p1].push_back(p2); 4fd800b3a8 2011-02-23 kinaba: G[p2].push_back(p1); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } else if( e[j].x2 == x ) { 4fd800b3a8 2011-02-23 kinaba: point p1(x, y); 4fd800b3a8 2011-02-23 kinaba: point p2(e[j].x2, e[j].y); 4fd800b3a8 2011-02-23 kinaba: if( G[p2].size() < 2 ) { 4fd800b3a8 2011-02-23 kinaba: G[p1].push_back(p2); 4fd800b3a8 2011-02-23 kinaba: G[p2].push_back(p1); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } else if( e[j].x1 < x && x < e[j].x2 ) { 4fd800b3a8 2011-02-23 kinaba: break; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: for(graph::iterator it=G.begin(); it!=G.end(); ++it) 4fd800b3a8 2011-02-23 kinaba: if( it->second.size() != 2 ) 4fd800b3a8 2011-02-23 kinaba: return 0; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: point s = G.begin()->first; 4fd800b3a8 2011-02-23 kinaba: point p = s, p2 = G[p][0]; 4fd800b3a8 2011-02-23 kinaba: for(int c=2 ;; ++c) { 4fd800b3a8 2011-02-23 kinaba: point q = (G[p2][0] == p ? G[p2][1] : G[p2][0]); 4fd800b3a8 2011-02-23 kinaba: p=p2, p2=q; 4fd800b3a8 2011-02-23 kinaba: if( q == s ) 4fd800b3a8 2011-02-23 kinaba: return c==e.size()*2 ? c : 0; 4fd800b3a8 2011-02-23 kinaba: } 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: int Test_(Case_<0>) { 4fd800b3a8 2011-02-23 kinaba: int x1_[] = {1,2,3,1}; 4fd800b3a8 2011-02-23 kinaba: vector <int> x1(x1_, x1_+sizeof(x1_)/sizeof(*x1_)); 4fd800b3a8 2011-02-23 kinaba: int x2_[] = {2,3,5,5}; 4fd800b3a8 2011-02-23 kinaba: vector <int> x2(x2_, x2_+sizeof(x2_)/sizeof(*x2_)); 4fd800b3a8 2011-02-23 kinaba: int y_[] = {1,4,2,0}; 4fd800b3a8 2011-02-23 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 8; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, RestoringPolygon().restore(x1, x2, y)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<1>) { 4fd800b3a8 2011-02-23 kinaba: int x1_[] = {1,1,2,2}; 4fd800b3a8 2011-02-23 kinaba: vector <int> x1(x1_, x1_+sizeof(x1_)/sizeof(*x1_)); 4fd800b3a8 2011-02-23 kinaba: int x2_[] = {3,3,4,4}; 4fd800b3a8 2011-02-23 kinaba: vector <int> x2(x2_, x2_+sizeof(x2_)/sizeof(*x2_)); 4fd800b3a8 2011-02-23 kinaba: int y_[] = {0,2,1,3}; 4fd800b3a8 2011-02-23 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 4; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, RestoringPolygon().restore(x1, x2, y)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<2>) { 4fd800b3a8 2011-02-23 kinaba: int x1_[] = {1}; 4fd800b3a8 2011-02-23 kinaba: vector <int> x1(x1_, x1_+sizeof(x1_)/sizeof(*x1_)); 4fd800b3a8 2011-02-23 kinaba: int x2_[] = {2}; 4fd800b3a8 2011-02-23 kinaba: vector <int> x2(x2_, x2_+sizeof(x2_)/sizeof(*x2_)); 4fd800b3a8 2011-02-23 kinaba: int y_[] = {1}; 4fd800b3a8 2011-02-23 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 0; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, RestoringPolygon().restore(x1, x2, y)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<3>) { 4fd800b3a8 2011-02-23 kinaba: int x1_[] = {0,0,0}; 4fd800b3a8 2011-02-23 kinaba: vector <int> x1(x1_, x1_+sizeof(x1_)/sizeof(*x1_)); 4fd800b3a8 2011-02-23 kinaba: int x2_[] = {1000,1000,1000}; 4fd800b3a8 2011-02-23 kinaba: vector <int> x2(x2_, x2_+sizeof(x2_)/sizeof(*x2_)); 4fd800b3a8 2011-02-23 kinaba: int y_[] = {0,1,2}; 4fd800b3a8 2011-02-23 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 4; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, RestoringPolygon().restore(x1, x2, y)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<4>) { 4fd800b3a8 2011-02-23 kinaba: int x1_[] = {0,1,1,2}; 4fd800b3a8 2011-02-23 kinaba: vector <int> x1(x1_, x1_+sizeof(x1_)/sizeof(*x1_)); 4fd800b3a8 2011-02-23 kinaba: int x2_[] = {1,0,2,1}; 4fd800b3a8 2011-02-23 kinaba: vector <int> x2(x2_, x2_+sizeof(x2_)/sizeof(*x2_)); 4fd800b3a8 2011-02-23 kinaba: int y_[] = {0,4,2,3}; 4fd800b3a8 2011-02-23 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 8; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, RestoringPolygon().restore(x1, x2, y)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<5>) { 4fd800b3a8 2011-02-23 kinaba: int x1_[] = {696, -193, -193, 367}; 4fd800b3a8 2011-02-23 kinaba: vector <int> x1(x1_, x1_+sizeof(x1_)/sizeof(*x1_)); 4fd800b3a8 2011-02-23 kinaba: int x2_[] = {367, -276, -276, 696}; 4fd800b3a8 2011-02-23 kinaba: vector <int> x2(x2_, x2_+sizeof(x2_)/sizeof(*x2_)); 4fd800b3a8 2011-02-23 kinaba: int y_[] = {-14, 168, -14, 168}; 4fd800b3a8 2011-02-23 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 4; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, RestoringPolygon().restore(x1, x2, y)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<6>) { 4fd800b3a8 2011-02-23 kinaba: int x1_[] = { 0, 1, 6, 5, 5, 1, 6, 1, 0, 0, 9}; 4fd800b3a8 2011-02-23 kinaba: vector <int> x1(x1_, x1_+sizeof(x1_)/sizeof(*x1_)); 4fd800b3a8 2011-02-23 kinaba: int x2_[] = { 5, 9, 9, 6, 6, 0, 9, 0, 1, 5, 6}; 4fd800b3a8 2011-02-23 kinaba: vector <int> x2(x2_, x2_+sizeof(x2_)/sizeof(*x2_)); 4fd800b3a8 2011-02-23 kinaba: int y_[] = { 1, 4, 5, 3, 2, 0, 0, 2, 3, 5, 1}; 4fd800b3a8 2011-02-23 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 16; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, RestoringPolygon().restore(x1, x2, y)); } 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: