Vous êtes sur la page 1sur 5

#include <algorithm>

#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
typedef int int_type;
// -- utility --
// C-style loop
#define FOR(x, a, b) for(int_type x = static_cast<int_type>(a); x < static_cast<
int_type>(b); ++x)
// Ruby-style loop
#define TIMES(x, n) FOR(x, 0, n)
#define STEP(x, a, b, s) for(int_type x = static_cast<int_type>(a); s > 0 ? x <=
static_cast<int_type>(b) : x >= static_cast<int_type>(b); x += static_cast<int_
type>(s) )
#define UPTO(x, a, b) for(int_type x = static_cast<int_type>(a); x <= static_cas
t<int_type>(b); ++x)
#define DOWNTO(x, a, b) for(int_type x = static_cast<int_type>(a); x >= static_c
ast<int_type>(b); --x)
#define EACH(c, i) for(__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++
i)
#define ALL(cont, it, cond, ret) \
bool ret = true; EACH(cont, it) { if(!(cond)) {ret=false;break;} }
#define ANY(cont, it, cond, ret) \
bool ret = false; EACH(cont, it) { if(cond) {ret=true;break;} }
using namespace std;
// debug
// BEGIN CUT HERE
#define DUMP(x) std::cerr << #x << " = " << to_s(x) << std::endl;
template<typename T> string to_s(const T& v);
template<> string to_s(const string& v);
template<> string to_s(const bool& v);
template<typename T> string to_s(const vector<T>& v);
template<typename T> string to_s(const list<T>& v);
template<typename T> string to_s(const set<T>& v);
template<typename F, typename S> string to_s(const pair<F,S>& v);
template<typename K, typename V> string to_s(const map<K,V>& v);
// END CUT HERE
#ifndef DUMP
#define DUMP(x)
#endif
class CasketOfStar {
public:
typedef pair<int, int> pii;
map< pii, int> memo;
vector<int> w;
int go(int l, int r)
{
pii key = make_pair(l, r);
if(memo.find(key) != memo.end()) {
return memo[key];
}
if(l == r || l == r - 1) {
return 0;
}
if(l == r - 2) {
memo[key] = w[l] * w[r];
return memo[key];
}
int ret = 0;
FOR(k, l + 1, r) {
ret = max(ret, go(l, k) + go(k, r) + w[l] * w[r]);
}
memo[key] = ret;
return memo[key];
}
int maxEnergy(vector <int> weight)
{
memo.clear();
w = weight;
return go(0, weight.size() - 1);
}
// BEGIN CUT HERE
void debug()
{
}
/*
// PROBLEM STATEMENT
// The Casket of Star (sic) is a device in the Touhou universe. Its purpose is t
o generate energy rapidly.
Initially it contains n stars in a row. The stars are labeled 0 through n-1 from
the left to the right.
You are given a vector <int> weight, where weight[i] is the weight of star i.

The following operation can be repeatedly used to generate energy:


Choose a star x other than the very first star and the very last star.
The x-th star disappears.
This generates weight[x-1] * weight[x+1] units of energy.
We decrease n and relabel the stars 0 through n-1 from the left to the right.
Your task is to use the device to generate as many units of energy as possible.
Return the largest possible total amount of generated energy.
DEFINITION
Class:CasketOfStar
Method:maxEnergy
Parameters:vector <int>
Returns:int
Method signature:int maxEnergy(vector <int> weight)

CONSTRAINTS
-weight will contain between 3 and 50 elements, inclusive.
-Each element in weight will be between 1 and 1,000, inclusive.

EXAMPLES
0)
{1,2,3,4}
Returns: 12
We have only 2 choices:
Make the "2" disappear first, and "3" next. The total energy is 1*3 + 1*4 = 7.
Make the "3" disappear first, and "2" next. The total energy is 2*4 + 1*4 = 12.
So the answer is 12.
1)
{100,2,1,3,100}
Returns: 10400
We proceed as follows:
{100,2,1,3,100} => {100,1,3,100} => {100,3,100} => {100,100}
The total energy is 100*1 + 100*3 + 100*100 = 10400.
2)
{2,2,7,6,90,5,9}
Returns: 1818

3)
{477,744,474,777,447,747,777,474}
Returns: 2937051

4)
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
Returns: 13

*/
// END CUT HERE

// BEGIN CUT HERE


public:
void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0()
; if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2
)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -
1) || (Case == 4)) test_case_4(); }
private:
template <typename T> string print_array(const vector<T> &V) { ostringst
ream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); i
ter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(
); }
void verify_case(int Case, const int &Expected, const int &Received) { c
err << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED
" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected
<< '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
void test_case_0() { int Arr0[] = {1,2,3,4}; vector <int> Arg0(Arr0, Arr
0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 12; verify_case(0, Arg1, maxEn
ergy(Arg0)); }
void test_case_1() { int Arr0[] = {100,2,1,3,100}; vector <int> Arg0(Arr
0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 10400; verify_case(1, Ar
g1, maxEnergy(Arg0)); }
void test_case_2() { int Arr0[] = {2,2,7,6,90,5,9}; vector <int> Arg0(Ar
r0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 1818; verify_case(2, Ar
g1, maxEnergy(Arg0)); }
void test_case_3() { int Arr0[] = {477,744,474,777,447,747,777,474}; vec
tor <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 293705
1; verify_case(3, Arg1, maxEnergy(Arg0)); }
void test_case_4() { int Arr0[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}; vecto
r <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 13; veri
fy_case(4, Arg1, maxEnergy(Arg0)); }
// END CUT HERE
};
// BEGIN CUT HERE
int main(int argc, char *argv[])
{
CasketOfStar test;
if(argc == 1) {
test.run_test(-1);
}else {
std::string arg(argv[1]);
if(arg[0] != '-') {
test.run_test(arg[0] - '0');
}else {
test.debug();
}
}
return 0;
}
template<typename T> string to_s(const T& v) { ostringstream oss; oss << v; retu
rn oss.str(); }
template<> string to_s(const string& v) { ostringstream oss; oss << '"' << v <<
'"'; return oss.str(); }
template<> string to_s(const bool& v) { ostringstream oss; oss << ( v ? "true" :
"false") ; return oss.str(); }
template<typename T> string to_s(const vector<T>& v) { ostringstream oss; oss <<
"["; EACH(v,i) oss << to_s(*i) << ","; oss << "]"; return oss.str(); }
template<typename T> string to_s(const list<T>& v) { ostringstream oss; oss << "
("; EACH(v,i) oss << to_s(*i) << ","; oss << ")"; return oss.str(); }
template<typename T> string to_s(const set<T>& v) { ostringstream oss; oss << "{
"; EACH(v,i) oss << to_s(*i) << ","; oss << "}"; return oss.str(); }
template<typename F, typename S> string to_s(const pair<F,S>& v) { ostringstream
oss; oss << "<" << to_s(v.first) << " " << to_s(v.second) << ">"; return oss.st
r(); }
template<typename K, typename V> string to_s(const map<K,V>& v) { ostringstream
oss; oss << "{"; EACH(v,i) oss << to_s(i->first) << " => " << to_s(i->second) <<
","; oss << "}"; return oss.str(); }
// END CUT HERE

Vous aimerez peut-être aussi