【LOJ】#2080. 「JSOI2016」病毒感染
题解
那个限制表示一回头要治完前面的所有病人
我们处理一个g[i][j]表示治疗i到j的病人至少会死多少病人
\(g[i][j] = g[i + 1][j] + sum[i + 1,j] + min(sum[i + 1,j],(i - j) * 3 * a[i])\)
每次新加一个i,要么治疗i要么转一圈回来再治
\(f[i] = min(f[j] + g[j + 1][i] + ((i - j) * 4 - 2) * sum[i +1,n])\)
表示处理前i个需要的最小代价
代码
#include <bits/stdc++.h>
#define enter putchar('\n')
#define space putchar(' ')
#define pii pair<int,int>
#define fi first
#define se second
#define mp make_pair
#define MAXN 3005
#define mo 99994711
#define pb push_back
#define eps 1e-8
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef unsigned long long u64;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 - '0' + c;
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) out(x / 10);
putchar('0' + x % 10);
}
int N;
int64 a[MAXN],g[MAXN][MAXN],dp[MAXN],sum[MAXN];
void Solve() {
read(N);
for(int i = 1 ; i <= N ; ++i) read(a[i]);
for(int i = 1 ; i <= N ; ++i) sum[i] = sum[i - 1] + a[i];
for(int i = 1 ; i <= N ; ++i) g[i][i] = 0;
for(int d = 2 ; d <= N ; ++d) {
for(int i = 1 ; i <= N ; ++i) {
int j = i + d - 1;
if(j > N) break;
g[i][j] = g[i + 1][j] + sum[j] - sum[i] + min(sum[j] - sum[i],(j - i) * 3 * a[i]);
}
}
for(int i = 1 ; i <= N ; ++i) {
dp[i] = g[1][i] + (i * 4 - 2) * (sum[N] - sum[i]);
for(int j = 1 ; j < i ; ++j) {
dp[i] = min(dp[i],dp[j] + g[j + 1][i] + ((i - j) * 4 - 2) * (sum[N] - sum[i]));
}
}
out(dp[N]);
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}