CF-281C Rectangle Puzzle(凸包+面积)

题意:https://codeforces.com/problemset/problem/281/C

就存个模板

  1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
  2 #include <cstdio>//sprintf islower isupper
  3 #include <cstdlib>//malloc  exit strcat itoa system("cls")
  4 #include <iostream>//pair
  5 #include <fstream>//freopen("C:\\Users\\13606\\Desktop\\Input.txt","r",stdin);
  6 #include <bitset>
  7 //#include <map>
  8 //#include<unordered_map>
  9 #include <vector>
 10 #include <stack>
 11 #include <set>
 12 #include <string.h>//strstr substr strcat
 13 #include <string>
 14 #include <time.h>// srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
 15 #include <cmath>
 16 #include <deque>
 17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
 18 #include <vector>//emplace_back
 19 //#include <math.h>
 20 #include <cassert>
 21 #include <iomanip>
 22 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
 23 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
 24 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
 25 //******************
 26 clock_t __START,__END;
 27 double __TOTALTIME;
 28 void _MS(){__START=clock();}
 29 void _ME(){__END=clock();__TOTALTIME=(double)(__END-__START)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;}
 30 //***********************
 31 #define rint register int
 32 #define fo(a,b,c) for(rint a=b;a<=c;++a)
 33 #define fr(a,b,c) for(rint a=b;a>=c;--a)
 34 #define mem(a,b) memset(a,b,sizeof(a))
 35 #define pr printf
 36 #define sc scanf
 37 #define ls rt<<1
 38 #define rs rt<<1|1
 39 typedef pair<int,int> PII;
 40 typedef vector<int> VI;
 41 typedef unsigned long long ull;
 42 typedef long long ll;
 43 typedef double db;
 44 const db E=2.718281828;
 45 const db PI=acos(-1.0);
 46 const ll INF=(1LL<<60);
 47 const int inf=(1<<30);
 48 const db ESP=1e-9;
 49 const int mod=(int)1e9+7;
 50 const int N=(int)1e6+10;
 51
 52 bool zero(double x){
 53     return (x>0?x:-x)<ESP;
 54 }
 55 struct point
 56 {
 57     double x,y;
 58 };
 59 struct line
 60 {
 61     point a,b;
 62 };
 63 //计算 cross product (P1-P0)x(P2-P0)
 64 double xmult(point p1,point p2,point p0){
 65     return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
 66 }
 67 //计算 dot product (P1-P0).(P2-P0)
 68 double dmult(point p1,point p2,point p0){
 69     return (p1.x-p0.x)*(p2.x-p0.x)+(p1.y-p0.y)*(p2.y-p0.y);
 70 }
 71 //两点距离
 72 double distance(point p1,point p2){
 73     return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
 74 }
 75 bool isIntersected(point s1,point e1, point s2,point e2){
 76     return    (max(s1.x,e1.x) >= min(s2.x,e2.x))&&
 77               (max(s2.x,e2.x) >= min(s1.x,e1.x))&&
 78               (max(s1.y,e1.y) >= min(s2.y,e2.y))&&
 79               (max(s2.y,e2.y) >= min(s1.y,e1.y))&&
 80               (xmult(s1,s2,e1)*xmult(s1,e1,e2)>0)&&
 81               (xmult(s2,s1,e2)*xmult(s2,e2,e1)>0);
 82 }
 83 point intersection(point u1,point u2,point v1,point v2){
 84     point ret=u1;
 85     double t=((u1.x-v1.x)*(v1.y-v2.y)-(u1.y-v1.y)*(v1.x-v2.x))
 86              /((u1.x-u2.x)*(v1.y-v2.y)-(u1.y-u2.y)*(v1.x-v2.x));
 87     ret.x+=(u2.x-u1.x)*t;
 88     ret.y+=(u2.y-u1.y)*t;
 89     return ret;
 90 }
 91
 92
 93 //点以原点逆时针旋转α度
 94 point rotate(point p0,double alpha){
 95     alpha=alpha/180*PI;
 96     return {p0.x*cos(alpha)-p0.y*sin(alpha),p0.y*cos(alpha)+p0.x*sin(alpha)};
 97 }
 98
 99 point Pin[20];
