Академический Документы
Профессиональный Документы
Культура Документы
#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.
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