ADDED SRM/634-U/1A.cpp Index: SRM/634-U/1A.cpp ================================================================== --- SRM/634-U/1A.cpp +++ SRM/634-U/1A.cpp @@ -0,0 +1,124 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class ShoppingSurveyDiv1 { public: + int minValue(int N, int K, vector s) + { + for(int big=0; big<=N; ++big) + if(can(N, K, s, big)) + return big; + + assert(false); + return -1; + } + + bool can(int N, int K, vector sb, int BIG) + { + sort(sb.rbegin(), sb.rend()); + assert(sb[K-1] >= BIG); + + multiset> s; + for(auto p: sb) + if(p-BIG > 0) + s.insert(p-BIG); + int total = accumulate(s.begin(), s.end(), 0); + if(total > (K-1)*(N-BIG)) + return false; + if(!s.empty() && *s.begin() > (N-BIG)) + return false; + return true; + } +}; + +// BEGIN CUT HERE +#include +double start_time; string timer() + { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } +template ostream& operator<<(ostream& os, const vector& v) + { os << "{ "; + for(typename vector::const_iterator it=v.begin(); it!=v.end(); ++it) + os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } +void verify_case(const int& Expected, const int& Received) { + bool ok = (Expected == Received); + if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; + cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } +#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); +#define END verify_case(_, ShoppingSurveyDiv1().minValue(N, K, s));} +int main(){ + +CASE(0) + int N = 10; + int K = 2; + int s_[] = {1, 2, 3}; + vector s(s_, s_+sizeof(s_)/sizeof(*s_)); + int _ = 0; +END +CASE(1) + int N = 5; + int K = 2; + int s_[] = {1, 2, 3}; + vector s(s_, s_+sizeof(s_)/sizeof(*s_)); + int _ = 1; +END +CASE(2) + int N = 4; + int K = 4; + int s_[] = {4, 4, 4, 2}; + vector s(s_, s_+sizeof(s_)/sizeof(*s_)); + int _ = 2; +END +CASE(3) + int N = 20; + int K = 3; + int s_[] = {1, 10, 3, 4, 8, 15, 3, 16, 18, 2, 7, 3}; + vector s(s_, s_+sizeof(s_)/sizeof(*s_)); + int _ = 10; +END +CASE(4) + int N = 4; + int K = 2; + int s_[] = {1, 2, 1, 1, 3, 1, 2, 2, 1, 2, 1}; + vector s(s_, s_+sizeof(s_)/sizeof(*s_)); + int _ = 2; +END +CASE(5) + int N = 2; + int K = 3; + int s_[] = {1, 1, 1, 2}; + vector s(s_, s_+sizeof(s_)/sizeof(*s_)); + int _ = 1; +END +CASE(6) + int N = 500; + int K = 1; + int s_[] = {500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500}; + vector s(s_, s_+sizeof(s_)/sizeof(*s_)); + int _ = -1; +END +CASE(6) + int N = 500; + int K = 500; + int s_[] = {500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500}; + vector s(s_, s_+sizeof(s_)/sizeof(*s_)); + int _ = -1; +END +} +// END CUT HERE ADDED SRM/634-U/1B.cpp Index: SRM/634-U/1B.cpp ================================================================== --- SRM/634-U/1B.cpp +++ SRM/634-U/1B.cpp @@ -0,0 +1,311 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +template +class IdGen +{ + map v2id_; + vector id2v_; +public: + int v2id(const T& v) { + if( !v2id_.count(v) ) { v2id_[v] = size(); id2v_.push_back(v); } + return v2id_[v]; + } + const T& id2v(int i) const { return id2v_[i]; } + int size() const { return id2v_.size(); } +}; + +template +class MaxFlow +{ + IdGen idgen; + vector G[NV]; + Flow F[NV][NV]; + +public: + void addEdge( Vert s_, Vert t_, Flow f ) + { + const int s = idgen.v2id(s_), t = idgen.v2id(t_); + G[s].push_back(t); + G[t].push_back(s); + F[s][t] = f; + F[t][s] = 0; + } + + Flow calc( Vert s_, Vert t_ ) + { + const int S = idgen.v2id(s_), D = idgen.v2id(t_); + for( Flow total=0 ;; ) { + // Do BFS and compute the level for each node. + int LV[NV] = {0}; + vector Q(1, S); + for(int lv=1; !Q.empty(); ++lv) { + vector Q2; + for(size_t i=0; i!=Q.size(); ++i) { + const vector& ne = G[Q[i]]; + for(size_t j=0; j!=ne.size(); ++j) + if( F[Q[i]][ne[j]] && !LV[ne[j]] && ne[j]!=S ) + LV[ne[j]]=lv, Q2.push_back(ne[j]); + } + Q.swap(Q2); + } + + // Destination is now unreachable. Done. + if( !LV[D] ) + return total; + + // Iterating DFS. + bool blocked[NV] = {}; + total += dinic_dfs( S, D, LV, 0x7fffffff, blocked ); + } + } + +private: + Flow dinic_dfs( int v, int D, int LV[], Flow flow_in, bool blocked[] ) + { + Flow flow_out = 0; + for(size_t i=0; i!=G[v].size(); ++i) { + int u = G[v][i]; + if( LV[v]+1==LV[u] && F[v][u] ) { + Flow f = min(flow_in-flow_out, F[v][u]); + if( u==D || !blocked[u] && (f=dinic_dfs(u,D,LV,f,blocked))>0 ) { + F[v][u] -= f; + F[u][v] += f; + flow_out += f; + if( flow_in == flow_out ) return flow_out; + } + } + } + blocked[v] = (flow_out==0); + return flow_out; + } +}; + + +typedef complex CMP; +double outer_prod(const CMP& a, const CMP& b) { return imag(conj(a)*b); } +double inner_prod(const CMP& a, const CMP& b) { return real(conj(a)*b); } +int ccw(const CMP& a, CMP b, CMP c) { + b -= a; c -= a; + if( outer_prod(b,c) > 0 ) return +1; // counter clockwise + if( outer_prod(b,c) < 0 ) return -1; // clockwise + if( inner_prod(b,c) < 0 ) return +2; // c--[a--b] on line + if( norm(b) < norm(c) ) return -2; // [a--b]--c on line + return 0; // [a--c--b] on line +} + +bool cross(int x1, int y1, int x2, int y2, + int X1, int Y1, int X2, int Y2) { + CMP p1(x1,y1); + CMP p2(x2,y2); + CMP P1(X1,Y1); + CMP P2(X2,Y2); + return ccw(p1,p2,P1)*ccw(p1,p2,P2)<=0 && ccw(P1,P2,p1)*ccw(P1,P2,p2)<=0; +} + +class SegmentDrawing { public: + enum TAG {SRC, LEFT_R, RIGHT_R, LEFT_B, RIGHT_B, DST}; + static const int INF = 0x3fffffff; + + int maxScore(vector x, vector y, vector redScore, vector blueScore) + { + const int N = x.size(); + + vector> cross_table(N*N, vector(N*N)); + for(int i=0; i, LL>; + + int all = 0; + for(int i=0; iaddEdge(make_pair(SRC,0), make_pair(LEFT_R,p), redScore[p]); + mf->addEdge(make_pair(LEFT_R,p), make_pair(RIGHT_R,p), INF); + mf->addEdge(make_pair(RIGHT_R,p), make_pair(DST,0), 0); + mf->addEdge(make_pair(SRC,0), make_pair(LEFT_B,p), 0); + mf->addEdge(make_pair(LEFT_B,p), make_pair(RIGHT_B,p), INF); + mf->addEdge(make_pair(RIGHT_B,p), make_pair(DST,0), blueScore[p]); + all += redScore[p]; + all += blueScore[p]; + } + + for(int i=0; iaddEdge(make_pair(RIGHT_R,rp), make_pair(LEFT_B,bp), INF); + } + + LL flow = mf->calc(make_pair(SRC,0), make_pair(DST,0)); + delete mf; + return all - flow; + } +}; + +// BEGIN CUT HERE +#include +double start_time; string timer() + { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } +template ostream& operator<<(ostream& os, const vector& v) + { os << "{ "; + for(typename vector::const_iterator it=v.begin(); it!=v.end(); ++it) + os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } +void verify_case(const int& Expected, const int& Received) { + bool ok = (Expected == Received); + if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; + cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } +#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); +#define END verify_case(_, SegmentDrawing().maxScore(x, y, redScore, blueScore));} +int main(){ + +CASE(0) + int x_[] = {0,1,0,-1}; + vector x(x_, x_+sizeof(x_)/sizeof(*x_)); + int y_[] = {1,0,-1,0}; + vector y(y_, y_+sizeof(y_)/sizeof(*y_)); + int redScore_[] = {0, 1, 2, 3, + 1, 0, 6, 4, + 2, 6, 0, 5, + 3, 4, 5, 0}; + vector redScore(redScore_, redScore_+sizeof(redScore_)/sizeof(*redScore_)); + int blueScore_[] = {0, 2, 3, 7, + 2, 0, 4, 6, + 3, 4, 0, 5, + 7, 6, 5, 0}; + vector blueScore(blueScore_, blueScore_+sizeof(blueScore_)/sizeof(*blueScore_)); + int _ = 27; +END +CASE(1) + int x_[] = {0, 1}; + vector x(x_, x_+sizeof(x_)/sizeof(*x_)); + int y_[] = {1, 0}; + vector y(y_, y_+sizeof(y_)/sizeof(*y_)); + int redScore_[] = {0, 101, 101, 0}; + vector redScore(redScore_, redScore_+sizeof(redScore_)/sizeof(*redScore_)); + int blueScore_[] = {0, 100, 100, 0}; + vector blueScore(blueScore_, blueScore_+sizeof(blueScore_)/sizeof(*blueScore_)); + int _ = 101; +END +CASE(2) + int x_[] = {-3, -1, -1, 1, 1, 3}; + vector x(x_, x_+sizeof(x_)/sizeof(*x_)); + int y_[] = { 0, -2, 2, -2, 2, 0}; + vector y(y_, y_+sizeof(y_)/sizeof(*y_)); + int redScore_[] = {0, 2, 1, 2, 1, 2, + 2, 0, 2, 1, 2, 1, + 1, 2, 0, 2, 1, 2, + 2, 1, 2, 0, 2, 1, + 1, 2, 1, 2, 0, 2, + 2, 1, 2, 1, 2, 0}; + vector redScore(redScore_, redScore_+sizeof(redScore_)/sizeof(*redScore_)); + int blueScore_[] = {0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 21, 0, 0, + 0, 0, 21, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0}; + vector blueScore(blueScore_, blueScore_+sizeof(blueScore_)/sizeof(*blueScore_)); + int _ = 25; +END +CASE(3) + int x_[] = {-100, 100, 0, -10, 10, 0}; + vector x(x_, x_+sizeof(x_)/sizeof(*x_)); + int y_[] = {0, 0, 100, 10, 10, 1}; + vector y(y_, y_+sizeof(y_)/sizeof(*y_)); + int redScore_[] = { 0, 96, 96, 25, 25, 25, + 96, 0, 96, 25, 25, 25, + 96, 96, 0, 25, 25, 25, + 25, 25, 25, 0, 10, 10, + 25, 25, 25, 10, 0, 10, + 25, 25, 25, 10, 10, 0}; + vector redScore(redScore_, redScore_+sizeof(redScore_)/sizeof(*redScore_)); + int blueScore_[] = { 0, 30, 30, 20, 20, 20, + 30, 0, 30, 20, 20, 20, + 30, 30, 0, 20, 20, 20, + 20, 20, 20, 0, 86, 86, + 20, 20, 20, 86, 0, 86, + 20, 20, 20, 86, 86, 0}; + vector blueScore(blueScore_, blueScore_+sizeof(blueScore_)/sizeof(*blueScore_)); + int _ = 546; +END +CASE(4) + int x_[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + vector x(x_, x_+sizeof(x_)/sizeof(*x_)); + int y_[] = {1, 4, 9, 16, 25, 36, 49, 64, 81, 100}; + vector y(y_, y_+sizeof(y_)/sizeof(*y_)); + int redScore_[] = {0, 15, 2, 3, 4, 5, 6, 7, 8, 9, + 15, 0, 15, 2, 3, 4, 5, 6, 7, 8, + 2, 15, 0, 15, 2, 3, 4, 5, 6, 7, + 3, 2, 15, 0, 15, 2, 3, 4, 5, 6, + 4, 3, 2, 15, 0, 15, 2, 3, 4, 5, + 5, 4, 3, 2, 15, 0, 15, 2, 3, 4, + 6, 5, 4, 3, 2, 15, 0, 15, 2, 3, + 7, 6, 5, 4, 3, 2, 15, 0, 15, 2, + 8, 7, 6, 5, 4, 3, 2, 15, 0, 15, + 9, 8, 7, 6, 5, 4, 3, 2, 15, 0} +; + vector redScore(redScore_, redScore_+sizeof(redScore_)/sizeof(*redScore_)); + int blueScore_[] = {0, 0, 2, 3, 4, 5, 6, 7, 8, 9, + 0, 0, 0, 2, 3, 4, 5, 6, 7, 8, + 2, 0, 0, 0, 2, 3, 4, 5, 6, 7, + 3, 2, 0, 0, 0, 2, 3, 4, 5, 6, + 4, 3, 2, 0, 0, 100, 2, 3, 4, 5, + 5, 4, 3, 2, 100, 0, 0, 2, 3, 4, + 6, 5, 4, 3, 2, 0, 0, 0, 2, 3, + 7, 6, 5, 4, 3, 2, 0, 0, 0, 2, + 8, 7, 6, 5, 4, 3, 2, 0, 0, 0, + 9, 8, 7, 6, 5, 4, 3, 2, 0, 0}; + vector blueScore(blueScore_, blueScore_+sizeof(blueScore_)/sizeof(*blueScore_)); + int _ = 300; +END +/* +CASE(5) + int x_[] = ; + vector x(x_, x_+sizeof(x_)/sizeof(*x_)); + int y_[] = ; + vector y(y_, y_+sizeof(y_)/sizeof(*y_)); + int redScore_[] = ; + vector redScore(redScore_, redScore_+sizeof(redScore_)/sizeof(*redScore_)); + int blueScore_[] = ; + vector blueScore(blueScore_, blueScore_+sizeof(blueScore_)/sizeof(*blueScore_)); + int _ = ; +END +CASE(6) + int x_[] = ; + vector x(x_, x_+sizeof(x_)/sizeof(*x_)); + int y_[] = ; + vector y(y_, y_+sizeof(y_)/sizeof(*y_)); + int redScore_[] = ; + vector redScore(redScore_, redScore_+sizeof(redScore_)/sizeof(*redScore_)); + int blueScore_[] = ; + vector blueScore(blueScore_, blueScore_+sizeof(blueScore_)/sizeof(*blueScore_)); + int _ = ; +END +*/ +} +// END CUT HERE Index: lib/geo/ccw.cpp ================================================================== --- lib/geo/ccw.cpp +++ lib/geo/ccw.cpp @@ -14,5 +14,11 @@ if( outer_prod(b,c) < 0 ) return -1; // clockwise if( inner_prod(b,c) < 0 ) return +2; // c--[a--b] on line if( norm(b) < norm(c) ) return -2; // [a--b]--c on line return 0; // [a--c--b] on line } + + +// intersection of two line segments. +bool cross(CMP p1, CMP p2, CMP P1, CMP P2) { + return ccw(p1,p2,P1)*ccw(p1,p2,P2)<=0 && ccw(P1,P2,p1)*ccw(P1,P2,p2)<=0; +}