基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题
收藏
关注
二维平面上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<algorithm>
#include<string.h>
using namespace std;
struct node
{
int x;
int y;
int pos;
}ac[50005];
int c[50005];
int k,n;
bool cmp(node a,node b)
{
if(a.y==b.y)
return a.x<b.x;
return a.y<b.y;
}
int lowbit(int x)
{
return x&(-x);
}
bool cmp1(node a,node b)
{
if(a.x==b.x)
return a.pos<b.pos;
return a.x<b.x;
}
void update(int num,int x)
{
while(x<=n)
{
c[x]+=num;
x+=lowbit(x);
}
}
int get_sum(int i)
{
int sum=0;
while(i)
{
sum+=c[i];
i-=lowbit(i);
}
return sum;
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>ac[i].x>>ac[i].y;
}
sort(ac+1,ac+n+1,cmp);
k=0;
for(int i=1;i<=n;i++)
{
if(ac[i].y!=ac[i-1].y||i==1)
ac[i].pos=++k;
ac[i].pos=k;
}
sort(ac+1,ac+n+1,cmp1);
int ans=0;
for(int i=1;i<=n;i++)
{
update(1,ac[i].pos);
ans+=(i-get_sum(ac[i].pos));
}
cout<<ans<<endl;
return 0;
}