Problem: partitioning (DP & greedy)

topic

Portal

Thinking

  • Part 1 min (36)

    First, it is easy to think of a the DP
    \ (dp_ {i, j} \) indicates a precursor to No. i node is j
    transfer is also very simple \ (dp_ {i, j} = min_ {k = 1} ^ {j-1} dp [k] [j-1]
    + (s [i] -s [j-1]) ^ 2 \) of course be transferred to ensure

  • Part 2 min (64)

    The DP is optimized for direct

    It is easy to find for a j, we just need to find \ (min_ {i = 1} ^ {j-1} dp [i] [j-1] \) on the line

    Minn using this value can be transferred to the back

  • Correct

    In the process of Debug,

    We found a pattern \ (dp_ {i, j} <= dp_ {i, k} (k <= j) \)

    Of course, this conclusion does not prove the author

    But the emotional understanding

    The last paragraph of the smallest,

    Means in front of each section as small as possible,

    In front of each section as small as possible means that the smaller the square of each segment

    That is, if \ (x_1 + x_2 == y_1 + y_2 + y_3 \)

    The \ (x_1, x_2 \) as equal as possible, \ (Y_1, y_2, Y_3 \) as equal as possible

    \(x_1*x_1+x_2*x_2>=y_1*y_1+y_2*y_2+y_3*y_3\)

    That is, for a sequence,

    Optimum length of the last segment of the entire sequence must be the smallest of all solutions of a length

    Consider how the $ dp_ {i, j} $ will be better

    First, it must be the closest a j j i's,

    Then we consider how to quickly find \ (min_ {k = 1} ^ {j-1} dp_ {j-1, k} \)

    Closest of k j-1 must also be of a k

    We can redefine DP

    \ (dp_i \) who said i predecessor node node number is

    Consider how transfer

    dp value from the node 1 to the node number i can be transferred to the maximum number of node i

    So can the (dp_i \) \ What does the law in line with node metastasis

    Transfer node set is k

    \(s_k-s_{dp_k}<=s_i-s_k\)

    \(2*s_k-s_{dp_k}<=s_i\)

    Through this equation can easily think of monotonous queue optimization

    By Big Ben at the time of the examination room has been boasted by optimizing A slope of this question, it seems self-test is 0

    Code

    Because I was too lazy

    In fact, too much food

    Directly with the __int128

    Reminder, CSP can not __int128

    And this question a little bit of regular cards and card space

    #include<iostream>
    #include<cstdio>
    #include<queue>
    using namespace std;
    const int mod=(1ll<<30);
    void read(__int128 &x)
    {
        x=0;
        int f=1;
        char c=getchar();
        while('0'>c||c>'9')
        {
            if(c=='-')
                f=-1;
            c=getchar();
        }
        while('0'<=c&&c<='9')
        {
            x=(x<<3)+(x<<1)+c-'0';
            c=getchar();
        }
        x*=f;
    }
    void read(long long &x)
    {
        x=0;
        int f=1;
        char c=getchar();
        while('0'>c||c>'9')
        {
            if(c=='-')
                f=-1;
            c=getchar();
        }
        while('0'<=c&&c<='9')
        {
            x=(x<<3)+(x<<1)+c-'0';
            c=getchar();
        }
        x*=f;
    }
    void read(int &x)
    {
        x=0;
        int f=1;
        char c=getchar();
        while('0'>c||c>'9')
        {
            if(c=='-')
                f=-1;
            c=getchar();
        }
        while('0'<=c&&c<='9')
        {
            x=(x<<3)+(x<<1)+c-'0';
            c=getchar();
        }
        x*=f;
    }
    void write(__int128 x)
    {
        if(x>9)
            write(x/10);
        putchar(x%10+'0');
    }
    struct node
    {
        int p;
        int l;
        int r;
    }fff[100005];
    int n,ty;
    int hea=1;
    int tai=1;
    int now;
    int q[40000005];
    int dp[40000005];
    long long s[40000005];
    int a[40000005];
    int b[40000005];
    __int128 ans;
    __int128 basic=1;
    long long x,y,z,m;
    long long solve_mon(int i)
    {
        return 2*s[i]-s[dp[i]];
    }
    int main()
    {
        read(n);
        read(ty);
        if(ty==0)
        {
            for(int i=1;i<=n;i++)
            {
                read(a[i]);
                s[i]=s[i-1]+a[i];
            }
        }
        else
        {
            read(x);
            read(y);
            read(z);
            read(b[1]);
            read(b[2]);
            read(m);
            for(int i=1;i<=m;i++)
            {
                read(fff[i].p);
                read(fff[i].l);
                read(fff[i].r);
            }
            for(int i=3;i<=n;i++)
                b[i]=(x*b[i-1]%mod+y*b[i-2]%mod+z)%mod;
            for(int j=1;j<=m;j++)
                for(int i=fff[j-1].p+1;i<=fff[j].p;i++)
                {
                    a[i]=(b[i]%(fff[j].r-fff[j].l+1))+fff[j].l;
                    s[i]=s[i-1]+a[i];
                }
        }
        for(int i=1;i<=n;i++)
        {
            while(hea<tai&&solve_mon(q[hea+1])<=s[i])
                hea++;
            dp[i]=q[hea];
            while(hea<tai&&solve_mon(q[tai])>=solve_mon(i))
                tai--;
            q[++tai]=i;
        }
        now=n;
        while(now)
        {
            ans+=basic*(s[now]-s[dp[now]])*((s[now]-s[dp[now]]));
            now=dp[now];
        }
        write(ans);
        return 0;
    }

Guess you like

Origin www.cnblogs.com/loney-s/p/11908364.html