100 int cmp(point a,point b)  ///设<p1,p2,...pm>为对其余点按以p0为中心的极角逆时针排序所得的点集(如果有多个点有相同的极角,除了距p0最远的点外全部移除)
101 {
102     if(xmult(a,b,Pin[1])>ESP)
103         return 1;
104     if(fabs(xmult(a,b,Pin[1]))<ESP&&distance(b,Pin[1])-distance(a,Pin[1])>ESP)
105         return 1;
106     return 0;
107 }
108
109 point Stack[N];
110 int top;
111 void Graham(int n,point p[])
112 {
113     top=3;///栈顶在2,因为凸包的前两个点是不会变了
114     sort(p+2,p+1+n,cmp);
115     Stack[1]=p[1];///压p0p1p2进栈S
116     Stack[2]=p[2];
117     Stack[3]=p[3];
118     for(int i=4;i<=n;i++){
119         while(top>=1&&xmult(p[i],Stack[top],Stack[top-1])>ESP){///有了更好的选择
120             top--;
121         }
122         Stack[++top]=p[i];
123     }
124 }
125 double getArea()
126 {
127     double sum=fabs(xmult(Stack[2],Stack[3],Stack[1]));
128     for(int i=3;i<top;i++)
129         sum+=fabs(xmult(Stack[i],Stack[i+1],Stack[1]));
130     return sum/2.0;
131 }
132
133 int main()
134 {
135     double w,h,alpha;
136     sc("%lf%lf%lf",&w,&h,&alpha);
137     if(alpha==0||alpha==180)
138     {
139         pr("%.10lf\n",w*h);
140         return 0;
141     }
142     if(alpha==90&&w==h)
143     {
144         pr("%.10lf\n",w*h);
145         return 0;
146     }
147     line line1[10];
148     point p1={-w/2,h/2};
149     point p2={w/2,h/2};
150     point p3={w/2,-h/2};
151     point p4={-w/2,-h/2};
152     line1[1]={p1,p2};
153     line1[2]={p2,p3};
154     line1[3]={p3,p4};
155     line1[4]={p4,p1};
156
157     line line2[10];
158     p1=rotate(p1,alpha);
159     p2=rotate(p2,alpha);
160     p3=rotate(p3,alpha);
161     p4=rotate(p4,alpha);
162     line2[1]={p1,p2};
163     line2[2]={p2,p3};
164     line2[3]={p3,p4};
165     line2[4]={p4,p1};
166
167     int cp=0;
168     for(int i=1;i<=4;++i)
169     {
170         for(int j=1;j<=4;++j)
171         {
172             if(isIntersected(line1[i].a,line1[i].b,line2[j].a,line2[j].b))
173                 Pin[++cp]=intersection(line1[i].a,line1[i].b,line2[j].a,line2[j].b);
174         }
175     }
176 //    for(int i=1;i<=cp;++i)pr("%lf\t%lf\n",Pin[i].x,Pin[i].y);
177     Graham(cp,Pin);
178     pr("%.10lf\n",getArea());
179     return 0;
180 }
181
182 /**************************************************************************************/

原文地址:https://www.cnblogs.com/--HPY-7m/p/12687822.html

时间: 2024-10-16 22:37:40

CF-281C Rectangle Puzzle(凸包+面积)的相关文章

poj 3348 Cow 凸包面积

Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8122   Accepted: 3674 Description Your friend to the south is interested in building fences and turning plowshares into swords. In order to help with his overseas adventure, they are f

poj 3348(凸包面积)

Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8063   Accepted: 3651 Description Your friend to the south is interested in building fences and turning plowshares into swords. In order to help with his overseas adventure, they are f

POJ 3348 Cows(凸包面积)

Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7515   Accepted: 3418 Description Your friend to the south is interested in building fences and turning plowshares into swords. In order to help with his overseas adventure, they are f

【UVA10652】Board Wrapping(求凸包面积)

点此看题面 大致题意: 告诉你若干个矩形的重心坐标.长.宽和相对\(y\)轴的偏转角度,求矩形面积和与能围住这些矩形的最小凸包面积之比. 矩形面积和 这应该是比较好求的吧. 已经给了你长和宽,直接乘起来累加即可. 最小凸包面积 这道题关键还是在于求凸包面积. 首先,我们要注意将题目中给出的角度转换成弧度,还要记得取相反数,不然调死你. 这段代码可以与旋转函数放在一起: inline Point Rotate(Vector A,double deg) { register double rad=d

leetcode 223. Rectangle Area 计算面积---------- java

Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined by its bottom left corner and top right corner as shown in the figure. Assume that the total area is never beyond the maximum possible value of int. 计算

poj 3348 Cows 求凸包面积

题目链接 大意: 求凸包的面积. #include <iostream> #include <vector> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <map> #include <set> #include <string> #include <queue>

UESTC 33 Area --凸包面积

题意: 求一条直线分凸包两边的面积. 解法: 因为题意会说一定穿过,那么不会有直线与某条边重合的情况.我们只要找到一个直线分成的凸包即可,另一个的面积等于总面积减去那个的面积. 怎么得到分成的一个凸包呢? 从0~n扫过去,如果扫到的边与直线不相交,那么把端点加进新凸包中,如果直线与扫到的边相交了,那么就将交点加入新凸包,然后以后不相交的话也不加入点到新凸包中,直到遇到下一个与直线相交的边,则把交点又加入新凸包,然后在扫到末尾加入点.这样就得到了. 即找到如图: 注意四舍五入. 代码: #incl

[LeetCode] Rectangle Area 矩形面积

Find the total area covered by two rectilinear rectangles in a2D plane. Each rectangle is defined by its bottom left corner and top right corner as shown in the figure. Assume that the total area is never beyond the maximum possible value of int. Cre

[HDU 4419] Colourful Rectangle (扫描线 矩形面积并)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4419 题目大意:比矩形面积并多了颜色,问染成的每种颜色的面积. 矩形面积并的扫描线维护的是长度,这道题就是维护每个颜色的长度,写起来很蛋疼. 1 #include <cstdio> 2 #include <cmath> 3 #include <algorithm> 4 #include <cstring> 5 #include <vector> 6