Luogu2665[USACO08FEB] 连线游戏

原题链接:https://www.luogu.org/problemnew/show/P2665

连线游戏

题目背景

Farmer John最近发明了一个游戏,来考验自命不凡的贝茜。

题目描述

Farmer John has challenged Bessie to the following game: FJ has a board with dots marked at N (2 ≤ N ≤ 200) distinct lattice points. Dot i has the integer coordinates Xi and Yi (-1,000 ≤ Xi ≤ 1,000; -1,000 ≤ Yi ≤ 1,000).

Bessie can score a point in the game by picking two of the dots and drawing a straight line between them; however, she is not allowed to draw a line if she has already drawn another line that is parallel to that line. Bessie would like to know her chances of winning, so she has asked you to help find the maximum score she can obtain.

游戏开始的时 候,FJ会给贝茜一块画着N (2 <= N <= 200)个不重合的点的木板,其中第i个点 的横、纵坐标分别为X_i和Y_i (-1,000 <= X_i <=1,000; -1,000 <= Y_i <= 1,000)。 贝茜可以选两个点画一条过它们的直线,当且仅当平面上不存在与画出直线 平行的直线。游戏结束时贝茜的得分,就是她画出的直线的总条数。为了在游戏 中胜出,贝茜找到了你,希望你帮她计算一下最大可能得分。

输入输出格式
输入格式:

第1行: 输入1个正整数:N

第2..N+1行: 第i+1行用2个用空格隔开的整数X_i、Y_i,描述了点i的坐标

输出格式:

第1行: 输出1个整数,表示贝茜的最大得分,即她能画出的互不平行的线段数

输入输出样例
输入样例#1:

4
-1 1
-2 0
0 0
1 1

输出样例#1:

4  

说明

贝茜能画出以下4种斜率的直线:-1,0,1/3以及1。

题解

巨水,只需要O(n^2)枚举所有点算出所有可能的斜率,最后扫描一边去重+统计即可AC

注意垂直的情况,算斜率的时候会除以0然后RE

代码
#include<bits/stdc++.h>
#define db double
using namespace std;
struct pt{int x,y;};
pt hh[205];
db ang[40005];
int main()
{
    int n,top=0,ans=0;
    scanf("%d",&n);
    for(int i=1;i<=n;++i)
    scanf("%d%d",&hh[i].x,&hh[i].y);
    for(int i=1;i<=n;++i)
    for(int j=i+1;j<=n;++j)
    {
        if(hh[i].x==hh[j].x) ang[++top]=1e9;
        else ang[++top]=1.0*(hh[j].y-hh[i].y)/(hh[j].x-hh[i].x);
    }
    sort(ang+1,ang+1+top);
    for(int i=1;i<=top;++i)
    if(fabs(ang[i]-ang[i-1])>1e-8) ans++;
    printf("%d",ans);
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ShadyPi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值