[Bzoj1010] [hnoi2008] toy movement gauge for toy packing _ slope Optimization

Source: Internet
Author: User

The solution to the first slope optimization question is still not very thorough.

I can recommend a blog here.

Http://blog.sina.com.cn/s/blog_5f5353cc0100jx41.html

Well written, but it may not be clear in some places.


Let me explain my understanding of slope optimization:

It is an optimization that finds a dynamic planning question, TLE, and then it happens to be in line with some crazy nature.

In essence, it is limited to create a property, so that the dynamic equation can satisfy certain evil monotonicity and thus be greatly optimized.

How can this monotonicity be achieved?

That is to say, let's get the original transfer status and make a conversion, for example, split the square. There are two types of transfer: J-> I and K-> I, we make it have a size relationship DP [J-> I] <= DP [k-> I], and finally converts it to a formula with J and K on the left, there is an I-only expression on the right side, so that two transfer directions can be judged at high speed.Which one is strong?.

In terms of ideology, WYF gave me a well-understood version as follows.


It is it! We need to maintain a Convex Shell. When I want to join the priority queue to judge the slope of J-> I and the slope of K-> I, then K is obviously going out, then Judge H-> I. Well, I stayed. That's it.


Well, let's talk about some specific things related to this question.

First, define f [I] = sum [I] + I. This sum [I] is the prefix and.

Then, when G (j, k)/S (j, k) <= I, j is better than K.

#define G(x,y) (dp[x]-dp[y]+(f[x]+c)*(f[x]+c)-(f[y]+c)*(f[y]+c))#define S(x,y) (2*(f[x]-f[y]))


Code:

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define N 55000#define G(x,y) (dp[x]-dp[y]+(f[x]+c)*(f[x]+c)-(f[y]+c)*(f[y]+c))#define S(x,y) (2*(f[x]-f[y]))using namespace std;long long c,sum[N],f[N],dp[N];int n,q[N];int main(){//freopen("test.in","r",stdin);scanf("%d%lld",&n,&c);c++;for(int i=1;i<=n;i++){scanf("%lld",&sum[i]);sum[i]+=sum[i-1];f[i]=sum[i]+i;}int head=1,tail=1;q[1]=0;for(int i=1;i<=n;i++){while(head<tail&&G(q[head+1],q[head])<=S(q[head+1],q[head])*f[i]) head++;dp[i]=dp[q[head]]+(f[i]-f[q[head]]-c)*(f[i]-f[q[head]]-c);while(head<tail&&G(i,q[tail])*S(q[tail],q[tail-1])<=G(q[tail],q[tail-1])*S(i,q[tail]))tail--;q[++tail]=i;}printf("%lld\n",dp[n]);return 0;}



[Bzoj1010] [hnoi2008] toy movement gauge for toy packing _ slope Optimization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.