51nod 1107 斜率小于0的连线数量 树状数组
来源:互联网 发布:rsa算法加密解密过程 编辑:程序博客网 时间:2024/06/06 03:57
二维平面上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示例
42 33 41 54 6
对于第i个点,如果之前的点在他的左上方,斜率小于0;
我们按照x坐标排好序,对于y坐标,其实就是统计逆序对了。
比如2 3
3 4
1 5
4 6
按x排序后y为5 3 4 6.
逆序对的数量就是最终答案
所以我们利用树状数组来做就可以了
不过需要注意的时,树状数组连带y相同的也统计上了,我们需要最后减去。
也就是if(a[i].x==a[i-1].x&&a[i].num<a[i-1].num)
{
t++
}
最后的ans-t输出即可
#include <algorithm>
#include <ctype.h>
#include <cstdio>
using namespace std;
void read(int &x)
{
x=0;char ch=getchar();
for(;!isdigit(ch);ch=getchar());
for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';
}
struct node
{
int x,y,num;
}pos[50005];
bool cmp(node a,node b)
{
return a.y<b.y;
}
bool comp(node a,node b)
{
if(a.x!=b.x) return a.x<b.x;
else if(a.x==b.x) return a.y<b.y;
}
int ans,sum,size,tim,n,tag[50005];
int lowbit(int x)
{
return x&(-x);
}
void add(int x,int y)
{
for(;x<=size;x+=lowbit(x)) tag[x]+=y;
}
int query(int x)
{
int ans=0;
for(;x;x-=lowbit(x)) ans+=tag[x];
return ans;
}
int main()
{
read(n);
for(int i=1;i<=n;i++)
{
read(pos[i].x);
read(pos[i].y);
}
sort(pos+1,pos+1+n,cmp);
pos[1].num=++tim;
for(int i=2;i<=n;i++)
{
if(pos[i].y==pos[i-1].y) pos[i].num=tim;
else pos[i].num=++tim;
}
sort(pos+1,pos+1+n,comp);
int t=0;
ans=0;
size=tim;
add(pos[1].num,1);
for(int i=2;i<=n;i++)
{
ans+=i-1-query(pos[i].num);
add(pos[i].num,1);
if(pos[i].x==pos[i-1].x&&pos[i].num <pos[i-1].num) t++;
}
printf("%d",ans-t);
return 0;
}
#include <ctype.h>
#include <cstdio>
using namespace std;
void read(int &x)
{
x=0;char ch=getchar();
for(;!isdigit(ch);ch=getchar());
for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';
}
struct node
{
int x,y,num;
}pos[50005];
bool cmp(node a,node b)
{
return a.y<b.y;
}
bool comp(node a,node b)
{
if(a.x!=b.x) return a.x<b.x;
else if(a.x==b.x) return a.y<b.y;
}
int ans,sum,size,tim,n,tag[50005];
int lowbit(int x)
{
return x&(-x);
}
void add(int x,int y)
{
for(;x<=size;x+=lowbit(x)) tag[x]+=y;
}
int query(int x)
{
int ans=0;
for(;x;x-=lowbit(x)) ans+=tag[x];
return ans;
}
int main()
{
read(n);
for(int i=1;i<=n;i++)
{
read(pos[i].x);
read(pos[i].y);
}
sort(pos+1,pos+1+n,cmp);
pos[1].num=++tim;
for(int i=2;i<=n;i++)
{
if(pos[i].y==pos[i-1].y) pos[i].num=tim;
else pos[i].num=++tim;
}
sort(pos+1,pos+1+n,comp);
int t=0;
ans=0;
size=tim;
add(pos[1].num,1);
for(int i=2;i<=n;i++)
{
ans+=i-1-query(pos[i].num);
add(pos[i].num,1);
if(pos[i].x==pos[i-1].x&&pos[i].num <pos[i-1].num) t++;
}
printf("%d",ans-t);
return 0;
}
阅读全文
0 0
- 51nod 1107 斜率小于0的连线数量 (树状数组+离线化)
- 51NOD 1107 斜率小于0的连线数量 坐标离散化+树状数组
- 51nod 1107 斜率小于0的连线数量 树状数组
- 51nod-斜率小于0的连线数量(树状数组+离散化)
- 51nod 1107 斜率小于0的连线数量
- 51nod 1107 斜率小于0的连线数量
- 51 nod 1107 斜率小于0的连线数量
- 51Nod 1107 斜率小于0的连线数量
- 斜率小于0的连线数量 51Nod
- 斜率小于0的连线数量 51NOD
- 51nod 1107 斜率小于0的连线的数量 (逆序数)
- 51nod-1107 斜率小于0的连线数量(逆序数+离散化)
- 51Nod-1107-斜率小于0的连线数量
- 51nod oj 1107 斜率小于0的连线数量 【类逆序数】
- 51nod 1107 斜率小于0的连线数量(逆序数)
- 1107 斜率小于0的连线数量
- 1107 斜率小于0的连线数量 求逆序对
- 斜率小于0的连线数量-归并排序
- CodeChef:Chef and Subarray Queries(线段树)
- 关于BeanUtils.copyProperties的用法和优缺点
- HDU 1114 Piggy-Bank
- Retrofit+多线程下载
- Codeforces 246E Blood Cousins Return 树上启发式合并
- 51nod 1107 斜率小于0的连线数量 树状数组
- SSM框架整合(IntelliJ IDEA + maven + Spring + SpringMVC + MyBatis)
- 【Scikit-Learn 中文文档】3 广义线性模型
- 关于Android Studio项目上传Github
- C++下创建并使用DLL文件 (VS2013 + win7)
- Python2 与 Python3 RSA 无填充加解密方式比较
- HTML基础
- 编译caffe和MTCNN出现的问题和解决
- android ART编译预优化