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 <iostream> +#include <sstream> +#include <iomanip> +#include <vector> +#include <string> +#include <map> +#include <set> +#include <algorithm> +#include <numeric> +#include <iterator> +#include <functional> +#include <complex> +#include <queue> +#include <stack> +#include <cmath> +#include <cassert> +using namespace std; +typedef long long LL; +typedef long double LD; +typedef complex<double> CMP; + +class Egalitarianism { public: + int maxDifference(vector <string> isFriend, int d) + { + int N = isFriend.size(); + int INF = 0x3fffffff; + vector< vector<int> > g(N, vector<int>(N, INF)); + for(int x=0; x<N; ++x) + for(int y=0; y<N; ++y) + if(x==y) + g[x][y] = 0; + else if(isFriend[x][y]=='Y') + g[x][y] = 1; + + for(int k=0; k<N; ++k) + for(int i=0; i<N; ++i) + for(int j=0; j<N; ++j) + g[i][j] = min(g[i][j], g[i][k]+g[k][j]); + + int maxd = 0; + for(int x=0; x<N; ++x) + for(int y=0; y<N; ++y) + maxd = max(maxd, g[x][y]); + return maxd==INF ? -1 : maxd*d; + } +}; + +// BEGIN CUT HERE +#include <ctime> +double start_time; string timer() + { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } +template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) + { os << "{ "; + for(typename vector<T>::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 <string> isFriend(isFriend_, isFriend_+sizeof(isFriend_)/sizeof(*isFriend_)); + int d = 10; + int _ = 20; +END +CASE(1) + string isFriend_[] = {"NN", + "NN"}; + vector <string> isFriend(isFriend_, isFriend_+sizeof(isFriend_)/sizeof(*isFriend_)); + int d = 1; + int _ = -1; +END +CASE(2) + string isFriend_[] = {"NNYNNN", + "NNYNNN", + "YYNYNN", + "NNYNYY", + "NNNYNN", + "NNNYNN"}; + vector <string> isFriend(isFriend_, isFriend_+sizeof(isFriend_)/sizeof(*isFriend_)); + int d = 1000; + int _ = 3000; +END +CASE(3) + string isFriend_[] = {"NNYN", + "NNNY", + "YNNN", + "NYNN"}; + vector <string> 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 <string> 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 <string> isFriend(isFriend_, isFriend_+sizeof(isFriend_)/sizeof(*isFriend_)); + int d = 747; + int _ = 2988; +END +/* +CASE(6) + string isFriend_[] = {"NY", + "YN"}; + vector <string> isFriend(isFriend_, isFriend_+sizeof(isFriend_)/sizeof(*isFriend_)); + int d = 0; + int _ = 0; +END +CASE(7) + string isFriend_[] = ; + vector <string> isFriend(isFriend_, isFriend_+sizeof(isFriend_)/sizeof(*isFriend_)); + int d = ; + int _ = ; +END +CASE(8) + string isFriend_[] = ; + vector <string> 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 <iostream> +#include <sstream> +#include <iomanip> +#include <vector> +#include <string> +#include <map> +#include <set> +#include <algorithm> +#include <numeric> +#include <iterator> +#include <functional> +#include <complex> +#include <queue> +#include <stack> +#include <cmath> +#include <cassert> +using namespace std; +typedef long long LL; +typedef long double LD; +typedef complex<double> CMP; + +template<typename T> +vector<int> compress(const vector<T>& xs, int origin = 0) +{ + vector< pair<T,int> > xi; + for(int i=0; i<xs.size(); ++i) + xi.push_back( make_pair(xs[i], i) ); + sort(xi.begin(), xi.end()); + + vector<int> result(xs.size()); + for(int k=0,id=origin; k<xi.size(); ++k) { + if(0<=k-1 && xi[k-1].first<xi[k].first) + ++id; + result[xi[k].second] = id; + } + return result; +} + +std::vector<int> compress(const std::vector<int>& xs) // convert to 1-origin rank +{ + set<int> s(xs.begin(), xs.end()); + map<int,int> mp; + int ID = 1; + for(set<int>::iterator it=s.begin(); it!=s.end(); ++it) + mp[*it] = ID++; + + std::vector<int> result(xs.size()); + for(int k=0; k<xs.size(); ++k) + result[k] = mp[xs[k]]; + return result; +} + +LL C(LL n, LL k) +{ + if(n<k) + return 0; + + k = min(k, n-k); + + LL c = 1; + for(LL i=0; i<k; ++i) + c *= n-i, c /= i+1; + return c; +} + +class Excavations { public: + long long count(vector <int> kind, vector <int> depth, vector <int> found, int K) + { + depth = compress(depth, 1); + int MD = *max_element(depth.begin(), depth.end()); + + map< int, vector<int> > kind_to_depth; + set<int> found_set(found.begin(), found.end()); + + for(int i=0; i<kind.size(); ++i) + kind_to_depth[kind[i]].push_back(depth[i]); + + vector< vector<LL> > tbl_f(K+1, vector<LL>(MD+1)); + vector< vector<LL> > tbl_nf(K+1, vector<LL>(MD+1)); + for(int d=0; d<=MD; ++d) + tbl_f[0][d] = tbl_nf[0][d] = 1; + + for(map<int,vector<int> >::iterator it=kind_to_depth.begin(); it != kind_to_depth.end(); ++it) { + bool f = !!found_set.count(it->first); + vector<int> ds = it->second; + sort(ds.begin(), ds.end()); + vector< vector<LL> > tbl2 = calc_table(K, MD, ds, f); + vector< vector<LL> >& 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<LL>& f, const vector<LL>& n) + { + vector< pair<int,LL> > pf; + for(int d=0; d<f.size(); ++d) { + LL cnt = (d==0 ? f[d] : f[d]-f[d-1]); + if(cnt) { + pf.push_back(make_pair(d, cnt)); + } + } + + vector< pair<int,LL> > nf; + for(int d=0; d<n.size(); ++d) { + LL cnt = (d==n.size()-1 ? n[d] : n[d]-n[d+1]); + if(cnt) { + nf.push_back(make_pair(d, cnt)); + } + } + + LL total = 0; + for(int i=0; i<pf.size(); ++i) + for(int k=0; k<nf.size(); ++k) + { + if(pf[i].first <= nf[k].first) + total += pf[i].second * nf[k].second; + } + return total; + } + + vector< vector<LL> > calc_table(int K, int D, const vector<int>& ds, bool found) + { + vector< vector<LL> > tbl(K+1, vector<LL>(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<LL> > combine(int K, int D, const vector<vector<LL> >& s, const vector<vector<LL> >& t) + { + vector< vector<LL> > r(K+1, vector<LL>(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 <ctime> +double start_time; string timer() + { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } +template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) + { os << "{ "; + for(typename vector<T>::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 <int> kind(kind_, kind_+sizeof(kind_)/sizeof(*kind_)); + int depth_[] = {10, 15, 10, 20}; + vector <int> depth(depth_, depth_+sizeof(depth_)/sizeof(*depth_)); + int found_[] = {1}; + vector <int> found(found_, found_+sizeof(found_)/sizeof(*found_)); + int K = 2; + long long _ = 3LL; +END +CASE(1) + int kind_[] = {1, 1, 2, 2}; + vector <int> kind(kind_, kind_+sizeof(kind_)/sizeof(*kind_)); + int depth_[] = {10, 15, 10, 20}; + vector <int> depth(depth_, depth_+sizeof(depth_)/sizeof(*depth_)); + int found_[] = {1, 2}; + vector <int> found(found_, found_+sizeof(found_)/sizeof(*found_)); + int K = 2; + long long _ = 4LL; +END +CASE(2) + int kind_[] = {1, 2, 3, 4}; + vector <int> kind(kind_, kind_+sizeof(kind_)/sizeof(*kind_)); + int depth_[] = {10, 10, 10, 10}; + vector <int> depth(depth_, depth_+sizeof(depth_)/sizeof(*depth_)); + int found_[] = {1, 2}; + vector <int> 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 <int> kind(kind_, kind_+sizeof(kind_)/sizeof(*kind_)); + int depth_[] = {12512, 12859, 125, 1000, 99, 114, 125, 125, 114}; + vector <int> depth(depth_, depth_+sizeof(depth_)/sizeof(*depth_)); + int found_[] = {1, 2, 3}; + vector <int> 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 <int> 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 <int> depth(depth_, depth_+sizeof(depth_)/sizeof(*depth_)); + int found_[] = {50}; + vector <int> found(found_, found_+sizeof(found_)/sizeof(*found_)); + int K = 18; + long long _ = 9075135300LL; +END +/* +CASE(5) + int kind_[] = ; + vector <int> kind(kind_, kind_+sizeof(kind_)/sizeof(*kind_)); + int depth_[] = ; + vector <int> depth(depth_, depth_+sizeof(depth_)/sizeof(*depth_)); + int found_[] = ; + vector <int> found(found_, found_+sizeof(found_)/sizeof(*found_)); + int K = ; + long long _ = LL; +END +CASE(6) + int kind_[] = ; + vector <int> kind(kind_, kind_+sizeof(kind_)/sizeof(*kind_)); + int depth_[] = ; + vector <int> depth(depth_, depth_+sizeof(depth_)/sizeof(*depth_)); + int found_[] = ; + vector <int> found(found_, found_+sizeof(found_)/sizeof(*found_)); + int K = ; + long long _ = LL; +END + */ +} +// END CUT HERE