CF-1163C2-Power Transmission (Hard Edition)(計算幾何,map表示向量)

題目鏈接:https://codeforces.com/contest/1163/problem/C2

題目大意:給出n個點(點不重複)。每個點之間都有一條線相連。重合的直線算一條。問有多少對直線相交。

思路:一個常見的方法,map種存上斜率。然後判斷斜率不相等的直線的個數。這道題因爲重合的直線算一條。所以我又開了一個一般式的直線去重。最後n*n枚舉直線,log(n)查詢即可。

個人感覺多了幾個log,時間花費似乎有點大。但還沒想到更好的思路優化。

ACCode:

#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#include<time.h>
#include<math.h>
// srand((unsigned)time(NULL));rand();
      
#include<map>//unordered_map
#include<set>//multiset
#include<deque>
#include<queue>
#include<stack>
#include<bitset>
#include<string>
#include<fstream>
#include<iostream>
#include<algorithm>
 
#define ll long long
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define clean(a,b) memset(a,b,sizeof(a))
using namespace std;
      
const int MAXN=1e3+10;
//const int MAXM=10;
const int INF32=0x3f3f3f3f;
const ll INF64=0x3f3f3f3f3f3f3f3f;
const ll MOD=1e9+7;
const double PI=acos(-1.0);
const double EPS=1.0e-8;
//unsigned register
// ios::sync_with_stdio(false)

struct Line{
	int A,B,C;
	void Intt(){
		int tmp=__gcd(A,__gcd(B,C));
		A/=tmp;B/=tmp;C/=tmp;
		if(A<0||(A==0&&B<0)) A=-A,B=-B,C=-C;
	}
	friend int operator < (Line a,Line b){
		if(a.A!=b.A) return a.A<b.A;
		if(a.B!=b.B) return a.B<b.B;
		return a.C<b.C;
	}
};
struct Point{
	ll x,y;
	Point(ll _x=0,ll _y=0){
		x=_x;y=_y;
	}
	void Intt(){ if(x<0||(x==0&&y<0)) x=-x,y=-y; }
	friend Point operator - (Point a,Point b){
		return Point(a.x-b.x,a.y-b.y);
	}
	friend ll operator ^ (Point a,Point b){
		return a.x*b.y-a.y*b.x;
	}
	friend int operator < (Point a,Point b){
		a.Intt();b.Intt();
		return (a^b)<0;
	}
};
map<Line,int> CntL;//重合的 
map<Point,int> CntP;//平行&&重合的 
Point Dots[MAXN];
int n;

int main(){
	while(~scanf("%d",&n)){
		CntL.clear();CntP.clear();
		for(int i=1;i<=n;++i){
			scanf("%lld%lld",&Dots[i].x,&Dots[i].y);
		}
		int tot=0;
		for(int i=1;i<=n;++i){
			for(int j=i+1;j<=n;++j){
				Point tmp=Dots[i]-Dots[j];tmp.Intt();
				Line l;l.A=Dots[j].y-Dots[i].y;l.B=Dots[i].x-Dots[j].x;l.C=-(Dots[i]^Dots[j]);l.Intt();
				CntL[l]++;
				if(CntL[l]==1) CntP[tmp]++,tot++;
			}
		}
		ll ans=0;
		for(int i=1;i<=n;++i){
			for(int j=i+1;j<=n;++j){
				Point tmp=Dots[i]-Dots[j];tmp.Intt();
				Line l;l.A=Dots[j].y-Dots[i].y;l.B=Dots[i].x-Dots[j].x;l.C=-(Dots[i]^Dots[j]);l.Intt();
				int samecntl=CntL[l],samecntp=CntP[tmp];//重合的,平行||重合的 
//				printf("ans=%lld samecntp=%d samecntl=%d tot=%d\n",ans,samecntp,samecntl,tot);
				if(samecntl==0) continue ;
				ans+=tot-samecntp;
				CntL[l]=0;CntP[tmp]-=1;tot-=1;//重合的都取消掉 
			}
		}printf("%lld\n",ans);
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章