Codeforces Round #558 (Div. 2) C1. Power Transmission (Easy Edition)

本文探讨了如何计算由n个点形成的电力线交叉对数,通过数学和编程方法,解决了一个复杂的问题,即确定任意两个电力线是否相交,并统计相交对的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

This problem is same as the next one, but has smaller constraints.

It was a Sunday morning when the three friends Selena, Shiro and Katie decided to have a trip to the nearby power station (do not try this at home). After arriving at the power station, the cats got impressed with a large power transmission system consisting of many chimneys, electric poles, and wires. Since they are cats, they found those things gigantic.

At the entrance of the station, there is a map describing the complicated wiring system. Selena is the best at math among three friends. He decided to draw the map on the Cartesian plane. Each pole is now a point at some coordinates (xi,yi)(xi,yi). Since every pole is different, all of the points representing these poles are distinct. Also, every two poles are connected with each other by wires. A wire is a straight line on the plane infinite in both directions. If there are more than two poles lying on the same line, they are connected by a single common wire.

Selena thinks, that whenever two different electric wires intersect, they may interfere with each other and cause damage. So he wonders, how many pairs are intersecting? Could you help him with this problem?

Input

The first line contains a single integer nn (2≤n≤502≤n≤50) — the number of electric poles.

Each of the following nn lines contains two integers xixi, yiyi (−104≤xi,yi≤104−104≤xi,yi≤104) — the coordinates of the poles.

It is guaranteed that all of these nn points are distinct.

Output

Print a single integer — the number of pairs of wires that are intersecting.

Examples

input

Copy

4
0 0
1 1
0 3
1 2

output

Copy

14

input

Copy

4
0 0
0 2
0 4
2 0

output

Copy

6

input

Copy

3
-1 -1
1 0
3 1

output

Copy

0

Note

In the first example:

In the second example:

Note that the three poles (0,0)(0,0), (0,2)(0,2) and (0,4)(0,4) are connected by a single wire.

In the third example:

题目链接:http://codeforces.com/contest/1163/problem/C1

题意:给出n个点,找出每个点连成的直线的相交的对数。

思路:找出两两点之间直线,找出两两直线的交点

#include<algorithm>
#include<string.h>
#include<stdio.h>
#include<map>
using namespace std;
int n,tot=0;
pair<int,int>dot[55];
map<pair<double,double>,int>mp;//记录交点
map<pair<double,double>,int>conghe;//记录重边
struct node
{
    double A,B,C;
    node() {};
    node(double _A,double _B,double _C)
    {
        A=_A;
        B=_B;
        C=_C;
    }
} line[55*55];
int cut=1;
void F(int i,int j)
{
    int x1=dot[i].first,y1=dot[i].second;
    int x2=dot[j].first,y2=dot[j].second;
    //一般式为Ax+By+C=0
    int G=__gcd(y1-y2,x1-x2);
    double A=(y1-y2)/G,B=(x1-x2)/G;//一定要除gcd
    //1x+0y+1=0,2x+0y+2=0,是同一条边,不除gcd的话就会判成不同边
    double C=A*x1-B*y1;
    if(conghe[make_pair(-A/B,C)]) return ;//判重边-A/B是斜率K
    line[++tot]=node(A,B,C);
    conghe[make_pair(-A/B,C)]=1;
}
void P(int i,int j)
{
    double A1=line[i].A,B1=line[i].B,C1=line[i].C;
    double A2=line[j].A,B2=line[j].B,C2=line[j].C;
    double x=(B1*C2-B2*C1)/(A1*B2-A2*B1);
    double y=(A1*C2-A2*C1)/(B1*A2-B2*A1);//(x,y)是两条线的交点
    if(A1*B2==A2*B1||-A1/B1==-A2/B2)//如果直线平行
    {
        return ;
    }
    mp[make_pair(x,y)]++;
}
int main()
{
    scanf("%d",&n);
    for(int i=1; i<=n; i++)
        scanf("%d%d",&dot[i].first,&dot[i].second);
    for(int i=1; i<=n; i++)
    {
        for(int j=i+1; j<=n; j++)
        {
            F(i,j);
        }
    }
    for(int i=1; i<=tot; i++)
    {
        for(int j=i+1; j<=tot; j++)
        {
            P(i,j);
        }
    }
    int ans=0;
    for(map<pair<double,double>,int>::iterator it=mp.begin(); it!=mp.end(); it++)//遍历map
    {
        ans+=it->second;
    }
    printf("%d\n",ans);
}

题解代码............................

题解出处:https://codeforces.com/blog/entry/66943

#include <cstdio>
#include <map>
#include <set>
#include <utility>
const int N = 1001;
int x[N], y[N];
std::map<std::pair<int,int>,std::set<long long>> slope_map;

int gcd(int a, int b) 
{
	if (a == 0)
		return b;
	return gcd(b % a, a);
}

int main()
{
	int n; scanf("%d", &n);
	for (int i = 1; i <= n; ++i)
		scanf("%d%d", &x[i], &y[i]);
	long long total = 0, res = 0;
	for (int i = 1; i <= n - 1; ++i)
		for (int j = i + 1; j <= n; ++j)
		{
			int x1 = x[i], y1 = y[i], x2 = x[j], y2 = y[j];
			// construct a line passing through (x1, y1) and (x2, y2)
			// line is expressed as equation ax - by = c with constant a, b, c 
			int a = y1 - y2, b = x1 - x2;
			// simplify equation
			int d = gcd(a, b); a /= d, b /= d;
			if (a < 0 || (a == 0 && b < 0))
			{
			    a = -a;
			    b = -b;
			}
			// lines with the same slope (same a, b) are stored in a map
			std::pair<int,int> slope(a, b);
			long long c = 1LL * a * x1 - 1LL * b * y1;
			if (!slope_map[slope].count(c))
			{
				++total;
				slope_map[slope].insert(c);
				// if this line is new, it intersects every line with different slope
				res += total - slope_map[slope].size();
			}
		}
	printf("%lld\n", res);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值