# BZOJ1597 土地购买 【dp + 斜率优化】

## 1597: [Usaco2008 Mar]土地购买

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 5466  Solved: 2035
[Submit][Status][Discuss]

## Description

= 1,000,000; 1 <= 长 <= 1,000,000). 每块土地的价格是它的面积,但FJ可以同时购买多快土地. 这些土地的价

## Input

* 第1行: 一个数: N
* 第2..N+1行: 第i+1行包含两个数,分别为第i块土地的长和宽

* 第一行: 最小的可行费用.

4
100 1
15 15
20 5
1 100

## Sample Output

500
FJ分3组买这些土地:

【调了一个晚上QAQ我还是太弱了】

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define eps 1e-9
#define LL long long int
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define fo(i,x,y) for (int i = (x); i <= (y); i++)
#define Redge(u) for (int k = head[u]; k != -1; k = edge[k].next)
using namespace std;
const int maxn = 50005,maxm = 100005,INF = 1000000000;
LL out = 0,flag = 1;char c = getchar();
while (c < 48 || c > 57) {if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57) {out = out * 10 + c - 48; c = getchar();}
return out * flag;
}
LL N,n;
LL f[maxn],q[maxn],l,r,X[maxn],Y[maxn];
struct node{LL x,y;}p[maxn];
inline bool operator < (const node& a,const node& b){
return a.x == b.x ? a.y < b.y : a.x < b.x;
}
inline double slope(LL u,LL v){
return (double)(f[u] - f[v]) / (Y[u + 1] - Y[v + 1]);
}
inline LL getf(LL i,LL j){
return f[j] + Y[j + 1] * X[i];
}
int main()
{
sort(p + 1,p + 1 + N); Y[n] = INF;
for (int i = 1; i <= N; i++){
while (n && Y[n] <= p[i].y) n--;
X[++n] = p[i].x; Y[n] = p[i].y;
}
l = r = 0;
for (int i = 1; i <= n; i++){
while (l < r && slope(q[l],q[l + 1]) > -X[i]) l++;
f[i] = getf(i,q[l]);
while (l < r && slope(i,q[r]) > slope(q[r],q[r - 1])) r--;
q[++r] = i;
}
cout<<f[n]<<endl;
return 0;
}


posted @ 2017-11-27 20:06  Mychael  阅读(124)  评论(0编辑  收藏  举报