Bzoj 1597 Usaco Mar land purchase slope optimization DP

Source: Internet
Author: User

The main topic: give some planks, now to buy these planks. The purchase rule is that some planks can be bought together, and then the price is the maximum length multiplied by the maximum width. The minimum cost of purchasing all wood planks.


Idea: If a plank's length is smaller than a plank, and the width is smaller than a plank, then this plank can be ruled out. Sort all the planks according to the length of x, then remove the excluded planks, and the remaining planks are the ones with the X-values falling and the Y-values rising. In this case we buy a continuous section of the cost is X[J] * y[i], and then the DP equation is very simple: f[i] = f[j]-x[j + 1] * y[i].

Notice the data range, write a slope optimization on the water over.


CODE:

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAX 50010using namespace Std;    struct complex{int x, y;         BOOL blocked;    BOOL operator < (const Complex &a) const {return x > a.x;    } void Read () {scanf ("%d%d", &x,&y); }}src[max],point[max];         struct point{long long x, y; Point (Long long _ = 0,long Long __ = 0): X (_), Y (__) {}}q[max]; int Cnt,total;long long F[max];int front,tail;    Inline double getslope (const point &a,const point &b) {if (a.x = = b.x) return 1e15; Return (double) (A.Y-B.Y)/(a.x-b.x);}    inline void Insert (long long X,long long y) {point now (x, y);        while (Tail-front >= 2) if (Getslope (Q[tail],now) < Getslope (Q[tail-1],q[tail]))--tail;    else break; Q[++tail] = Now;} Inline point Getans (double slope) {while (Tail-front >= 2) if (Getslope (Q[front + 1],q[front + 2]) < slope ) ++fRont;    else break; return Q[front + 1];}    int main () {CIN >> cnt; for (int i = 1; I <= cnt; ++i) Src[i].    Read ();    Sort (src + 1,src + cnt + 1);    int now = 0;        for (int i = 1; I <= cnt, ++i) {if (src[i].y <= now) src[i].blocked = true;    else now = SRC[I].Y;    } for (int i = 1; I <= cnt; ++i) if (!src[i].blocked) point[++total] = Src[i];        for (int i = 1, i <= total; ++i) {Insert (-point[i].x,f[i-1]);        Point ans = Getans (POINT[I].Y);    F[i] = ans.y-point[i].y * ans.x;    } cout << f[total] << Endl; return 0;}


Bzoj 1597 Usaco 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.