1010知恵の主導
http://acm.hdu.edu.cn/showproblem.php?pid=6772
題名
n項目、多くてもkタイプ、各項目の1つだけを選択できます。数式の最大値を見つけます。
アイデア
複雑さを研究すれば、爆発的な検索が可能であることがわかります。私のラムダでもそれを通過できます。
しかし、dfsを検索する場合、型が離散的であるため、この質問には再帰的なスタックの問題が発生する可能性があります。無効な型をスキップしないと、はるかに遅くなります。(少なくともhduのパフォーマンスでは)
複雑さは最初のページに達する可能性があり、いくつかのランダム最適化アルゴリズムが使用されていると言われています。
コード
// 这份代码需要C++14特性,编译器 MinGW 6.0+
// 不过你懂的话,可以用全局变量+结构体来实现等效的代码。
using item = tuple<int, int, int, int, int>;
void solve(int kaseId = -1) {
int n, k;
ll ans = 0;
cin >> n >> k;
vector<item> items(n);
vector<pii> itemq;
itemq.reserve(n);
for (auto &it :items) {
int t, a, b, c, d;
cin >> t >> a >> b >> c >> d;
it = tie(t, a, b, c, d);
}
sort(items.begin(), items.end());
int las = 0;
int cnt = 0;
for (auto &it : items) {
if (get<0>(it) != get<0>(items[las])) {
itemq.emplace_back(las, cnt);
cnt = 1;
las = int(&it - &items.front());
} else {
cnt++;
}
}
itemq.emplace_back(las, cnt);
auto DFS = [&ans, &q = itemq, &v = items](auto self,
int cur, int suma, int sumb, int sumc, int sumd) -> void {
if (cur >= q.size()) {
ans = max(ans, suma * 1ll * sumb * sumc * sumd);
return;
}
for (int l = 0; l < q[cur].second; ++l) {
int i = q[cur].first + l;
int t, a, b, c, d;
tie(t, a, b, c, d) = v[i];
self(self, cur + 1,
suma + a, sumb + b, sumc + c, sumd + d);
}
};
DFS(DFS, 0, 100, 100, 100, 100);
cout << ans << endl;
}