D. Vanya and Triangles(平面内三角形个数+精度)

https://codeforces.com/problemset/problem/552/D


思路:
看了下有n^3跑过去的..看了下5s觉得n^3有点离谱了,把别人代码交上去1000ms..

正解应该是n^2logn的吧。

这个题范围最大就是100*100的矩形,所以其斜率最多200个,提前预处理出以每个点为基础的斜率上有多少点,每次遍历的时候每一点的每一个斜率上算C(X,2)个,最后用总数的C(n,3)给减去。

注意下算斜率可能卡精度(wa4

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=2e3+100;
typedef long long LL;
const LL inf=0x3f3f3f3f;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
map<double,double>map1[maxn];
struct P{
   double x,y;
}p[maxn];
double get(LL i,LL j)
{
	LL y=p[i].y-p[j].y;
	LL x=p[i].x-p[j].x;
	if (!x)return 100000;
	LL gd=__gcd(x,y);
	x/=gd;
	y/=gd;
    return (y*1.0/x);
}
int main(void){
   cin.tie(0);std::ios::sync_with_stdio(false);
   LL n;cin>>n;
   LL tot=n*(n-1)*(n-2)/6;
   for(LL i=1;i<=n;i++){
      cin>>p[i].x>>p[i].y;
   }
   for(LL i=1;i<=n;i++){
       for(LL j=i+1;j<=n;j++){
           double k=get(i,j);
           map1[i][k]++;
       }
   }
   LL ans=0;
   for(LL i=1;i<=n;i++){
       for(auto j:map1[i]){
           if(j.second>=2){
              ans+=(j.second)*(j.second-1)/2;
           }
       }
   }
   cout<<tot-ans<<"\n";
   return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值