AcWing 1749. 阻挡广告牌 II----小白错了n次之后才知道的分类讨论QAQ
原题链接
简单
作者:
明璐花生牛奶
,
2022-03-31 19:27:36
,
所有人可见
,
阅读 158
错了五次,耗时好几个小时才搞出来的分类讨论问题,FW小白路过
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
struct Coo{
int x;
int y;
};
int main()
{
Coo c1,c2,c3,c4;
//c1 c2是割草机的坐标;c3 c4是牛饲料的坐标
//以割草机广告牌为参考系,牛饲料广告牌运动
cin>>c1.x>>c1.y>>c2.x>>c2.y>>c3.x>>c3.y>>c4.x>>c4.y;
int res,area=(c2.x-c1.x)*(c2.y-c1.y);
//先假设竖直无漏出
if(c3.x<=c1.x&&c2.x<=c4.x){
if(c1.y>=c4.y||c2.y<=c3.y||(c1.y<c3.y&&c2.y>c4.y))
res=area;
else
res=(max(0,c2.y-c4.y)+max(0,c3.y-c1.y))*(c2.x-c1.x);
cout << res <<endl;
return 0;
}
//接着假设横向的有无漏出的情况
if(c1.y>=c3.y&&c2.y<=c4.y){
if(c2.x<=c3.x||c1.x>=c4.x||(c1.x<c3.x&&c2.x>c4.x))
res=area;
else
res=(max(0,c3.x-c1.x)+max(0,c2.x-c4.x))*(c2.y-c1.y);
cout << res <<endl;
return 0;
}
res=area;
cout<<res<<endl;
return 0;
}