许诺 • 29天前
#include <bits/stdc++.h>
using namespace std;
int main() {
int k, r, m, n;
cin >> k >> r >> m >> n;
vector<pair<int, int>> items;
for (int i = 0; i < k; ++i) {
string s;
cin >> s;
int a = count(s.begin(), s.end(), '0');
int b = s.size() - a;
if (a <= m && b <= n) {
items.push_back({a, b});
}
}
int item_count = items.size();
vector<vector<pair<int, int>>> dp(item_count + 1);
dp[0].emplace_back(0, 0);
int max_t = 0;
for (const auto& item : items) {
int a = item.first;
int b = item.second;
int prev_max_t = max_t;
for (int t = prev_max_t; t >= 0; --t) {
if (dp[t].empty()) continue;
vector<pair<int, int>> new_combos;
for (const auto& p : dp[t]) {
int new_a = p.first + a;
int new_b = p.second + b;
if (new_a > m || new_b > n) continue;
new_combos.emplace_back(new_a, new_b);
}
if (new_combos.empty()) continue;
vector<pair<int, int>>& target = dp[t + 1];
vector<pair<int, int>> merged = target;
merged.insert(merged.end(), new_combos.begin(), new_combos.end());
sort(merged.begin(), merged.end());
vector<pair<int, int>> filtered;
int min_b = INT_MAX;
for (const auto& p : merged) {
if (p.second < min_b) {
filtered.push_back(p);
min_b = p.second;
}
}
dp[t + 1] = filtered;
}
max_t = min(prev_max_t + 1, item_count);
}
for (int t = item_count; t >= 0; --t) {
for (const auto& p : dp[t]) {
if (p.first <= m && p.second <= n) {
cout << t << endl;
return 0;
}
}
}
cout << 0 << endl;
return 0;
}
评论:
请先登录,才能进行评论