- Author: zifeiy
- Tags: greedy
Topic links: https://www.luogu.org/problem/P1809
We assume that the first \ (i \) individual time-consuming and error is \ (t [i] \) , and \ (t [i] \) in ascending sorted (ie: \ (t [i] \ T Le [I +. 1] \) ), and the reset state \ (f [i] \) represents the former \ (I \) minimum cost of individual river.
Here we are greedy based on the idea: the priority order \ (t [i] \) large river.
Then:
When \ (i = 1 \) , only one person, so in this case \ (F [. 1] T = [. 1] \) ;
if \ (i = 2 \) , only two people, so in this case \ (F [2] = T [2] \) ;
if \ (i = 3 \) , we can choose the first somebody with the second individual in the past, and then come back for a third individual; or the first individual accompany 3 the individual's past, and then come back for a second person, in both cases satisfy the \ (F [. 3] = T [2] + T [. 1] + T [. 3] \) ;
if (i \ gt 3 \) \ when we bid farewell to the first \ (i \) Personal, there are two ways:
- One way: a first person i individuals and across the river, and then back to the first individual, when \ (F [i] = F [i-1] + T [i] + T [1] \) ;
- Second way: the first individual and the second individual across the river, and then back to the second individual, the first individual i-1 and i-th individual over the individual first come back (or: a first individual and the second individual river, 1 individuals come back, the first i-1 individual and multi-person i to the second individual back again), this time to change the state \ (F [i-2] \) , then \ (f [i] = f [ 2-I] + T [2] + T [2] + T [I] + T [. 1] \) .
所以,当 \(i \gt 3\) 时, \(f[i] = \max(f[i-1] + t[i] + t[1] , f[i-2] + t[2] + t[2] + t[i] + t[1])\) 。
In fact, we can see that for any a \ (f [i] \) , its state is the \ (f [j] (j \ gt i) \) of evolution, but we can by first solving \ (f [i] \) , derived \ (f [J] \) , we finally get the answer - \ (f [the n-] \) .
Codes are as follows:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int n, t[maxn], f[maxn];
int main() {
cin >> n;
for (int i = 1; i <= n; i ++) cin >> t[i];
sort(t+1, t+1+n);
for (int i = 1; i <= n; i ++) {
if (i == 1) f[i] = t[i];
else if (i == 2) f[i] = t[2];
else if (i == 3) f[i] = t[2] + t[1] + t[3];
else f[i] = min(f[i-1]+t[i]+t[1], f[i-2]+t[2]+t[2]+t[i]+t[1]);
}
cout << f[n] << endl;
return 0;
}