Skip to content

Instantly share code, notes, and snippets.

@lychees
Last active December 20, 2015 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lychees/6202038 to your computer and use it in GitHub Desktop.
Save lychees/6202038 to your computer and use it in GitHub Desktop.
SRM 584
/* &*#()&*#)&E*F& */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <cassert>
using namespace std;
#define REP(I, N) for (int I=0;I<int(N);++I)
#define FOR(I, A, B) for (int I=int(A);I<int(B);++I)
#define DWN(I, B, A) for (int I=int(B-1);I>=int(A);--I)
#define REP_1(i, n) for (int i=1;i<=int(n);++i)
#define ECH(it, A) for (typeof(A.begin()) it=A.begin(); it != A.end(); ++it)
#define ALL(A) A.begin(), A.end()
#define CLR(A) A.clear()
#define CPY(A, B) memcpy(A, B, sizeof(A))
#define INS(A, P, B) A.insert(A.begin() + P, B)
#define ERS(A, P) A.erase(A.begin() + P)
#define SRT(A) sort(ALL(A))
#define SZ(A) int(A.size())
#define PB push_back
#define MP(A, B) make_pair(A, B)
typedef long long LL;
typedef double DB;
typedef vector<int> VI;
template<class T> inline void RST(T &A){memset(A, 0, sizeof(A));}
template<class T> inline void FLC(T &A, int x){memset(A, x, sizeof(A));}
template<class T> inline void checkMin(T &a, T b){if (b<a) a=b;}
template<class T> inline void checkMax(T &a, T b){if (b>a) a=b;}
const int MOD = 1000000007;
const int INF = 0x3f3f3f3f;
/* -&$&#*( &#*@)^$@&*)*/
const int N = 60;
LL Binom[N][N], dp[N][N];
VI A[N]; int LIM, n, m;
LL f(int n, int k){
if (n > k) return 0;
if (!n--) return k ? 0 : 1;
LL &res = dp[n][k];
if (!~res){
res = 0; REP_1(i, min(k, SZ(A[n]))) REP(j, SZ(A[n])-i+1){
if (A[n][j] >= LIM) break;
res += Binom[SZ(A[n])-j-1][i-1] * f(n, k-i);
}
}
return res;
}
class Excavations {
public:
long long count(vector <int> kind, vector <int> depth, vector <int> found, int K) {
REP(i, N){Binom[i][0] = 1; REP_1(j, i) Binom[i][j] = Binom[i-1][j-1] + Binom[i-1][j];}
n = SZ(kind), m = SZ(found); REP(i, m+1) CLR(A[i]);
REP(i, n) A[find(ALL(found), kind[i]) - found.begin()].PB(depth[i]);
REP(i, m+1) SRT(A[i]);
FLC(dp, -1); LIM = INF; LL res = f(m, K);
DWN(i, SZ(A[m]), 0){
FLC(dp, -1), LIM = A[m][i];
REP_1(k, K-m) res += Binom[SZ(A[m])-i-1][k-1] * f(m, K-k);
}
return res;
}
};
// BEGIN CUT HERE
namespace moj_harness {
int run_test_case(int);
void run_test(int casenum = -1, bool quiet = false) {
if (casenum != -1) {
if (run_test_case(casenum) == -1 && !quiet) {
cerr << "Illegal input! Test case " << casenum << " does not exist." << endl;
}
return;
}
int correct = 0, total = 0;
for (int i=0;; ++i) {
int x = run_test_case(i);
if (x == -1) {
if (i >= 100) break;
continue;
}
correct += x;
++total;
}
if (total == 0) {
cerr << "No test cases run." << endl;
} else if (correct < total) {
cerr << "Some cases FAILED (passed " << correct << " of " << total << ")." << endl;
} else {
cerr << "All " << total << " tests passed!" << endl;
}
}
int verify_case(int casenum, const long long &expected, const long long &received, clock_t elapsed) {
cerr << "Example " << casenum << "... ";
string verdict;
vector<string> info;
char buf[100];
if (elapsed > CLOCKS_PER_SEC / 200) {
sprintf(buf, "time %.2fs", elapsed * (1.0/CLOCKS_PER_SEC));
info.push_back(buf);
}
if (expected == received) {
verdict = "PASSED";
} else {
verdict = "FAILED";
}
cerr << verdict;
if (!info.empty()) {
cerr << " (";
for (int i=0; i<(int)info.size(); ++i) {
if (i > 0) cerr << ", ";
cerr << info[i];
}
cerr << ")";
}
cerr << endl;
if (verdict == "FAILED") {
cerr << " Expected: " << expected << endl;
cerr << " Received: " << received << endl;
}
return verdict == "PASSED";
}
int run_test_case(int casenum) {
switch (casenum) {
case 0: {
int kind[] = {1, 1, 2, 2};
int depth[] = {10, 15, 10, 20};
int found[] = {1};
int K = 2;
long long expected__ = 3;
clock_t start__ = clock();
long long received__ = Excavations().count(vector <int>(kind, kind + (sizeof kind / sizeof kind[0])), vector <int>(depth, depth + (sizeof depth / sizeof depth[0])), vector <int>(found, found + (sizeof found / sizeof found[0])), K);
return verify_case(casenum, expected__, received__, clock()-start__);
}
case 1: {
int kind[] = {1, 1, 2, 2};
int depth[] = {10, 15, 10, 20};
int found[] = {1, 2};
int K = 2;
long long expected__ = 4;
clock_t start__ = clock();
long long received__ = Excavations().count(vector <int>(kind, kind + (sizeof kind / sizeof kind[0])), vector <int>(depth, depth + (sizeof depth / sizeof depth[0])), vector <int>(found, found + (sizeof found / sizeof found[0])), K);
return verify_case(casenum, expected__, received__, clock()-start__);
}
case 2: {
int kind[] = {1, 2, 3, 4};
int depth[] = {10, 10, 10, 10};
int found[] = {1, 2};
int K = 3;
long long expected__ = 0;
clock_t start__ = clock();
long long received__ = Excavations().count(vector <int>(kind, kind + (sizeof kind / sizeof kind[0])), vector <int>(depth, depth + (sizeof depth / sizeof depth[0])), vector <int>(found, found + (sizeof found / sizeof found[0])), K);
return verify_case(casenum, expected__, received__, clock()-start__);
}
case 3: {
int kind[] = {1, 2, 2, 3, 1, 3, 2, 1, 2};
int depth[] = {12512, 12859, 125, 1000, 99, 114, 125, 125, 114};
int found[] = {1, 2, 3};
int K = 7;
long long expected__ = 35;
clock_t start__ = clock();
long long received__ = Excavations().count(vector <int>(kind, kind + (sizeof kind / sizeof kind[0])), vector <int>(depth, depth + (sizeof depth / sizeof depth[0])), vector <int>(found, found + (sizeof found / sizeof found[0])), K);
return verify_case(casenum, expected__, received__, clock()-start__);
}
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};
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};
int found[] = {50};
int K = 18;
long long expected__ = 9075135300LL;
clock_t start__ = clock();
long long received__ = Excavations().count(vector <int>(kind, kind + (sizeof kind / sizeof kind[0])), vector <int>(depth, depth + (sizeof depth / sizeof depth[0])), vector <int>(found, found + (sizeof found / sizeof found[0])), K);
return verify_case(casenum, expected__, received__, clock()-start__);
}
// custom cases
/* case 5: {
int kind[] = ;
int depth[] = ;
int found[] = ;
int K = ;
long long expected__ = ;
clock_t start__ = clock();
long long received__ = Excavations().count(vector <int>(kind, kind + (sizeof kind / sizeof kind[0])), vector <int>(depth, depth + (sizeof depth / sizeof depth[0])), vector <int>(found, found + (sizeof found / sizeof found[0])), K);
return verify_case(casenum, expected__, received__, clock()-start__);
}*/
/* case 6: {
int kind[] = ;
int depth[] = ;
int found[] = ;
int K = ;
long long expected__ = ;
clock_t start__ = clock();
long long received__ = Excavations().count(vector <int>(kind, kind + (sizeof kind / sizeof kind[0])), vector <int>(depth, depth + (sizeof depth / sizeof depth[0])), vector <int>(found, found + (sizeof found / sizeof found[0])), K);
return verify_case(casenum, expected__, received__, clock()-start__);
}*/
/* case 7: {
int kind[] = ;
int depth[] = ;
int found[] = ;
int K = ;
long long expected__ = ;
clock_t start__ = clock();
long long received__ = Excavations().count(vector <int>(kind, kind + (sizeof kind / sizeof kind[0])), vector <int>(depth, depth + (sizeof depth / sizeof depth[0])), vector <int>(found, found + (sizeof found / sizeof found[0])), K);
return verify_case(casenum, expected__, received__, clock()-start__);
}*/
default:
return -1;
}
}
}
int main(int argc, char *argv[]) {
if (argc == 1) {
moj_harness::run_test();
} else {
for (int i=1; i<argc; ++i)
moj_harness::run_test(atoi(argv[i]));
}
}
// END CUT HERE
/** Micro Mezz Macro Flation -- Overheated Economy ., Last Update: Aug. 4th 2013 **/ //{
/** Header .. **/ //{
#pragma comment(linker, "/STACK:36777216")
//#pragma GCC optimize ("O2")
#define LOCAL
//#include "testlib.h"
#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <climits>
#include <cassert>
#include <complex>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
//#include <tr1/unordered_set>
//#include <tr1/unordered_map>
//#include <array>
using namespace std;
#define REP(i, n) for (int i=0;i<int(n);++i)
#define FOR(i, a, b) for (int i=int(a);i<int(b);++i)
#define DWN(i, b, a) for (int i=int(b-1);i>=int(a);--i)
#define REP_1(i, n) for (int i=1;i<=int(n);++i)
#define FOR_1(i, a, b) for (int i=int(a);i<=int(b);++i)
#define DWN_1(i, b, a) for (int i=int(b);i>=int(a);--i)
#define REP_C(i, n) for (int n____=int(n),i=0;i<n____;++i)
#define FOR_C(i, a, b) for (int b____=int(b),i=a;i<b____;++i)
#define DWN_C(i, b, a) for (int a____=int(a),i=b-1;i>=a____;--i)
#define REP_N(i, n) for (i=0;i<int(n);++i)
#define FOR_N(i, a, b) for (i=int(a);i<int(b);++i)
#define DWN_N(i, b, a) for (i=int(b-1);i>=int(a);--i)
#define REP_1_C(i, n) for (int n____=int(n),i=1;i<=n____;++i)
#define FOR_1_C(i, a, b) for (int b____=int(b),i=a;i<=b____;++i)
#define DWN_1_C(i, b, a) for (int a____=int(a),i=b;i>=a____;--i)
#define REP_1_N(i, n) for (i=1;i<=int(n);++i)
#define FOR_1_N(i, a, b) for (i=int(a);i<=int(b);++i)
#define DWN_1_N(i, b, a) for (i=int(b);i>=int(a);--i)
#define REP_C_N(i, n) for (int n____=(i=0,int(n));i<n____;++i)
#define FOR_C_N(i, a, b) for (int b____=(i=0,int(b);i<b____;++i)
#define DWN_C_N(i, b, a) for (int a____=(i=b-1,int(a));i>=a____;--i)
#define REP_1_C_N(i, n) for (int n____=(i=1,int(n));i<=n____;++i)
#define FOR_1_C_N(i, a, b) for (int b____=(i=1,int(b);i<=b____;++i)
#define DWN_1_C_N(i, b, a) for (int a____=(i=b,int(a));i>=a____;--i)
#define ECH(it, A) for (__typeof(A.begin()) it=A.begin(); it != A.end(); ++it)
#define REP_S(i, str) for (char*i=str;*i;++i)
#define REP_L(i, hd, nxt) for (int i=hd;i;i=nxt[i])
#define REP_G(i, u) REP_L(i,hd[u],suc)
#define REP_SS(x, s) for (int x=s;x;x=(x-1)&s)
#define DO(n) for ( int ____n = n; ____n-->0; )
#define REP_2(i, j, n, m) REP(i, n) REP(j, m)
#define REP_2_1(i, j, n, m) REP_1(i, n) REP_1(j, m)
#define REP_3(i, j, k, n, m, l) REP(i, n) REP(j, m) REP(k, l)
#define REP_3_1(i, j, k, n, m, l) REP_1(i, n) REP_1(j, m) REP_1(k, l)
#define REP_4(i, j, k, ii, n, m, l, nn) REP(i, n) REP(j, m) REP(k, l) REP(ii, nn)
#define REP_4_1(i, j, k, ii, n, m, l, nn) REP_1(i, n) REP_1(j, m) REP_1(k, l) REP_1(ii, nn)
#define ALL(A) A.begin(), A.end()
#define LLA(A) A.rbegin(), A.rend()
#define CPY(A, B) memcpy(A, B, sizeof(A))
#define INS(A, P, B) A.insert(A.begin() + P, B)
#define ERS(A, P) A.erase(A.begin() + P)
#define BSC(A, x) (lower_bound(ALL(A), x) - A.begin())
#define CTN(T, x) (T.find(x) != T.end())
#define SZ(A) int((A).size())
#define PB push_back
#define MP(A, B) make_pair(A, B)
#define PTT pair<T, T>
#define fi first
#define se second
#define re real()
#define im imag()
#define Rush for(int ____T=RD(); ____T--;)
#define Display(A, n, m) { \
REP(i, n){ \
REP(j, m-1) cout << A[i][j] << " "; \
cout << A[i][m-1] << endl; \
} \
}
#define Display_1(A, n, m) { \
REP_1(i, n){ \
REP_1(j, m-1) cout << A[i][j] << " "; \
cout << A[i][m] << endl; \
} \
}
string __file__(){
string res = __FILE__;
int r = SZ(res) - 1; while (res[r] != '.') --r;
int l = r - 1; while (res[l] != '\\') --l; ++l;
return res.substr(l, r-l);
}
void Exec(string a, string b, string c){
if (b.empty()) b = __file__();
string cmd = a + ' ' + b + '.' + c;
system(cmd.c_str());
}
void Ruby(string file = ""){Exec("ruby", file, "rb");}
void Python(string file = ""){Exec("python", file, "py");}
void Haskell(string file = ""){Exec("runghc", file, "hs");}
void Pascal(string file = ""){Exec("pascal", file, "pas");}
void Ocaml(string file = ""){Exec("ocaml", file, "ml");}
typedef long long LL;
//typedef long double DB;
typedef double DB;
typedef unsigned UINT;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<char> VC;
typedef vector<string> VS;
typedef vector<LL> VL;
typedef vector<DB> VF;
typedef set<int> SI;
typedef set<string> SS;
typedef map<int, int> MII;
typedef map<string, int> MSI;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef vector<VII> VVII;
template<class T> inline T& RD(T &);
template<class T> inline void OT(const T &);
//inline int RD(){int x; return RD(x);}
inline LL RD(){LL x; return RD(x);}
inline DB& RF(DB &);
inline DB RF(){DB x; return RF(x);}
inline char* RS(char *s);
inline char& RC(char &c);
inline char RC();
inline char& RC(char &c){scanf(" %c", &c); return c;}
inline char RC(){char c; return RC(c);}
//inline char& RC(char &c){c = getchar(); return c;}
//inline char RC(){return getchar();}
template<class T> inline T& RDD(T &x){
char c; for (c = getchar(); c < '-'; c = getchar());
if (c == '-'){x = '0' - getchar(); for (c = getchar(); '0' <= c && c <= '9'; c = getchar()) x = x * 10 + '0' - c;}
else {x = c - '0'; for (c = getchar(); '0' <= c && c <= '9'; c = getchar()) x = x * 10 + c - '0';}
return x;
}
inline LL RDD(){LL x; return RDD(x);}
template<class T0, class T1> inline T0& RD(T0 &x0, T1 &x1){RD(x0), RD(x1); return x0;}
template<class T0, class T1, class T2> inline T0& RD(T0 &x0, T1 &x1, T2 &x2){RD(x0), RD(x1), RD(x2); return x0;}
template<class T0, class T1, class T2, class T3> inline T0& RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3){RD(x0), RD(x1), RD(x2), RD(x3); return x0;}
template<class T0, class T1, class T2, class T3, class T4> inline T0& RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4); return x0;}
template<class T0, class T1, class T2, class T3, class T4, class T5> inline T0& RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5); return x0;}
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline T0& RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5), RD(x6); return x0;}
template<class T0, class T1> inline void OT(const T0 &x0, const T1 &x1){OT(x0), OT(x1);}
template<class T0, class T1, class T2> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2){OT(x0), OT(x1), OT(x2);}
template<class T0, class T1, class T2, class T3> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3){OT(x0), OT(x1), OT(x2), OT(x3);}
template<class T0, class T1, class T2, class T3, class T4> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3, const T4 &x4){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4);}
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3, const T4 &x4, const T5 &x5){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5);}
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3, const T4 &x4, const T5 &x5, const T6 &x6){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5), OT(x6);}
inline char& RC(char &a, char &b){RC(a), RC(b); return a;}
inline char& RC(char &a, char &b, char &c){RC(a), RC(b), RC(c); return a;}
inline char& RC(char &a, char &b, char &c, char &d){RC(a), RC(b), RC(c), RC(d); return a;}
inline char& RC(char &a, char &b, char &c, char &d, char &e){RC(a), RC(b), RC(c), RC(d), RC(e); return a;}
inline char& RC(char &a, char &b, char &c, char &d, char &e, char &f){RC(a), RC(b), RC(c), RC(d), RC(e), RC(f); return a;}
inline char& RC(char &a, char &b, char &c, char &d, char &e, char &f, char &g){RC(a), RC(b), RC(c), RC(d), RC(e), RC(f), RC(g); return a;}
inline DB& RF(DB &a, DB &b){RF(a), RF(b); return a;}
inline DB& RF(DB &a, DB &b, DB &c){RF(a), RF(b), RF(c); return a;}
inline DB& RF(DB &a, DB &b, DB &c, DB &d){RF(a), RF(b), RF(c), RF(d); return a;}
inline DB& RF(DB &a, DB &b, DB &c, DB &d, DB &e){RF(a), RF(b), RF(c), RF(d), RF(e); return a;}
inline DB& RF(DB &a, DB &b, DB &c, DB &d, DB &e, DB &f){RF(a), RF(b), RF(c), RF(d), RF(e), RF(f); return a;}
inline DB& RF(DB &a, DB &b, DB &c, DB &d, DB &e, DB &f, DB &g){RF(a), RF(b), RF(c), RF(d), RF(e), RF(f), RF(g); return a;}
inline void RS(char *s1, char *s2){RS(s1), RS(s2);}
inline void RS(char *s1, char *s2, char *s3){RS(s1), RS(s2), RS(s3);}
template<class T0,class T1>inline void RDD(T0&a, T1&b){RDD(a),RDD(b);}
template<class T0,class T1,class T2>inline void RDD(T0&a, T1&b, T2&c){RDD(a),RDD(b),RDD(c);}
template<class T> inline void RST(T &A){memset(A, 0, sizeof(A));}
template<class T> inline void FLC(T &A, int x){memset(A, x, sizeof(A));}
template<class T> inline void CLR(T &A){A.clear();}
template<class T0, class T1> inline void RST(T0 &A0, T1 &A1){RST(A0), RST(A1);}
template<class T0, class T1, class T2> inline void RST(T0 &A0, T1 &A1, T2 &A2){RST(A0), RST(A1), RST(A2);}
template<class T0, class T1, class T2, class T3> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3){RST(A0), RST(A1), RST(A2), RST(A3);}
template<class T0, class T1, class T2, class T3, class T4> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4);}
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5);}
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5), RST(A6);}
template<class T0, class T1> inline void FLC(T0 &A0, T1 &A1, int x){FLC(A0, x), FLC(A1, x);}
template<class T0, class T1, class T2> inline void FLC(T0 &A0, T1 &A1, T2 &A2, int x){FLC(A0, x), FLC(A1, x), FLC(A2, x);}
template<class T0, class T1, class T2, class T3> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, int x){FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x);}
template<class T0, class T1, class T2, class T3, class T4> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, int x){FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x);}
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, int x){FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x), FLC(A5, x);}
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6, int x){FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x), FLC(A5, x), FLC(A6, x);}
template<class T> inline void CLR(priority_queue<T, vector<T>, less<T> > &Q){while (!Q.empty()) Q.pop();}
template<class T> inline void CLR(priority_queue<T, vector<T>, greater<T> > &Q){while (!Q.empty()) Q.pop();}
template<class T> inline void CLR(stack<T> &S){while (!S.empty()) S.pop();}
template<class T0, class T1> inline void CLR(T0 &A0, T1 &A1){CLR(A0), CLR(A1);}
template<class T0, class T1, class T2> inline void CLR(T0 &A0, T1 &A1, T2 &A2){CLR(A0), CLR(A1), CLR(A2);}
template<class T0, class T1, class T2, class T3> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3){CLR(A0), CLR(A1), CLR(A2), CLR(A3);}
template<class T0, class T1, class T2, class T3, class T4> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4);}
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5);}
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5), CLR(A6);}
template<class T> inline void CLR(T &A, int n){REP(i, n) CLR(A[i]);}
template<class T> inline bool EPT(T &a){return a.empty();}
template<class T> inline T& SRT(T &A){sort(ALL(A)); return A;}
template<class T> inline T& RVS(T &A){reverse(ALL(A)); return A;}
template<class T> inline T& UNQ(T &A){A.resize(unique(ALL(SRT(A)))-A.begin());return A;}
template<class T, class C> inline T& SRT(T &A, C B){sort(ALL(A), B); return A;}
//}
/** Constant List .. **/ //{
const int MOD = int(1e9) + 7;
//int MOD = 99990001;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f3f3f3f3f3f3fLL;
const DB EPS = 1e-9;
const DB OO = 1e20;
const DB PI = acos(-1.0); //M_PI;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
//}
/** Add On .. **/ //{
// <<= '0. Nichi Joo ., //{
template<class T> inline void checkMin(T &a,const T b){if (b<a) a=b;}
template<class T> inline void checkMax(T &a,const T b){if (a<b) a=b;}
template<class T> inline void checkMin(T &a, T &b, const T x){checkMin(a, x), checkMin(b, x);}
template<class T> inline void checkMax(T &a, T &b, const T x){checkMax(a, x), checkMax(b, x);}
template <class T, class C> inline void checkMin(T& a, const T b, C c){if (c(b,a)) a = b;}
template <class T, class C> inline void checkMax(T& a, const T b, C c){if (c(a,b)) a = b;}
template<class T> inline T min(T a, T b, T c){return min(min(a, b), c);}
template<class T> inline T max(T a, T b, T c){return max(max(a, b), c);}
template<class T> inline T min(T a, T b, T c, T d){return min(min(a, b), min(c, d));}
template<class T> inline T max(T a, T b, T c, T d){return max(max(a, b), max(c, d));}
template<class T> inline T sqr(T a){return a*a;}
template<class T> inline T cub(T a){return a*a*a;}
template<class T> inline T ceil(T x, T y){return (x - 1) / y + 1;}
inline int sgn(DB x){return x < -EPS ? -1 : x > EPS;}
inline int sgn(DB x, DB y){return sgn(x - y);}
inline DB cot(DB x){return 1./tan(x);};
inline DB sec(DB x){return 1./cos(x);};
inline DB csc(DB x){return 1./sin(x);};
//}
// <<= '1. Bitwise Operation ., //{
namespace BO{
inline bool _1(int x, int i){return bool(x&1<<i);}
inline bool _1(LL x, int i){return bool(x&1LL<<i);}
inline LL _1(int i){return 1LL<<i;}
inline LL _U(int i){return _1(i) - 1;};
inline int reverse_bits(int x){
x = ((x >> 1) & 0x55555555) | ((x << 1) & 0xaaaaaaaa);
x = ((x >> 2) & 0x33333333) | ((x << 2) & 0xcccccccc);
x = ((x >> 4) & 0x0f0f0f0f) | ((x << 4) & 0xf0f0f0f0);
x = ((x >> 8) & 0x00ff00ff) | ((x << 8) & 0xff00ff00);
x = ((x >>16) & 0x0000ffff) | ((x <<16) & 0xffff0000);
return x;
}
inline LL reverse_bits(LL x){
x = ((x >> 1) & 0x5555555555555555LL) | ((x << 1) & 0xaaaaaaaaaaaaaaaaLL);
x = ((x >> 2) & 0x3333333333333333LL) | ((x << 2) & 0xccccccccccccccccLL);
x = ((x >> 4) & 0x0f0f0f0f0f0f0f0fLL) | ((x << 4) & 0xf0f0f0f0f0f0f0f0LL);
x = ((x >> 8) & 0x00ff00ff00ff00ffLL) | ((x << 8) & 0xff00ff00ff00ff00LL);
x = ((x >>16) & 0x0000ffff0000ffffLL) | ((x <<16) & 0xffff0000ffff0000LL);
x = ((x >>32) & 0x00000000ffffffffLL) | ((x <<32) & 0xffffffff00000000LL);
return x;
}
template<class T> inline bool odd(T x){return x&1;}
template<class T> inline bool even(T x){return !odd(x);}
template<class T> inline T low_bit(T x) {return x & -x;}
template<class T> inline T high_bit(T x) {T p = low_bit(x);while (p != x) x -= p, p = low_bit(x);return p;}
template<class T> inline T cover_bit(T x){T p = 1; while (p < x) p <<= 1;return p;}
template<class T> inline int cover_idx(T x){int p = 0; while (_1(p) < x ) ++p; return p;}
inline int clz(int x){return __builtin_clz(x);}
inline int clz(LL x){return __builtin_clzll(x);}
inline int ctz(int x){return __builtin_ctz(x);}
inline int ctz(LL x){return __builtin_ctzll(x);}
inline int lg2(int x){return !x ? -1 : 31 - clz(x);}
inline int lg2(LL x){return !x ? -1 : 63 - clz(x);}
inline int low_idx(int x){return !x ? -1 : ctz(x);}
inline int low_idx(LL x){return !x ? -1 : ctz(x);}
inline int high_idx(int x){return lg2(x);}
inline int high_idx(LL x){return lg2(x);}
inline int parity(int x){return __builtin_parity(x);}
inline int parity(LL x){return __builtin_parityll(x);}
inline int count_bits(int x){return __builtin_popcount(x);}
inline int count_bits(LL x){return __builtin_popcountll(x);}
} using namespace BO;//}
//}
/** I/O Accelerator Interface .. **/ //{
template<class T> inline T& RD(T &x){
//cin >> x;
//scanf("%d", &x);
char c; for (c = getchar(); c < '0'; c = getchar()); x = c - '0'; for (c = getchar(); '0' <= c && c <= '9'; c = getchar()) x = x * 10 + c - '0';
//char c; c = getchar(); x = c - '0'; for (c = getchar(); c >= '0'; c = getchar()) x = x * 10 + c - '0';
return x;
}
inline DB& RF(DB &x){
//cin >> x;
scanf("%lf", &x);
/*char t; while ((t=getchar())==' '||t=='\n'); x = t - '0';
while ((t=getchar())!=' '&&t!='\n'&&t!='.')x*=10,x+=t-'0';
if (t=='.'){DB l=1; while ((t=getchar())!=' '&&t!='\n')l*=0.1,x += (t-'0')*l;}*/
return x;
}
inline char* RS(char *s){
//gets(s);
scanf("%s", s);
return s;
}
LL last_ans; int Case; template<class T> inline void OT(const T &x){
//printf("Case %d: %d\n", ++Case, x);
//printf("%lld\n", x);
//printf("%.9lf\n", x);
printf("%d\n", x);
//cout << x << endl;
//last_ans = x;
}
//}
//}/* .................................................................................................................................. */
const int N = 500 * 2 + 9;
int G[N][N], Cur[N], fa[N], ww[N], bj[N];
int n, nn, mst;
void Gen(){
Cur[n++] = nn++;
}
void Del(int x){
swap(Cur[x], Cur[--n]);
}
#define u Cur[i]
#define v Cur[j]
#define uu bj[u]
#define vv bj[v]
bool Find(){
int _n = n, _nn = nn; RST(fa); FLC(ww, 0x3f);
REP_2(i, j, n, n) if (i != j && G[u][v] < ww[v])
fa[v] = u, ww[v] = G[u][v];
//REP(i, n) cout << ww[u] << " "; cout << endl;
bool found = 0; RST(bj), bj[0] = -1;
FOR(i, 1, _n) if (!bj[u]){
int x = u; do{bj[x] = u, x = fa[x];} while (!bj[x]);
if (bj[x] == u){
found = 1; do{bj[x] = nn, mst += ww[x = fa[x]];} while (bj[x] != nn);
Gen();
}
}
REP(i, _n) if (bj[u] < _nn) bj[u] = 0;
return found;
}
void Melt(){
REP(i, n) if (uu){ // Circle Canceling ...
REP(j, n) if (vv != uu){
if (vv) checkMin(G[uu][vv], G[u][v] - ww[v]);
else checkMin(G[uu][v], G[u][v]);
}
}
else {
REP(j, n) if (vv){
checkMin(G[u][vv], G[u][v] - ww[v]);
}
}
REP(i, n) if (uu) Del(i--);
}
#undef vv
#undef uu
#undef u
#undef v
void dfs(int u = 0){
bj[u] = 1; REP(v, n) if (G[u][v] != INF && !bj[v]) dfs(v);
}
int dMST(){
RST(bj); dfs();
FOR(i, 1, n) if (!bj[i]) return -1;
REP(i, n) Cur[i] = i; nn = n, mst = 0;
while (Find()) Melt();
FOR(i, 1, n) mst += ww[Cur[i]];
return mst;
}
class FoxTheLinguist {
public:
int minimalHours(int _n, vector <string> courseInfo) {
n = _n * 10 + 1; FLC(G, 0x3f); REP(i, _n){
G[0][i*10+1] = 0; REP_1(j, 9) checkMin(G[i*10+j+1][i*10+j], 0);
}
string s; s = accumulate(ALL(courseInfo), s);
REP(i, SZ(s)) if (s[i]=='-' || s[i]=='>' || s[i]==':') s[i] = ' ';
istringstream iss(s); for (string s1, s2, s3; iss >> s1 >> s2 >> s3;){
int u = (s1[0]-'A') * 10 + (s1[1]-'0') + 1;
int v = (s2[0]-'A') * 10 + (s2[1]-'0') + 1;
int w = s3[0] * 1000 + s3[1] * 100 + s3[2] * 10 + s3[3] - '0' * 1111;
checkMin(G[u][v], w);
}
return dMST();
}
};
// BEGIN CUT HERE
namespace moj_harness {
int run_test_case(int);
void run_test(int casenum = -1, bool quiet = false) {
if (casenum != -1) {
if (run_test_case(casenum) == -1 && !quiet) {
cerr << "Illegal input! Test case " << casenum << " does not exist." << endl;
}
return;
}
int correct = 0, total = 0;
for (int i=0;; ++i) {
int x = run_test_case(i);
if (x == -1) {
if (i >= 100) break;
continue;
}
correct += x;
++total;
}
if (total == 0) {
cerr << "No test cases run." << endl;
} else if (correct < total) {
cerr << "Some cases FAILED (passed " << correct << " of " << total << ")." << endl;
} else {
cerr << "All " << total << " tests passed!" << endl;
}
}
int verify_case(int casenum, const int &expected, const int &received, clock_t elapsed) {
cerr << "Example " << casenum << "... ";
string verdict;
vector<string> info;
char buf[100];
if (elapsed > CLOCKS_PER_SEC / 200) {
sprintf(buf, "time %.2fs", elapsed * (1.0/CLOCKS_PER_SEC));
info.push_back(buf);
}
if (expected == received) {
verdict = "PASSED";
} else {
verdict = "FAILED";
}
cerr << verdict;
if (!info.empty()) {
cerr << " (";
for (int i=0; i<(int)info.size(); ++i) {
if (i > 0) cerr << ", ";
cerr << info[i];
}
cerr << ")";
}
cerr << endl;
if (verdict == "FAILED") {
cerr << " Expected: " << expected << endl;
cerr << " Received: " << received << endl;
}
return verdict == "PASSED";
}
int run_test_case(int casenum) {
switch (casenum) {
case 0: {
int n = 2;
string courseInfo[] = {"B0->A9:1408 B0->B9:1307 A9->B7:0922 A0->A4:0946 B1", "->B5:1196 B3->A6:1240 A9->A5:1551 B2->A9:1293 A6->", "A6:1789 B3->B9:1617 A1->A8:1097 A1->B0:1022 A7->B2", ":1410 B1->A2:1382 B9->B2:1687 A0->A7:1341 A3->B3:1", "409 A3->B0:1165 A7->B2:1207 A9->B4:1311 B9->A3:126", "4 B8->B5:1133 B5->A4:1012 A8->B1:1673 A3->A7:1583 ", "B7->A7:1498 A8->B0:1540 A8->A6:0925 A3->B7:0961 A3", "->B8:1061 A3->A9:1027 A5->A9:1041 B5->A0:1224 B9->", "B3:1172 A9->B0:1097 B1->A9:1195 A1->B4:1122 A2->B6", ":1535 B8->A6:1448 A5->B9:1018 B9->B1:0959 B8->A9:1", "190 A5->B1:1463 B0->A0:1306 A9->B0:1496 A0->A5:103", "9 A7->A4:1652 A3->A3:1035 B4->A3:1504 A3->A5:1577 ", "A0->A6:1137 B0->B9:1604 A3->A2:1061 B5->B6:1744 A0", "->A5:1339 B4->B6:1276 B8->B9:0903 B3->A6:1117 A7->", "B0:0908 B1->A8:0898 B3->B9:1422 B1->B9:1147 A9->A7", ":1079 B4->A5:1305 A2->B5:1593 A7->B4:1449 A9->B0:1", "614 A1->B3:1643 A6->A0:1025 B8->A8:0932 B3->B1:174", "4 A9->B9:1737 B8->A7:1207 B4->B5:1707 A6->B0:1606 ", "A1->A1:1000 B6->A4:0947 A0->B8:1759 A2->B8:1752 B8", "->A1:1172 A2->A7:0968 B3->B8:1361 A7->A1:1541 A9->", "B4:0964 B0->B8:1749 A8->B1:1034 B1->A3:1500 A2->A6", ":1558 A6->B2:1343 B8->A5:1582 B7->B7:1260 B6->B8:0", "889 B8->B5:1789 A3->B2:1050 B0->B0:1612 A5->B7:134", "3 A3->A4:1600 A1->B8:1702 A4->A7:1507 A6->B6:1689 ", "B2->A8:1055 A5->A5:1550 B0->A3:1508 A3->A9:1795 B8", "->A2:1275 B7->B9:1346 B6->B7:1753 A5->B5:1332 A0->", "A8:1246 A5->B0:1474 B2->A8:0936 B1->B5:1169 A6->B9", ":1144 A7->B2:1632 B9->B8:1060 A2->A4:1328 A8->A2:1", "349 B4->B2:1403 A6->B6:1175 A3->B4:1141 B5->B0:108", "9 A3->A3:1682 B9->B6:1504 B5->A9:1683"};
int expected__ = 2426;
clock_t start__ = clock();
int received__ = FoxTheLinguist().minimalHours(n, vector <string>(courseInfo, courseInfo + (sizeof courseInfo / sizeof courseInfo[0])));
return verify_case(casenum, expected__, received__, clock()-start__);
}/*
case 1: {
int n = 2;
string courseInfo[] = {"A0->A9:1000 B0->B9:1000 A1->B9:0300 B1->A9:0200"};
int expected__ = 1200;
clock_t start__ = clock();
int received__ = FoxTheLinguist().minimalHours(n, vector <string>(courseInfo, courseInfo + (sizeof courseInfo / sizeof courseInfo[0])));
return verify_case(casenum, expected__, received__, clock()-start__);
}
case 2: {
int n = 3;
string courseInfo[] = {"C0->A6:00", "01 A3", "->B9:0001 A3->C6:000", "1",
" C3->A9:0001 A9->C9:0001 A0->A9:9999",
" B0->B9:9999 C0->C9:9999 A6->A9:9999"};
int expected__ = 5;
clock_t start__ = clock();
int received__ = FoxTheLinguist().minimalHours(n, vector <string>(courseInfo, courseInfo + (sizeof courseInfo / sizeof courseInfo[0])));
return verify_case(casenum, expected__, received__, clock()-start__);
}
case 3: {
int n = 4;
string courseInfo[] = {"A0->A6:6666 A0->A9:9999",
" B0->B6:6666 B0->B9:9999",
" C0->C6:6666 C0->C9:9999",
" D0->D6:6666 D0->D9:9999",
" A6->B6:0666 B6->C6:0666",
" C6->D6:0666 D6->A6:0666",
" A9->B9:0099 B9->C9:0099",
" C9->D9:0099 D9->A9:0099"};
int expected__ = 10296;
clock_t start__ = clock();
int received__ = FoxTheLinguist().minimalHours(n, vector <string>(courseInfo, courseInfo + (sizeof courseInfo / sizeof courseInfo[0])));
return verify_case(casenum, expected__, received__, clock()-start__);
}
case 4: {
int n = 1;
string courseInfo[] = {"A0->A9:9999 A0->A9:8888"};
int expected__ = 8888;
clock_t start__ = clock();
int received__ = FoxTheLinguist().minimalHours(n, vector <string>(courseInfo, courseInfo + (sizeof courseInfo / sizeof courseInfo[0])));
return verify_case(casenum, expected__, received__, clock()-start__);
}
case 5: {
int n = 1;
string courseInfo[] = {"A9->A9:0000",
" A9->A0:0000"};
int expected__ = -1;
clock_t start__ = clock();
int received__ = FoxTheLinguist().minimalHours(n, vector <string>(courseInfo, courseInfo + (sizeof courseInfo / sizeof courseInfo[0])));
return verify_case(casenum, expected__, received__, clock()-start__);
}
// custom cases
case 6: {
int n = ;
string courseInfo[] = ;
int expected__ = ;
clock_t start__ = clock();
int received__ = FoxTheLinguist().minimalHours(n, vector <string>(courseInfo, courseInfo + (sizeof courseInfo / sizeof courseInfo[0])));
return verify_case(casenum, expected__, received__, clock()-start__);
}*/
/* case 7: {
int n = ;
string courseInfo[] = ;
int expected__ = ;
clock_t start__ = clock();
int received__ = FoxTheLinguist().minimalHours(n, vector <string>(courseInfo, courseInfo + (sizeof courseInfo / sizeof courseInfo[0])));
return verify_case(casenum, expected__, received__, clock()-start__);
}*/
/* case 8: {
int n = ;
string courseInfo[] = ;
int expected__ = ;
clock_t start__ = clock();
int received__ = FoxTheLinguist().minimalHours(n, vector <string>(courseInfo, courseInfo + (sizeof courseInfo / sizeof courseInfo[0])));
return verify_case(casenum, expected__, received__, clock()-start__);
}*/
default:
return -1;
}
}
}
int main(int argc, char *argv[]) {
if (argc == 1) {
moj_harness::run_test();
} else {
for (int i=1; i<argc; ++i)
moj_harness::run_test(atoi(argv[i]));
}
}
// END CUT HERE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment