# 洛谷P1578 奶牛牧场（悬线法思想）

#include <bits/stdc++.h>
using namespace std;
struct dat {
int x, y;
} a[1010000];
int l, w, n, maxn;
bool cmp1 (dat a, dat b)
{return a.y < b.y;}
bool cmp2 (dat a, dat b)
{return a.x < b.x;}
inline void init()
{
scanf("%d%d", &l, &w);
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d%d", &a[i].x, &a[i].y);
a[++n].x = 0, a[n].y = w;
a[++n].x = l, a[n].y = w;
a[++n].x = 0, a[n].y = 0;
a[++n].x = l, a[n].y = 0;
}
int main()
{
init();
sort(a + 1, a + 1 + n, cmp2);//复杂度O(n^2)枚举两个障碍里的面积, 用扫描的思想解决，
for (int i = 1; i <= n; i++)//high为最低的点，low为最高的点 pos为向右扩展的悬线长度，不需要向左，因为前面的向右等同于后面的向左
{
int high, low, pos;
high = 0, low = w, pos = l - a[i].x;//pos*(low-high)为当前矩阵面积最大值，
for (int j = i + 1; j <= n; j++)
{
if (pos * (low - high) <= maxn) break;//如果当前最优解都不能比maxn大，break
maxn = max(maxn, (low - high) * (a[j].x - a[i].x));
if (a[j].y >= a[i].y)
low = min(low, a[j].y);
else
high = max(high, a[j].y);
}
}
sort(a + 1, a + 1 + n, cmp1);
for (int i = 1; i <= n; i++)
{
int lef, rig, pos;
lef = 0, rig = l,  pos = w - a[i].y;//lef为最左边的点，rig为当前最右边的点，pos为向下扩展的悬线长度。
for (int j = i + 1; j <= n; j++)
{
if (pos * (rig - lef) <= maxn) break;
maxn = max(maxn, (rig - lef) * (a[j].y - a[i].y));
if (a[j].x >= a[i].x)
rig = min(rig, a[j].x);
else
lef = max(lef, a[j].x);
}
}
for (int i = 1; i < n; i++)//有漏解的情况。
maxn = max( maxn, l * ( a[i + 1].y - a[i].y ) );
printf("%d", maxn);
return 0;
}

posted @ 2019-11-09 09:50  刘文尧  阅读(113)  评论(0编辑  收藏