1107 斜率小于0的连线数量

1107 斜率小于0的连线数量
基准时间限制: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

/* ***********************************************
Author        :xdlove
Created Time  :2016年05月05日 星期四 20时59分59秒
File Name     :2016_05_05_51nod_1391.cpp
 ************************************************ */

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <memory.h>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>

using namespace std;
const int N = 5e4 + 5;
vector<int> vec;
int c[N << 1];

struct Node
{
    int x,y;
    void read()
    {
        scanf("%d %d",&x,&y);
    }
    void make_id()
    {
        x = get_id(x);
        y = get_id(y);
    }
    int get_id(int x)
    {
        return lower_bound(vec.begin(),vec.end(),x) - vec.begin() + 1;
    }
    bool operator < (const Node &a) const 
    {
        if(x != a.x) return x < a.x;
        return y < a.y;
    }
}p[N];

void add(int x,int n)
{
    while(x <= n)
    {
        c[x]++;
        x += x & -x;
    }
}

int get_sum(int x,int n)
{
    int s = 0;
    while(x > 0)
    {
        s += c[x];
        x -= x & -x;
    }
    return s;
}

void solve()
{
    int n; 
    cin >> n;
    for(int i = 0; i < n; ++i)
    {
        p[i].read();
        vec.push_back(p[i].x);
        vec.push_back(p[i].y);
    }
    sort(vec.begin(),vec.end());
    vec.erase(unique(vec.begin(),vec.end()),vec.end());
    for(int i = 0; i < n; ++i) p[i].make_id();
    sort(p,p + n);
    int ans = 0;
    for(int i = 0; i < n; ++i)
    {
        int tp = get_sum(p[i].y,vec.size());
        ans += i - tp;
        add(p[i].y,vec.size());
    }
    cout << ans << endl;
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    solve();
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值