日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

UVAL 4728 Squares(旋转卡壳)

發布時間:2023/12/24 编程问答 41 如意码农
生活随笔 收集整理的這篇文章主要介紹了 UVAL 4728 Squares(旋转卡壳) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Squares

【題目鏈接】Squares

【題目類型】旋轉卡殼

&題解:

聽著算法名字,感覺挺難,仔細一看之后,發現其實很簡單,就是依靠所構成三角行面積來快速的找對踵點,就可以省去很多的復雜度了.旋轉的復雜度是O(n),之后還有計算每條邊對應的對踵點復雜度平均大約O(n/2)在實際中也許可以更快

&代碼:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std; struct Point {
int x,y;
Point(int x=0,int y=0): x(x),y(y) {}
};
typedef Point Vector;
Vector operator - (const Vector& A,const Vector& B) {return Vector(A.x-B.x , A.y-B.y);}
bool operator == (const Point& a,const Point& b) {return a.x==b.x&&a.y==b.y;}
bool operator < (const Point& a,const Point& b) {return a.x<b.x||(a.x==b.x&&a.y<b.y);}
int Cross(Vector A,Vector B) {return A.x*B.y - A.y*B.x;}
int Dist2(const Point& a,const Point& b) {return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);} vector<Point> ConvexHull(vector<Point> p) {
sort(p.begin(),p.end());
p.erase(unique(p.begin(),p.end()) , p.end());
int n=p.size(), m=0;
vector<Point> ch(n+1);
for(int i=0; i<n; i++) {
while(m>1&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-1])<=0) m--;
ch[m++]=p[i];
}
int k=m;
for(int i=n-2; i>=0; i--) {
while(m>k&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-1])<=0) m--;
ch[m++]=p[i];
}
if(m>1) m--;
ch.resize(m);
return ch;
} int diameter2(vector<Point>& points) {
vector<Point> p=ConvexHull(points);
int n=p.size();
if(n==1) return 0;
if(n==2) return Dist2(p[0] , p[1]);
p.push_back(p[0]);
int ans=0;
for(int u=0, v=1; u<n; u++) {
for(;;) {
int diff = Cross(p[u+1]-p[u] , p[v+1]-p[v]);
if( diff<=0 ) {
ans=max(ans , Dist2(p[u],p[v]));
//2個三角形面積相等
if(diff==0) ans=max(ans, Dist2(p[u], p[v+1]));
break;
}
v=(v+1)%n;
}
}
return ans;
} int main() {
freopen("E:1.in","r",stdin);
int T;
scanf("%d",&T);
while(T--) {
int n;
scanf("%d" ,&n);
vector<Point> po;
for(int i=0; i<n; i++) {
int x,y,w;
scanf("%d%d%d",&x,&y,&w);
po.push_back(Point(x,y));
po.push_back(Point(x+w,y));
po.push_back(Point(x,y+w));
po.push_back(Point(x+w,y+w));
}
printf("%d\n", diameter2(po));
}
return 0;
}

總結

以上是生活随笔為你收集整理的UVAL 4728 Squares(旋转卡壳)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。