jzoj 3129. 【WinterCamp 2013】数三角形

Description

在一只大灰狼偷偷潜入Farmer Don的牛群被群牛发现后,贝西现在不得不履行着她站岗的职责。从她的守卫塔向下瞭望简直就是一件烦透了的事情。她决定做一些开发智力的小练习,防止她睡着了。
想象牧场是一个X,Y平面的网格。她将N只奶牛标记为1…N (1 <= N <= 100,000),每只奶牛的坐标为X_i,Y_i (-100,000 <= X_i <= 100,000;-100,000 <= Y_i <= 100,000; 1 <= i <=N)。然后她脑海里想象着所有可能由奶牛构成的三角形。如果一个三角形完全包含了原点(0,0),那么她称这个三角形为“黄金三角形”。原点不会落在任何一对奶牛的连线上。另外,不会有奶牛在原点。
给出奶牛的坐标,计算出有多少个“黄金三角形”。

Input

  • 第一行:一个整数N

  • 第2到第N+1行:每行两个整数X_i,Y_i,表示每只牛的坐标

Output

  • 第一行: 一行包括一个整数,表示“黄金三角形的数量”

Sample Input

5
-5 0
0 2
11 2
-11 -6
11 -5

Sample Output

5

Data Constraint

Hint

考虑五只牛,坐标分别为(-5,0), (0,2), (11,2), (-11,-6), (11,-5)。

下图是由贝西视角所绘出的图示。

      ............|............
      ............*..........*.
      ............|............
      -------*----+------------
      ............|............
      ............|............
      ............|............
      ............|............
      ............|..........*.
      .*..........|............
      ............|............

Solution

这题主要是考察细节。
考场想到爆炸,把在x轴上的和在y轴上的不停地特判导致某不知名错误WA10。
然后听了讲题人的话,感觉真得好像可以把它算在象限里面。
搞了搞,就把考场代码简化了一下,优化了一下就WA90了。
发现将坐标轴的点并到象限里不能随便并,改了改才AC了。

Code

#include<cstdio>
#include<algorithm>
//#include<bits/stdc++.h>
#define N 100010
#define db double
#define ll long long 
using namespace std;
int n,x,y,one=0,two=0,thr=0,fou=0;
db sl,fir[N],sec[N],thi[N],fo[N];
ll ans=0;

inline int read()
{
	int x=0,f=0; char c=getchar();
	while (c<'0' || c>'9') f=(c=='-') ? 1:f,c=getchar();
	while (c>='0' && c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
	return f ? -x:x;
}

int main()
{
	freopen("triangle.in","r",stdin);
//	freopen("triangle.out","w",stdout);
	n=read();
	for (int i=1,x,y;i<=n;i++)
	{
		x=read(),y=read();
		if (x==0) sl=999999999;
		else if (y==0) sl=-999999999;
		else sl=(db)y/x;
		if (x>0)
		{
			if (y>0) fir[++one]=sl;
			else fo[++fou]=sl;
		}
		else
		{
			if (y>=0) sec[++two]=sl;
			else thi[++thr]=sl;
		}
	}
	sort(fir+1,fir+one+1);
	sort(sec+1,sec+two+1);
	sort(thi+1,thi+thr+1);
	sort(fo+1,fo+fou+1);
	for (int i=1,j=0;i<=one;i++)
	{
		while (j<thr && thi[j+1]<fir[i]) j++;
		ans+=j*fou+j*(thr-j);
	}
	for (int i=1,j=0;i<=two;i++)
	{
		while (j<fou && fo[j+1]<sec[i]) j++;
		ans+=j*one+j*(fou-j);
	}
	for (int i=1,j=0;i<=thr;i++)
	{
		while (j<one && fir[j+1]<thi[i]) j++;
		ans+=j*two+j*(one-j);
	}
	for (int i=1,j=0;i<=fou;i++)
	{
		while (j<two && sec[j+1]<fo[i]) j++;
		ans+=j*thr+j*(two-j);
	}
	printf("%lld
",ans);
	return 0;
}
转载需注明出处。
原文地址:https://www.cnblogs.com/jz929/p/11817533.html