fd58d23e5f 2012-12-12 kinaba: #include <iostream> fd58d23e5f 2012-12-12 kinaba: #include <sstream> fd58d23e5f 2012-12-12 kinaba: #include <iomanip> fd58d23e5f 2012-12-12 kinaba: #include <vector> fd58d23e5f 2012-12-12 kinaba: #include <string> fd58d23e5f 2012-12-12 kinaba: #include <map> fd58d23e5f 2012-12-12 kinaba: #include <set> fd58d23e5f 2012-12-12 kinaba: #include <algorithm> fd58d23e5f 2012-12-12 kinaba: #include <numeric> fd58d23e5f 2012-12-12 kinaba: #include <iterator> fd58d23e5f 2012-12-12 kinaba: #include <functional> fd58d23e5f 2012-12-12 kinaba: #include <complex> fd58d23e5f 2012-12-12 kinaba: #include <queue> fd58d23e5f 2012-12-12 kinaba: #include <stack> fd58d23e5f 2012-12-12 kinaba: #include <cmath> fd58d23e5f 2012-12-12 kinaba: #include <cassert> fd58d23e5f 2012-12-12 kinaba: using namespace std; fd58d23e5f 2012-12-12 kinaba: typedef long long LL; fd58d23e5f 2012-12-12 kinaba: typedef long double LD; fd58d23e5f 2012-12-12 kinaba: typedef complex<LD> CMP; fd58d23e5f 2012-12-12 kinaba: fd58d23e5f 2012-12-12 kinaba: template<typename T> fd58d23e5f 2012-12-12 kinaba: struct DP2 fd58d23e5f 2012-12-12 kinaba: { fd58d23e5f 2012-12-12 kinaba: const int N1, N2; fd58d23e5f 2012-12-12 kinaba: vector<T> data; fd58d23e5f 2012-12-12 kinaba: DP2(int N1, int N2, const T& t = T()) fd58d23e5f 2012-12-12 kinaba: : N1(N1), N2(N2), data(N1*N2, t) { assert(data.size()*sizeof(T)<(1<<26)); } fd58d23e5f 2012-12-12 kinaba: T& operator()(int i1, int i2) fd58d23e5f 2012-12-12 kinaba: { return data[ (i1*N2)+i2 ]; } fd58d23e5f 2012-12-12 kinaba: void swap(DP2& rhs) fd58d23e5f 2012-12-12 kinaba: { data.swap(rhs.data); } fd58d23e5f 2012-12-12 kinaba: }; fd58d23e5f 2012-12-12 kinaba: fd58d23e5f 2012-12-12 kinaba: class SpellCards { public: fd58d23e5f 2012-12-12 kinaba: int maxDamage(vector <int> level, vector <int> damage) fd58d23e5f 2012-12-12 kinaba: { fd58d23e5f 2012-12-12 kinaba: static const int IMPOSSIBLE = -99999999; fd58d23e5f 2012-12-12 kinaba: fd58d23e5f 2012-12-12 kinaba: int N = level.size(); fd58d23e5f 2012-12-12 kinaba: DP2<int> dp(N, N+1, IMPOSSIBLE); fd58d23e5f 2012-12-12 kinaba: for(int w=1; w<=N; ++w) fd58d23e5f 2012-12-12 kinaba: for(int s=0; s<N; ++s) { fd58d23e5f 2012-12-12 kinaba: int L = level[s]; fd58d23e5f 2012-12-12 kinaba: int D = damage[s]; fd58d23e5f 2012-12-12 kinaba: fd58d23e5f 2012-12-12 kinaba: DP2<int> ip(L+1, w, IMPOSSIBLE); fd58d23e5f 2012-12-12 kinaba: ip(1,0) = D; fd58d23e5f 2012-12-12 kinaba: for(int c=1; c<=L; ++c) fd58d23e5f 2012-12-12 kinaba: for(int k=1; k<w; ++k) { fd58d23e5f 2012-12-12 kinaba: int sk = (s+k) % N; fd58d23e5f 2012-12-12 kinaba: fd58d23e5f 2012-12-12 kinaba: int v = ip(c-1,k-1); fd58d23e5f 2012-12-12 kinaba: for(int p=1; p<=k; ++p) { fd58d23e5f 2012-12-12 kinaba: int sp = (s+p) % N; fd58d23e5f 2012-12-12 kinaba: v = max(v, ip(c,p-1) + dp(sp, k-p+1)); fd58d23e5f 2012-12-12 kinaba: } fd58d23e5f 2012-12-12 kinaba: ip(c,k) = v; fd58d23e5f 2012-12-12 kinaba: } fd58d23e5f 2012-12-12 kinaba: fd58d23e5f 2012-12-12 kinaba: dp(s,w) = ip(L, w-1); fd58d23e5f 2012-12-12 kinaba: } fd58d23e5f 2012-12-12 kinaba: fd58d23e5f 2012-12-12 kinaba: int theBest = 0; fd58d23e5f 2012-12-12 kinaba: for(int s=0; s<N; ++s) fd58d23e5f 2012-12-12 kinaba: { fd58d23e5f 2012-12-12 kinaba: vector<int> best(N+1); fd58d23e5f 2012-12-12 kinaba: for(int k=0; k<N; ++k) { fd58d23e5f 2012-12-12 kinaba: best[k+1] = best[k]; fd58d23e5f 2012-12-12 kinaba: for(int p=0; p<=k; ++p) fd58d23e5f 2012-12-12 kinaba: best[k+1] = max(best[k+1], best[p] + dp((s+p)%N, k-p+1)); fd58d23e5f 2012-12-12 kinaba: } fd58d23e5f 2012-12-12 kinaba: theBest = max(theBest, best[N]); fd58d23e5f 2012-12-12 kinaba: } fd58d23e5f 2012-12-12 kinaba: return theBest; fd58d23e5f 2012-12-12 kinaba: } fd58d23e5f 2012-12-12 kinaba: }; fd58d23e5f 2012-12-12 kinaba: fd58d23e5f 2012-12-12 kinaba: // BEGIN CUT HERE fd58d23e5f 2012-12-12 kinaba: #include <ctime> fd58d23e5f 2012-12-12 kinaba: double start_time; string timer() fd58d23e5f 2012-12-12 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } fd58d23e5f 2012-12-12 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) fd58d23e5f 2012-12-12 kinaba: { os << "{ "; fd58d23e5f 2012-12-12 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) fd58d23e5f 2012-12-12 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } fd58d23e5f 2012-12-12 kinaba: void verify_case(const int& Expected, const int& Received) { fd58d23e5f 2012-12-12 kinaba: bool ok = (Expected == Received); fd58d23e5f 2012-12-12 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; fd58d23e5f 2012-12-12 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } fd58d23e5f 2012-12-12 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); fd58d23e5f 2012-12-12 kinaba: #define END verify_case(_, SpellCards().maxDamage(level, damage));} fd58d23e5f 2012-12-12 kinaba: int main(){ fd58d23e5f 2012-12-12 kinaba: fd58d23e5f 2012-12-12 kinaba: CASE(0) fd58d23e5f 2012-12-12 kinaba: int level_[] = {1,1,1}; fd58d23e5f 2012-12-12 kinaba: vector <int> level(level_, level_+sizeof(level_)/sizeof(*level_)); fd58d23e5f 2012-12-12 kinaba: int damage_[] = {10,20,30}; fd58d23e5f 2012-12-12 kinaba: vector <int> damage(damage_, damage_+sizeof(damage_)/sizeof(*damage_)); fd58d23e5f 2012-12-12 kinaba: int _ = 60; fd58d23e5f 2012-12-12 kinaba: END fd58d23e5f 2012-12-12 kinaba: CASE(1) fd58d23e5f 2012-12-12 kinaba: int level_[] = {3,3,3}; fd58d23e5f 2012-12-12 kinaba: vector <int> level(level_, level_+sizeof(level_)/sizeof(*level_)); fd58d23e5f 2012-12-12 kinaba: int damage_[] = {10,20,30}; fd58d23e5f 2012-12-12 kinaba: vector <int> damage(damage_, damage_+sizeof(damage_)/sizeof(*damage_)); fd58d23e5f 2012-12-12 kinaba: int _ = 30; fd58d23e5f 2012-12-12 kinaba: END fd58d23e5f 2012-12-12 kinaba: CASE(2) fd58d23e5f 2012-12-12 kinaba: int level_[] = {4,4,4}; fd58d23e5f 2012-12-12 kinaba: vector <int> level(level_, level_+sizeof(level_)/sizeof(*level_)); fd58d23e5f 2012-12-12 kinaba: int damage_[] = {10,20,30}; fd58d23e5f 2012-12-12 kinaba: vector <int> damage(damage_, damage_+sizeof(damage_)/sizeof(*damage_)); fd58d23e5f 2012-12-12 kinaba: int _ = 0; fd58d23e5f 2012-12-12 kinaba: END fd58d23e5f 2012-12-12 kinaba: CASE(3) fd58d23e5f 2012-12-12 kinaba: int level_[] = {50,1,50,1,50}; fd58d23e5f 2012-12-12 kinaba: vector <int> level(level_, level_+sizeof(level_)/sizeof(*level_)); fd58d23e5f 2012-12-12 kinaba: int damage_[] = {10,20,30,40,50}; fd58d23e5f 2012-12-12 kinaba: vector <int> damage(damage_, damage_+sizeof(damage_)/sizeof(*damage_)); fd58d23e5f 2012-12-12 kinaba: int _ = 60; fd58d23e5f 2012-12-12 kinaba: END fd58d23e5f 2012-12-12 kinaba: CASE(4) fd58d23e5f 2012-12-12 kinaba: int level_[] = {2,1,1}; fd58d23e5f 2012-12-12 kinaba: vector <int> level(level_, level_+sizeof(level_)/sizeof(*level_)); fd58d23e5f 2012-12-12 kinaba: int damage_[] = {40,40,10}; fd58d23e5f 2012-12-12 kinaba: vector <int> damage(damage_, damage_+sizeof(damage_)/sizeof(*damage_)); fd58d23e5f 2012-12-12 kinaba: int _ = 80; fd58d23e5f 2012-12-12 kinaba: END fd58d23e5f 2012-12-12 kinaba: CASE(5) fd58d23e5f 2012-12-12 kinaba: int level_[] = {1,2,1,1,3,2,1}; fd58d23e5f 2012-12-12 kinaba: vector <int> level(level_, level_+sizeof(level_)/sizeof(*level_)); fd58d23e5f 2012-12-12 kinaba: int damage_[] = {10,40,10,10,90,40,10}; fd58d23e5f 2012-12-12 kinaba: vector <int> damage(damage_, damage_+sizeof(damage_)/sizeof(*damage_)); fd58d23e5f 2012-12-12 kinaba: int _ = 170; fd58d23e5f 2012-12-12 kinaba: END fd58d23e5f 2012-12-12 kinaba: CASE(6) fd58d23e5f 2012-12-12 kinaba: int level_[] = {1,2,2,3,1,4,2}; fd58d23e5f 2012-12-12 kinaba: vector <int> level(level_, level_+sizeof(level_)/sizeof(*level_)); fd58d23e5f 2012-12-12 kinaba: int damage_[] = {113,253,523,941,250,534,454}; fd58d23e5f 2012-12-12 kinaba: vector <int> damage(damage_, damage_+sizeof(damage_)/sizeof(*damage_)); fd58d23e5f 2012-12-12 kinaba: int _ = 1918; fd58d23e5f 2012-12-12 kinaba: END fd58d23e5f 2012-12-12 kinaba: CASE(7) fd58d23e5f 2012-12-12 kinaba: int level_[] = {4,8,50,24,4,32,6,39,3,2,4,4,39,39,10,3,42,36,38,18,6,18,41,15,28,40,45,26,40,30,25,46,18,35,29,25,48,10,18,25,46,33,30,26,36,8,20,35,28,29}; fd58d23e5f 2012-12-12 kinaba: vector <int> level(level_, level_+sizeof(level_)/sizeof(*level_)); fd58d23e5f 2012-12-12 kinaba: int damage_[] = {8577,2855,8317,7176,295,7028,5317,7099,4642,29,2481,7249,5387,6266,6241,4550,9178,2375,1037,4438,4189,5886,5285,3583,2874,8484,4725,2096,7560,4057,988,5914,3460,1305,6223,2305,6229,2719,2404,6454,278,5242,2948,9607,1302,4263,3366,4925,8381,3692}; fd58d23e5f 2012-12-12 kinaba: vector <int> damage(damage_, damage_+sizeof(damage_)/sizeof(*damage_)); fd58d23e5f 2012-12-12 kinaba: int _ = -2; fd58d23e5f 2012-12-12 kinaba: END fd58d23e5f 2012-12-12 kinaba: CASE(8) fd58d23e5f 2012-12-12 kinaba: int level_[] = {25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25}; fd58d23e5f 2012-12-12 kinaba: vector <int> level(level_, level_+sizeof(level_)/sizeof(*level_)); fd58d23e5f 2012-12-12 kinaba: int damage_[] = {8577,2855,8317,7176,295,7028,5317,7099,4642,29,2481,7249,5387,6266,6241,4550,9178,2375,1037,4438,4189,5886,5285,3583,2874,8484,4725,2096,7560,4057,988,5914,3460,1305,6223,2305,6229,2719,2404,6454,278,5242,2948,9607,1302,4263,3366,4925,8381,3692}; fd58d23e5f 2012-12-12 kinaba: vector <int> damage(damage_, damage_+sizeof(damage_)/sizeof(*damage_)); fd58d23e5f 2012-12-12 kinaba: int _ = -2; fd58d23e5f 2012-12-12 kinaba: END fd58d23e5f 2012-12-12 kinaba: } fd58d23e5f 2012-12-12 kinaba: // END CUT HERE