二维平面上N个点之间共有C(n,2)条连线。求这C(n,2)条线中斜率小于0的线的数量。
二维平面上的一个点,根据对应的X Y坐标可以表示为(X,Y)。例如:(2,3) (3,4) (1,5) (4,6),其中(1,5)同(2,3)(3,4)的连线斜率 < 0,因此斜率小于0的连线数量为2。
Input
第1行:1个数N,N为点的数量(0 <= N <= 50000) 第2 - N + 1行:N个点的坐标,坐标为整数。(0 <= X[i], Y[i] <= 10^9)
Output
输出斜率小于0的连线的数量。(2,3) (2,4)以及(2,3) (3,3)这2种情况不统计在内。
Input示例
4 2 3 3 4 1 5 4 6
Output示例
2
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
int c[55555];
struct node
{
int x;
int y;
int num;
}a[55555];
bool cmp(node t1,node t2)
{
if(t1.x<t2.x) return true;
else if(t1.x==t2.x && t1.y<t2.y) return true;
return false;
}
bool cmp1(node t1,node t2)
{
if(t1.y<t2.y) return true;
return false;
}
int mmax;
void update(int x,int num)
{
while(x<=mmax) {
c[x]+=num;
x+=x&-x;
}
}
int getsum(int x)
{
int ans=0;
while(x>0) {
ans+=c[x];
x-=x&-x;
}
return ans;
}
int main()
{
int n,i,j,ans,sum,t;
scanf("%d",&n);
mmax=0;
for(i=1;i<=n;i++) {
scanf("%d%d",&a[i].x,&a[i].y);
}
sort(a+1,a+1+n,cmp1);
int k=1;
a[1].num=k;
for(i=2;i<=n;i++) {
if(a[i].y==a[i-1].y) a[i].num=k;
else {
a[i].num=++k;
}
}
mmax=k;
sort(a+1,a+1+n,cmp);
ans=0;
sum=1;
t=0;
update(a[1].num,1);
for(i=2;i<=n;i++) {
ans+=sum-getsum(a[i].num);
update(a[i].num,1);
sum++;
if(a[i].x==a[i-1].x && a[i].num<a[i-1].num) t++;
}
printf("%d\n",ans-t);
return 0;
}