Topic links: https://www.luogu.org/problem/P2672
This question is greedy, greedy thought is:
select \ (m \) maximum fatigue value families should be the greater of the following two scenarios :
- Program: Select \ (a [i] \) maximum \ (m \) families;
- Scheme 2: Select \ (a [i] \) maximum \ (m-1 \) families, and the remaining \ (n- (m-1) \) families in \ (2 \ times s [i ] + a [i] \) the largest of the family
Therefore, we can give (n-\) \ families according to \ (a [i] \) descending sort Venus.
Then open three arrays (array uses three to solving DP):
suma[i]
: Indicates \ (\ sum_ ^ {J} = {I}. 1 A [J] + 2 \ Times \ max_. 1} = {J ^ {I} (S [J]) \) , derived formula:suma[i] = suma[i-1] + a[i]
maxs[i]
: Indicates \ (\ max_ J = {I}. 1} ^ {S [J] \) , derived formula:maxs[i] = maxs[i-1] + s[i]
maxsa[i]
: Indicates \ (\ max_ I = {J}} ^ {n-(2 \ S Times [J] + A [J]) \) , derived formula:maxsa[i] = max(maxsa[i+1], 2 \times s[i] + a[i])
Then we want to \ (n \) select families inside \ (m \) maximum fatigue value is to visit families sum[m] + 2 * maxs[m]
and suma[m-1] + 2 * maxs[i]
greater value.
Codes are as follows:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100100;
int n, s[maxn], a[maxn], idx[maxn], suma[maxn], maxs[maxn], maxsa[maxn];
bool cmp(int i, int j) {
return a[i] > a[j];
}
int main() {
cin >> n;
for (int i = 1; i <= n; i ++) cin >> s[i];
for (int i = 1; i <= n; i ++) cin >> a[i];
for (int i = 1; i <= n; i ++) idx[i] = i;
sort(idx+1, idx+1+n, cmp);
for (int i = 1; i <= n; i ++)
suma[i] = suma[i-1] + a[idx[i]], maxs[i] = max(maxs[i-1], s[idx[i]]);
for (int i = n; i >= 1; i --)
maxsa[i] = max(maxsa[i+1], s[idx[i]] * 2 + a[idx[i]]);
for (int i = 1; i <= n; i ++)
cout << max(suma[i]+2*maxs[i], suma[i-1]+maxsa[i]) << endl;
return 0;
}