"BZOJ1597" "Usaco2008 Mar" Land purchase slope optimization DP

Source: Internet
Author: User

The first step is to sequence the block, then remove the block contained by the other block, then the slope optimization dp

DP equation: f[i]=f[j]+a[i]*b[j+1];

Exactly what x ah y ah what in the code comment inside.


..... Tangled up dead. Start all kinds of WA and now discover:

Inline long long Xmul (point i,point j,point k) {(I.Y-J.Y) * (j.x-k.x)-(J.Y-K.Y) * (i.x-j.x);}
The code above can be compiled ... and unable to spit out the groove. Be sure to compile the file with the Terminal +wall command later.

Warning Day does not die, code day does not run!!!!

...... It's been a long, long time. Heartache Ah.


#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define N 50500# Define INF 0x3f3f3f3f/*f[j]=-a[i]*b[j+1]+f[i]y=f[j]x=b[j+1]k=-a[i] guarantee A does not drop, that is, K does not rise b=f[i] maintenance on the convex package */using namespace std; struct Point{long long x,y;int ID; Point (Long long _x,long long _y,int _id): X (_x), Y (_y), id (_id) {}point () {}}now,que[n];inline long long Xmul (Point I,point J , point K) {return (I.Y-J.Y) * (j.x-k.x)-(J.Y-K.Y) * (i.x-j.x);} int N,l,r;long long f[n];struct mrx{long long a,b;bool operator < (const MRX &c) const{if (A==C.A) return B<c.b;ret Urn a<c.a;}} Mrx[n];int Main () {//freopen ("test.in", "R", stdin),//freopen ("My.out", "w", stdout), int i,j,k;scanf ("%d", &n); for (i=1;i<=n;i++) scanf ("%d%d", &mrx[i].a,&mrx[i].b); sort (mrx+1,mrx+n+1); int Edl=0;long long maxx=0;for (i=n ; i;i--) {if (Mrx[i].b<=maxx) Edl++,mrx[i].a=mrx[i].b=inf;else maxx=mrx[i].b;} Sort (mrx+1,mrx+n+1); N-=edl;memset (F,0x3f,sizeof (f)); F[0]=0;que[0].x=mrx[1].b;for (i=1;i<=n;i++) {int K=-mrx[i] . A;//while (L< r&&que[l].y-que[l+1].y>= (que[l].x-que[l+1].x) *k) l++; Slope version while (l<r&&f[que[l].id]+mrx[i].a*mrx[que[l].id+1].b>=f[que[l+1].id]+mrx[i].a*mrx[que[l+1]. ID+1].B) l++; Function value version now=point (mrx[i+1].b,f[i]=f[que[l].id]+mrx[i].a*mrx[que[l].id+1].b,i); while (L<r&&xmul (now,que [r],que[r-1]) >=0) r--;//while (l<r&& (NOW.Y-QUE[R].Y) * (que[r].x-que[r-1].x) >= (Que[r].y-que[r-1]. Y) * (now.x-que[r].x))--r;que[++r]=now;} Cout<<f[n]<<endl;return 0;}



。。。。。

"BZOJ1597" "Usaco2008 Mar" Land purchase slope optimization DP

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.