# 4780. Virus research

Title description

Virus scientist Dr. Chen is leading her team to study the virus. She wants to study how to reduce the activity of the virus. The activity of the virus can be represented by an integer, but she does not know the specific activity.

She can perform $ m + 1 $ operations. For the first $ m $ operations, the first $ i $ operation is the cost of spending $ v_i $ and the virus activity is reduced by $ w_i $; Check the current state of the virus at no cost.

There are a total of $ n $ states for viruses. There are $ n + 1 $ incremental numbers $ a_0, a_1, a_2, ..., a_n $, where $ a_0 = 0 $; if the virus activity $ x $ satisfies $ a_ {i-1} <x \ le a_i $, then the virus is in the state $ i $. At the same time, ensure that the activity of the virus will not be greater than $ a_n $.

She can use each operation as many times as she wants, but she does not want the virus to be completely inactive. But if after using an operation, the virus activity $ \ le 0 $, then the study fails. And the activity of the virus is too high for research, only when the virus is in the state $ 1 $ is the most suitable for research.

Now, she only knows that the activity of the virus is a random integer of equal probability in $ [1, a_n] $. She wants to know what she expects to spend the least in the process of making the virus $ 1 $ while ensuring that the virus will not lose its activity completely.

It can be found that the answer multiplied by $ a_n $ must be an integer, the output answer multiplied by the value of $ a_n $.

If there is no guarantee that the virus will not completely lose activity, output $ -1 $.

data range

$ 1 \ le a_1 <a_2 <... <a_n \ le 2000, 1 \ le T \ le 10, 1 \ le v_i \ le 10 ^ 6 $。

answer

Consider $ \ text {dp} $: $ f [l] [r] $ means that the number in $ [l, r] $ reaches the expected sum of $ 1 $. Consider $ l, r $ if it is not in a state, it is a sum of several $ \ text {dp} $, if it is in a state, it will translate forward and add the cost of forward translation, this is $ O (n ^ 2m) $.

Considering reducing the number of states, we can assume that it has been translated to the position of the first split, then the middle process can be implemented with a backpack. Then we only need to remember the states of $ f [a_ {i-1} +1] [x] $ and $ f [x] [a_i] $, where $ x $ is in the $ i $ state. This efficiency is $ O (nm) $.

Code

 

#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int N=2005;
const LL G=1e18;
int T,n,m,z,a[N],b[N];
LL f[N][N],g[N],s[N];
LL F(int l,int r){
    if (f[l][r]!=G) return f[l][r];
    for (int x,y,i=1;i<l;i++)
        if (g[i]!=G && (b[l-i]!=b[r-i] || r-i<=a[1])){
            x=l-i;y=r-i;
            LL u=g[i]*(r-l+1)+F(x,a[b[x]])+F(a[b[y]-1]+1,y);
            if (b[x]+1<b[y]) u+=s[b[y]-1]-s[b[x]];
            f[l][r]=min(u,f[l][r]);
        }
    return f[l][r];
}
void work(){
    scanf("%d%d",&n,&m);
    memset(b,0,sizeof b);
    for (int i=1;i<=n;i++)
        scanf("%d",&a[i]),
        b[a[i]+1]++;z=a[n];b[0]=1;
    for (int i=1;i<=z;i++)
        b[i]+=b[i-1],g[i]=G;
    for (int x,y;m--;){
        scanf("%d%d",&x,&y);
        for (int i=y;i<=z;i++)
            g[i]=min(g[i],g[i-y]+x);
    }
    for (int i=1;i<=z;i++)
        for (int j=i;j<=z;j++) f[i][j]=G;
    for (int i=1;i<=a[1];i++)
        for (int j=i;j<=a[1];j++) f[i][j]=0;
    for (int x,y,i=2;i<=n;i++){
        x=a[i-1]+1;y=a[i];
        for (int r=x;r<=y;r++) f[x][r]=F(x,r);
        for (int l=x;l<y;l++) f[l][y]=F(l,y);
        if (f[x][y]==G){puts("-1");return;}
        s[i]=s[i-1]+f[x][y];
    }
    printf("%lld\n",s[n]);
}
int main(){for (cin>>T;T--;work());return 0;}

 

Guess you like

Origin www.cnblogs.com/xjqxjq/p/12727407.html