1. 程式人生 > >luogu P1578 奶牛浴場

luogu P1578 奶牛浴場

 

很好的一道題

王知昆爺爺的論文(講的特別清楚) https://wenku.baidu.com/view/bc8311f69e314332396893f7.html

先貼上AC程式碼

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 using namespace std;
 5 template<class T>void read(T &x){
 6     int f=0;x=0;char ch=getchar();
7 while(ch<'0'||ch>'9') {f|=(ch=='-');ch=getchar();} 8 while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();} 9 x=f?-x:x; 10 } 11 12 const int N=5007; 13 int ans,yx,yn,n,l,w,lst; 14 bool qwq[30007]; 15 struct yhhh{ 16 int x,y; 17 }a[N];
18 19 inline bool cmp(yhhh A,yhhh B){ 20 return A.x<B.x; 21 } 22 23 int main(){ 24 read(l),read(w),read(n); 25 for(int i=1;i<=n;++i) 26 read(a[i].x),read(a[i].y),qwq[a[i].y]=1; 27 for(int i=0;i<=w;++i) 28 if(qwq[i]){ 29 ans=max(ans,w*(i-lst));
30 lst=i; 31 } 32 ans=max(ans,w*(w-lst)); 33 a[++n].x=0,a[n].y=0; 34 a[++n].x=l,a[n].y=0; 35 a[++n].x=0,a[n].y=w; 36 a[++n].x=l,a[n].y=w; 37 sort(a+1,a+n+1,cmp); 38 for(int i=1;i<=n;++i){ 39 yn=0,yx=w; 40 for(int j=i+1;j<=n;++j){ 41 if(yx<=yn) break; 42 if(a[j].y>yx||a[j].y<yn) continue; 43 ans=max(ans,(a[j].x-a[i].x)*(yx-yn)); 44 if(a[j].y>=a[i].y) yx=min(yx,a[j].y); 45 if(a[j].y<=a[i].y) yn=max(yn,a[j].y); 46 } 47 ans=max(ans,(yx-yn)*(l-a[i].x)); 48 } 49 for(int i=n;i>=1;--i){ 50 yn=0,yx=w; 51 for(int j=i-1;j>=1;--j){ 52 if(yx<=yn) break; 53 if(a[j].y>yx||a[j].y<yn) continue; 54 ans=max(ans,(a[i].x-a[j].x)*(yx-yn)); 55 if(a[j].y>=a[i].y) yx=min(yx,a[j].y); 56 if(a[j].y<=a[i].y) yn=max(yn,a[j].y); 57 } 58 } 59 printf("%d\n",ans); 60 return 0; 61 }

case1:93ps

資料:

IN      6 4 4 1 2 4 1 4 3 2 1

OUT 10

沒有考慮如下邊界情況

ans=max(ans,(a[j].x-a[i].x)*(yx-yn));

 case2:84ps

沒有考慮如下情況

case3:56ps

沒有考慮上下邊界

 

另:幾組hack資料

IN
6 4
4
1 2
4 1
4 3
2 1

OUT
10

IN
10 10
3
3 0
8 2
3 9

OUT
72

IN
4 7
5
0 6
0 0
3 2
1 0
0 3

OUT
21

IN
10 10
2
8 1
3 9

OUT
80