zoukankan      html  css  js  c++  java
  • bzoj 1914: [Usaco2010 OPen]Triangle Counting 数三角形

    USACO划水中。。。

    题目中要求经过原点的三角形数目,但这种三角形没什么明显的特点并不好求,所以可以求不经过原点的三角形数量。

    对于一个非法三角形,它离原点最近的那条边连接的两个点所连的两条边一定在这个点与原点连线的一侧。

    为了不重的计数,只用极角序最小的点算。

    实现的时候可以把原数组复制一遍再用一个指针。

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<cmath>
    #define ll long long
    using namespace std;
    const int N = 200005;
    int n;
    ll ans;
    struct node
    {
    	double x,y,ang;
    	friend bool operator < (const node &aa,const node &bb)
    	{
    		return aa.ang<bb.ang;
    	}
    	friend double operator * (const node &aa,const node &bb)
    	{
    		return aa.x*bb.y-aa.y*bb.x;
    	}
    }a[N];
    int main()
    {
    	scanf("%d",&n);
    	for(int i=1;i<=n;i++)scanf("%lf%lf",&a[i].x,&a[i].y);
    	for(int i=1;i<=n;i++)a[i].ang=atan2(a[i].y,a[i].x);
    	sort(a+1,a+n+1);
    	for(int j=1;j<=n;j++)a[n+j]=a[j];
    	int t=0,r=1;
    	for(int i=1;i<=n;i++)
    	{
    		while(r<i+n-1&&a[i]*a[r+1]>0)r++,t++;
    		ans+=1LL*t*(t-1)/2;
    		t--;
    	}
    	printf("%lld
    ",1LL*n*(n-1)*(n-2)/6-ans);
    	return 0;
    

      

  • 相关阅读:
    课程总结1
    网站概要设计说明书
    数据库设计说明书
    团队项目之7天工作计划
    NABC
    敏捷开发综述
    二维数组最大子数组
    电梯调度
    输出整数数组中 最大的子数组的结果
    【自习任我行】第二阶段个人总结10
  • 原文地址:https://www.cnblogs.com/ezyzy/p/6648750.html
Copyright © 2011-2022 走看看