[USACO08MAR]土地征用Land Acquisition

传送门

这个题直接上斜率优化吧……

因为对答案有贡献的是长或者宽最大的那个。所以我们首先按一维排序,这样我们就只需要考虑另一维了。

考虑用dp[i]表示购买前i块土地的最小费用,那么我们可以很容易的得到dp方程:

[dp[i] = min_{j=1}^{i-1}dp[j] + x[j+1] * y[i] ]

注意这个方程得到之前需要先对原序列处理。因为能对答案有贡献的是最大的值,所以我们在排序以后需要对序列线性扫描一次,只把比当前最大值大的加入序列。(所以只要取j+1)即可。

考虑斜率优化。我没有采用线性规划一样的方法……我们考虑两个决策p,q,其中q在p的后面。当q比p优的时候我们把式子列出来移项转化。这样得到以下结论:

(frac{dp[q] - dp[p]}{y[p+1] - y[q+1]} < x[i])

这样我们就进行斜率优化就好了。具体的可以看这篇:HNOI2008 玩具装箱Toy

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<set>
#include<vector>
#include<map>
#include<queue>
#define rep(i,a,n) for(int i = a;i <= n;i++)
#define per(i,n,a) for(int i = n;i >= a;i--)
#define enter putchar('
')
#define fr friend inline
#define y1 poj
#define mp make_pair
#define pr pair<int,int>
#define fi first
#define sc second
#define pb push_back

using namespace std;
typedef long long ll;
const int M = 50005;
const int INF = 1000000009;
const double eps = 1e-7;

ll read()
{
    ll ans = 0,op = 1;char ch = getchar();
    while(ch < '0' || ch > '9') {if(ch == '-') op = -1;ch = getchar();}
    while(ch >= '0' && ch <= '9') ans = ans * 10 + ch - '0',ch = getchar();
    return ans * op;
}

struct node
{
   ll val,pos;
}q1[M];

struct land
{
   ll x,y;
   bool operator < (const land &g)const
   {
      if(y == g.y) return x > g.x;
      return y > g.y;
   }
}a[M],t[M];

ll n,dp[M],q[M],head,tail,tot;

double slope(ll p,ll q)
{
   return (double)((dp[q] - dp[p]) / (a[p+1].y - a[q+1].y));
}

int main()
{
   n = read();
   rep(i,1,n) t[i].x = read(),t[i].y = read();
   sort(t+1,t+1+n);
   rep(i,1,n) if(t[i].x > a[tot].x) a[++tot] = t[i];
   rep(i,1,tot)
   {
      while(head < tail && slope(q[head],q[head+1]) <= a[i].x) head++;
      dp[i] = dp[q[head]] + a[i].x * a[q[head]+1].y;
      while(head < tail && slope(q[tail-1],q[tail]) >= slope(q[tail],i)) tail--;
      q[++tail] = i;
   }
   printf("%lld
",dp[tot]);
   return 0;
}

原文地址:https://www.cnblogs.com/captain1/p/10153364.html