Bzoj 1597: [Usaco2008 Mar] Land purchase (DP + slope optimization)

Source: Internet
Author: User

Since each piece is to be bought, then a piece of land is contained by another piece can not be considered. First by the long sort, remove the land not considered, the remaining land length x increment, width y decrement

DP (v) = min{dp (p) +xv*yp+1}

Assuming that DP (v) is transferred by the I transfer ratio by J (I>J), then

DP (i) +xv*yi+1 < DP (j) +xv*yj+1

Simplification (DP (i)-DP (J))/(yi+1-yj+1) >-XV

Then on the slope optimization, the monotone queue maintains a convex function

-----------------------------------------------------------------------------

#include <bits/stdc++.h>using namespace std; typedef long Long ll;const int MAXN = 50009;struct O {int x, y;inline void Read () {scanf ("%d%d", &x, &y);}BOOL Operator < (const O &o) Const {return x < o.x | | (x = = o.x && y < o.y);}} A[MAXN];int N = 0, R[MAXN], Q[MAXN];ll DP[MAXN];void init () {int n; scanf ("%d", &n);for (int i = 0; i < n; i++) a[i]. Read ();sort (A, a + N);int mx = 0;for (int i = n; i--;) {if (a[i].y > mx) r[++n] = i;mx = max (mx, a[i].y);}for (int L = 1, R = N; L < R; l++, r--) swap (R[l], r[r]);}inline double slope (int a, int b) {return (double) (Dp[b]-dp[a])/(a[r[b + 1]].y-a[r[a + 1]].y);} void Work () {int QH = 0, qt = 0;dp[q[qt++] = 0] = 0;for (int i = 1; I <= N; i++) {while (Qt-qh > 1 && slope (Q[QH], Q[QH + 1]) >-a[r[i]].x) qh++;Dp[i] = Dp[q[qh]] + LL (a[r[i]].x) * A[R[Q[QH] + 1]].y;while (Qt-qh > 1 && slope (q[qt-2], q[qt-1]) < slope (Q[qt-1], i)) qt--;q[qt++] = i;}printf ("%lld\n", Dp[n]);}int main () {init ();Work ();return 0;}

-----------------------------------------------------------------------------

1597: [Usaco2008 Mar] Land purchase time limit: ten Sec Memory Limit: 162 MB
Submit: 2398 Solved: 869
[Submit] [Status] [Discuss] Description

Farmer John prepares to expand his farm, and he is considering N (1 <= n <= 50,000) blocks of rectangular land. The length and width of each land is satisfied (1 <= wide <= 1,000,000; 1 <= long <= 1,000,000). The price of each piece of land is its area, but FJ can buy much faster land at the same time. The price of these lands is their largest length multiplied by their maximum width, but the length of the land cannot be exchanged. If FJ buys a 3x5 land and a 5x3, he needs to pay 5x5=25. FJ wanted to buy all the land, but he found that grouping to buy the land would save money. He needs you to help him find the minimum funds.

Input

* Line 1th: one number: N

* 2nd. N+1 Line: Line i+1 contains two numbers, respectively, the length and width of the land of Block I

Output

* First line: The minimum feasible cost.

Sample Input4
100 1
15 15
20 5
1 100

Input explanation:

There are 4 pieces of land.

Sample Output500

HINT

FJ 3 groups to buy these lands: The first group: 100x1, the second group of 1x100, the third group of 20x5 and 15x15 plot. The prices for each group were 100,100,300, a total of 500.

Source

Gold

Bzoj 1597: [Usaco2008 Mar] Land purchase (DP + 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.