ADDED SRM/584-U/1A.cpp Index: SRM/584-U/1A.cpp ================================================================== --- SRM/584-U/1A.cpp +++ SRM/584-U/1A.cpp @@ -0,0 +1,154 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef long double LD; +typedef complex CMP; + +class Egalitarianism { public: + int maxDifference(vector isFriend, int d) + { + int N = isFriend.size(); + int INF = 0x3fffffff; + vector< vector > g(N, vector(N, INF)); + for(int x=0; x +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(_, Egalitarianism().maxDifference(isFriend, d));} +int main(){ + +CASE(0) + string isFriend_[] = {"NYN", + "YNY", + "NYN"}; + vector isFriend(isFriend_, isFriend_+sizeof(isFriend_)/sizeof(*isFriend_)); + int d = 10; + int _ = 20; +END +CASE(1) + string isFriend_[] = {"NN", + "NN"}; + vector isFriend(isFriend_, isFriend_+sizeof(isFriend_)/sizeof(*isFriend_)); + int d = 1; + int _ = -1; +END +CASE(2) + string isFriend_[] = {"NNYNNN", + "NNYNNN", + "YYNYNN", + "NNYNYY", + "NNNYNN", + "NNNYNN"}; + vector isFriend(isFriend_, isFriend_+sizeof(isFriend_)/sizeof(*isFriend_)); + int d = 1000; + int _ = 3000; +END +CASE(3) + string isFriend_[] = {"NNYN", + "NNNY", + "YNNN", + "NYNN"}; + vector isFriend(isFriend_, isFriend_+sizeof(isFriend_)/sizeof(*isFriend_)); + int d = 584; + int _ = -1; +END +CASE(4) + string isFriend_[] = {"NYNYYYN", + "YNNYYYN", + "NNNNYNN", + "YYNNYYN", + "YYYYNNN", + "YYNYNNY", + "NNNNNYN"}; + vector isFriend(isFriend_, isFriend_+sizeof(isFriend_)/sizeof(*isFriend_)); + int d = 5; + int _ = 20; +END +CASE(5) + string isFriend_[] = {"NYYNNNNYYYYNNNN", + "YNNNYNNNNNNYYNN", + "YNNYNYNNNNYNNNN", + "NNYNNYNNNNNNNNN", + "NYNNNNYNNYNNNNN", + "NNYYNNYNNYNNNYN", + "NNNNYYNNYNNNNNN", + "YNNNNNNNNNYNNNN", + "YNNNNNYNNNNNYNN", + "YNNNYYNNNNNNNNY", + "YNYNNNNYNNNNNNN", + "NYNNNNNNNNNNNNY", + "NYNNNNNNYNNNNYN", + "NNNNNYNNNNNNYNN", + "NNNNNNNNNYNYNNN"} +; + vector isFriend(isFriend_, isFriend_+sizeof(isFriend_)/sizeof(*isFriend_)); + int d = 747; + int _ = 2988; +END +/* +CASE(6) + string isFriend_[] = {"NY", + "YN"}; + vector isFriend(isFriend_, isFriend_+sizeof(isFriend_)/sizeof(*isFriend_)); + int d = 0; + int _ = 0; +END +CASE(7) + string isFriend_[] = ; + vector isFriend(isFriend_, isFriend_+sizeof(isFriend_)/sizeof(*isFriend_)); + int d = ; + int _ = ; +END +CASE(8) + string isFriend_[] = ; + vector isFriend(isFriend_, isFriend_+sizeof(isFriend_)/sizeof(*isFriend_)); + int d = ; + int _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/584-U/1B-U.cpp Index: SRM/584-U/1B-U.cpp ================================================================== --- SRM/584-U/1B-U.cpp +++ SRM/584-U/1B-U.cpp @@ -0,0 +1,238 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef long double LD; +typedef complex CMP; + +template +vector compress(const vector& xs, int origin = 0) +{ + vector< pair > xi; + for(int i=0; i result(xs.size()); + for(int k=0,id=origin; k compress(const std::vector& xs) // convert to 1-origin rank +{ + set s(xs.begin(), xs.end()); + map mp; + int ID = 1; + for(set::iterator it=s.begin(); it!=s.end(); ++it) + mp[*it] = ID++; + + std::vector result(xs.size()); + for(int k=0; k kind, vector depth, vector found, int K) + { + depth = compress(depth, 1); + int MD = *max_element(depth.begin(), depth.end()); + + map< int, vector > kind_to_depth; + set found_set(found.begin(), found.end()); + + for(int i=0; i > tbl_f(K+1, vector(MD+1)); + vector< vector > tbl_nf(K+1, vector(MD+1)); + for(int d=0; d<=MD; ++d) + tbl_f[0][d] = tbl_nf[0][d] = 1; + + for(map >::iterator it=kind_to_depth.begin(); it != kind_to_depth.end(); ++it) { + bool f = !!found_set.count(it->first); + vector ds = it->second; + sort(ds.begin(), ds.end()); + vector< vector > tbl2 = calc_table(K, MD, ds, f); + vector< vector >& tbl = (f ? tbl_f : tbl_nf); + tbl = combine(K, MD, tbl, tbl2); + } + + LL total = 0; + for(int k=0; k<=K; ++k) + total += cross(tbl_f[k], tbl_nf[K-k]); + return total; + } + + LL cross(const vector& f, const vector& n) + { + vector< pair > pf; + for(int d=0; d > nf; + for(int d=0; d > calc_table(int K, int D, const vector& ds, bool found) + { + vector< vector > tbl(K+1, vector(D+1)); + for(int d=0; d<=D; ++d) + { + int num_discover = upper_bound(ds.begin(), ds.end(), d) - ds.begin(); + for(int k=0; k<=K; ++k) + tbl[k][d] = (found ? (k==0 ? 0 : C(num_discover, k)) : C(ds.size()-num_discover, k)); + } + return tbl; + } + + vector< vector > combine(int K, int D, const vector >& s, const vector >& t) + { + vector< vector > r(K+1, vector(D+1)); + for(int ks=0; ks<=K; ++ks) + for(int kt=0; kt<=K; ++kt) if(ks+kt<=K) + for(int d=0; d<=D; ++d) + r[ks+kt][d] += s[ks][d] * t[kt][d]; + return r; + } +}; + +// 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 long long& Expected, const long long& 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(_, Excavations().count(kind, depth, found, K));} +int main(){ + +CASE(0) + int kind_[] = {1, 1, 2, 2}; + vector kind(kind_, kind_+sizeof(kind_)/sizeof(*kind_)); + int depth_[] = {10, 15, 10, 20}; + vector depth(depth_, depth_+sizeof(depth_)/sizeof(*depth_)); + int found_[] = {1}; + vector found(found_, found_+sizeof(found_)/sizeof(*found_)); + int K = 2; + long long _ = 3LL; +END +CASE(1) + int kind_[] = {1, 1, 2, 2}; + vector kind(kind_, kind_+sizeof(kind_)/sizeof(*kind_)); + int depth_[] = {10, 15, 10, 20}; + vector depth(depth_, depth_+sizeof(depth_)/sizeof(*depth_)); + int found_[] = {1, 2}; + vector found(found_, found_+sizeof(found_)/sizeof(*found_)); + int K = 2; + long long _ = 4LL; +END +CASE(2) + int kind_[] = {1, 2, 3, 4}; + vector kind(kind_, kind_+sizeof(kind_)/sizeof(*kind_)); + int depth_[] = {10, 10, 10, 10}; + vector depth(depth_, depth_+sizeof(depth_)/sizeof(*depth_)); + int found_[] = {1, 2}; + vector found(found_, found_+sizeof(found_)/sizeof(*found_)); + int K = 3; + long long _ = 0LL; +END +CASE(3) + int kind_[] = {1, 2, 2, 3, 1, 3, 2, 1, 2}; + vector kind(kind_, kind_+sizeof(kind_)/sizeof(*kind_)); + int depth_[] = {12512, 12859, 125, 1000, 99, 114, 125, 125, 114}; + vector depth(depth_, depth_+sizeof(depth_)/sizeof(*depth_)); + int found_[] = {1, 2, 3}; + vector found(found_, found_+sizeof(found_)/sizeof(*found_)); + int K = 7; + long long _ = 35LL; +END +CASE(4) + int kind_[] = {50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50}; + vector kind(kind_, kind_+sizeof(kind_)/sizeof(*kind_)); + int depth_[] = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3}; + vector depth(depth_, depth_+sizeof(depth_)/sizeof(*depth_)); + int found_[] = {50}; + vector found(found_, found_+sizeof(found_)/sizeof(*found_)); + int K = 18; + long long _ = 9075135300LL; +END +/* +CASE(5) + int kind_[] = ; + vector kind(kind_, kind_+sizeof(kind_)/sizeof(*kind_)); + int depth_[] = ; + vector depth(depth_, depth_+sizeof(depth_)/sizeof(*depth_)); + int found_[] = ; + vector found(found_, found_+sizeof(found_)/sizeof(*found_)); + int K = ; + long long _ = LL; +END +CASE(6) + int kind_[] = ; + vector kind(kind_, kind_+sizeof(kind_)/sizeof(*kind_)); + int depth_[] = ; + vector depth(depth_, depth_+sizeof(depth_)/sizeof(*depth_)); + int found_[] = ; + vector found(found_, found_+sizeof(found_)/sizeof(*found_)); + int K = ; + long long _ = LL; +END + */ +} +// END CUT HERE