bzoj4578: [Usaco2016 OPen]Splitting the Field

2365: Splitting the Field

题意:n个点,求用两个矩形面积覆盖完所有点和一个矩形覆盖完少多少面积

思路:枚举两个矩形的分割线,也就是把所有点分成两个部分,枚举分割点;先预处理每个点之前和之后的最大,最低高度;

矩形可以横着分,也可以竖着分

 1 // #pragma comment(linker, "/STACK:102c000000,102c000000")
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <sstream>
 6 #include <string>
 7 #include <algorithm>
 8 #include <list>
 9 #include <map>
10 #include <vector>
11 #include <queue>
12 #include <stack>
13 #include <cmath>
14 #include <cstdlib>
15 // #include <conio.h>
16 using namespace std;
17 #define clc(a,b) memset(a,b,sizeof(a))
18 #define inf 0x3f3f3f3f
19 #define lson l,mid,rt<<1
20 #define rson mid+1,r,rt<<1|1
21 const int N = 50010;
22 const int M = 1e6+10;
23 const int MOD = 1e9+7;
24 #define LL long long
25 #define LB long double
26 #define mi() (l+r)>>1
27 double const pi = acos(-1);
28 const double eps = 1e-8;
29 void fre() {
30     freopen("in.txt","r",stdin);
31 }
32 // inline int r() {
33 //     int x=0,f=1;char ch=getchar();
34 //     while(ch>'9'||ch<'0') {if(ch=='-') f=-1;ch=getchar();}
35 //     while(ch>='0'&&ch<='9') { x=x*10+ch-'0';ch=getchar();}return x*f;
36 // }
37 struct point{
38     int x,y;
39     point(int x_=0,int y_=0):x(x_),y(y_){}
40     bool operator < (const point &a) const{
41         return x==a.x?y<a.y:x<a.x;
42     }
43 }p[N];
44 int l[N],r[N],l1[N],r1[N];
45 int n;
46 LL ans;
47 void work(){
48     clc(l,inf),clc(r,-inf),clc(l1,inf),clc(r1,-inf);
49     sort(p+1,p+n+1);
50     for(int i=1;i<=n;i++){
51         l[i]=min(l[i-1],p[i].y),r[i]=max(r[i-1],p[i].y);
52     }
53     for(int i=n;i>=1;i--){
54         l1[i]=min(l1[i+1],p[i].y),r1[i]=max(r1[i+1],p[i].y);
55     }
56     for(int i=2;i<=n;i++){
57         LL tem=(LL)(r[i-1]-l[i-1])*(p[i-1].x-p[1].x)+(LL)(r1[i]-l1[i])*(p[n].x-p[i].x);
58         ans=min(tem,ans);
59     }
60 }
61 int main(){
62     int minx=inf,miny=inf,maxx=-inf,maxy=-inf;
63     scanf("%d",&n);
64     for(int i=1;i<=n;i++){
65         scanf("%d%d",&p[i].x,&p[i].y);
66         minx=min(minx,p[i].x);
67         maxx=max(maxx,p[i].x);
68         miny=min(miny,p[i].y);
69         maxy=max(maxy,p[i].y);
70     }
71     ans=(LL)(maxx-minx)*(maxy-miny);
72     LL ans1=ans;
73     work();
74     for(int i=1;i<=n;i++) swap(p[i].x,p[i].y);
75     work();
76     printf("%lld
",ans1-ans);
77     return  0;
78 }
原文地址:https://www.cnblogs.com/ITUPC/p/5702491.html