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);
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值