AtCoder Grand Contest 040 E

Portal: https://atcoder.jp/contests/agc040/tasks/agc040_e

sol:

Consider only $ $ $ operator or operator 2 $ 1
at only $ 1 to $ operator cases:
the number of $ 1 + $ do case if and only if there $ Ai> A_ {i + 1 } $
only $ operator $ 2 in the case of
+1 times do case if and only if $ Ai <A_ {i + 1 } $
found $ 1 $ operator operation does not affect the operation of the operator $ a $ 2
so we can do first consider $ operator 1 $ reunification $ operator 2 $ to do
so we find the original question about the abstract
is the sum of two sequences $ x $ and $ y $
which all $ x_i + y_i = A_i $
contribution is $ \ sum [xi> xi + 1] $ + $ \ sum [ yi <yi + 1] $
minimize this contribution
may DP $ $
$ dp_ {I, J} $ $ I $ denotes the number of considered before, the current fill $ $ $ j x_i $ circumstances of
the conventional transfer is a digital enumerating
then consider accelerating the shift
observed metastasis
was found for $ j $ circumstances increasing the value of $ dp $ must be no increase in the monotonous
we can find $ dp_ {i, 0 } <= dp_ {i, A_i } + 2 $

This places a $ 0 $, then, and fill up to $ A_i $ Difference $ 2 $ we can modify the $ A_i $ makes this must have been reached
so we find that
that is something we need to know is $ dp_ {i, 0} as well as how many of those $ $ pos $ is growing [this $ pos $ absolutely no more than two

Direct transfer to transfer $ O (1) $, the overall efficiency of $ O (n) $

Code:

#include <bits/stdc++.h>
using namespace std;
int N,a[200005];
int main(){
    scanf("%d",&N);
    for (int i=1;i<=N;i++)
        scanf("%d",&a[i]);
    int ans=0,now=0,v[2]{};
    for (int i=1;i<=N+1;i++){
        int p[2]{0,a[i]-now};
        if (p[0]<p[1]) swap(p[0],p[1]);
        int nex[3]{1000000000,1000000000,0};
        for (int j=0;j<2;j++)
            for (int k=0;k<2;k++)
                nex[j+k]=min(max(0,v[j]+p[k]),nex[j+k]);
        int d=nex[0]>a[i];
        for (int j=0;j<2;j++)
            v[j]=nex[j+d];
        years + = d;
        now=a[i];
    }
    cout << years;
    return  0 ;
}

 

Guess you like

Origin www.cnblogs.com/si--nian/p/11791273.html