Codeforces 1025F Disjoint Triangles (计算几何)
生活随笔
收集整理的這篇文章主要介紹了
Codeforces 1025F Disjoint Triangles (计算几何)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接
https://codeforces.com/contest/1025/problem/F
題解
一道挺有意思的計算幾何題 qwq
關鍵在于注意到任何一對不相交的三角形之間,一定有 \(2\) 條內公切線,而一對相交的三角形之間有 \(0\) 條。
于是枚舉兩個點,求一下這兩個點的連線兩側分別有多少點,就可以求出有多少對三角形以它為公切線,總和除以 \(2\) 就是答案。
這個用經典掃描線套路做即可。
時間復雜度 \(O(n^2\log n)\).
代碼
#include<bits/stdc++.h> #define llong long long #define mkpr make_pair #define iter iterator #define riter reversed_iterator #define y1 Lorem_ipsum_dolor using namespace std;inline int read() {int x = 0,f = 1; char ch = getchar();for(;!isdigit(ch);ch=getchar()) {if(ch=='-') f = -1;}for(; isdigit(ch);ch=getchar()) {x = x*10+ch-48;}return x*f; }const int mxN = 2000; struct Point {llong x,y;Point() {}Point(int _x,int _y):x(_x),y(_y) {}int quadrant() {return y>=0?(x>=0?0:1):(x>=0?3:2);} } a[mxN+3],b[mxN*2+3]; typedef Point Vector; Point operator +(const Point &x,const Point &y) {return Point(x.x+y.x,x.y+y.y);} Point operator -(const Point &x,const Point &y) {return Point(x.x-y.x,x.y-y.y);} Point operator *(const Point &x,const double &y) {return Point(x.x*y,x.y*y);} llong Dot(Vector x,Vector y) {return x.x*y.x+x.y*y.y;} llong Cross(Vector x,Vector y) {return x.x*y.y-x.y*y.x;} bool cmp_ang(Point x,Point y) {return x.quadrant()!=y.quadrant()?x.quadrant()<y.quadrant():Cross(x,y)>0;}int n;int main() {n = read();for(int i=1; i<=n; i++) a[i].x = read(),a[i].y = read();llong ans = 0ll;for(int i=1; i<=n; i++){for(int j=1; j<=n; j++) b[j] = a[j]-a[i]; swap(b[i],b[1]);sort(b+2,b+n+1,cmp_ang);for(int j=n+1; j<=n+n-1; j++) b[j] = b[j-(n-1)];for(int j=2,k=3; j<=n; j++){while(k<j||Cross(b[j],b[k+1])>0) {k++;}int cnt1 = k-j,cnt2 = n-2-cnt1; // printf("j=%d k=%d cnt1=%d cnt2=%d\n",j,k,cnt1,cnt2);ans += 1ll*cnt1*(cnt1-1ll)*cnt2*(cnt2-1ll)/4ll;}}ans/=2ll;printf("%I64d\n",ans);return 0; }總結
以上是生活随笔為你收集整理的Codeforces 1025F Disjoint Triangles (计算几何)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Codeforces 1025G Com
- 下一篇: Codeforces 1188 题解