

Hãy luôn nhớ cảm ơn và vote 5*
nếu câu trả lời hữu ích nhé!
#include <bits/stdc++.h>
#define ll long long
#define pii pair<ll,int>
using namespace std;
int main() {
int n, k; cin >> n >> k;
vector<ll> a(n);
for (int i = 0; i < n; ++i) cin >> a[i];
priority_queue<pii> q;
for (int i = 0; i < n; ++i) q.push({a[i], i});
vector<int> pick(n, 0);
vector<int> cnt(n - k + 1, 0);
ll sum = 0;
vector<int> res;
while (!q.empty()) {
auto [val, i] = q.top(); q.pop();
if (pick[i]) continue;
bool ok = true;
int start = max(0, i - k + 1);
int end = min(i, n - k);
for (int j = start; j <= end; ++j) {
if (cnt[j] == k - 1) {
ok = false;
break;
}
}
if (!ok) continue;
pick[i] = 1;
sum += val;
res.push_back(i + 1);
for (int j = start; j <= end; ++j)
cnt[j]++;
}
sort(res.begin(), res.end());
cout << res.size() << ' ' << sum << '\n';
for (int x : res) cout << x << ' ';
}
Hãy giúp mọi người biết câu trả lời này thế nào?
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL); int n, k;
std::cin >> n >> k;
std::vector<long long> a(n);
for (int i = 0; i < n; ++i) {
std::cin >> a[i];
}
if (n == 0)
{
std::cout << "0 0\n\n";
return 0;
}
if (n == 1)
{
std::cout << "1 " << a[0] << "\n";
std::cout << "1\n"; return 0;
}
std::vector<long long> dp(n);
dp[0] = a[0];
dp[1] = std::max(a[0], a[1]);
for (int i = 2; i < n; ++i)
{
dp[i] = std::max(dp[i-1], a[i] + dp[i-2]);
}
long long max_total_wine = dp[n-1];
std::vector<int> selected_bottles;
int current_idx = n - 1;
while (current_idx >= 0) {
if (current_idx == 0) {
if (dp[0] == a[0] && (n == 1 || dp[1] != a[0])) {
selected_bottles.push_back(1); } break; } if (dp[current_idx] == dp[current_idx - 1])
{
int current_idx--;
}
else {
selected_bottles.push_back(current_idx + 1);
current_idx -= 2; } }
std::reverse(selected_bottles.begin(), selected_bottles.end());
std: :cout << selected_bottles.size() << " " << max_total_wine << "\n";
for (int i = 0; i < selected_bottles.size(); ++i)
{
std: :cout << selected_bottles[i] << (i == selected_bottles.size() - 1 ? "" : " ");
}
std::cout << "\n";
return 0;
}
Hãy giúp mọi người biết câu trả lời này thế nào?
Bảng tin
163
768
150
nộp thử đi chứ tui thấy khôg giòn🐧
163
768
150
lqdoj.edu.vn/problem/bottles