洛谷 P2665 [USACO08FEB]连线游戏Game of Lines

题目背景

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。

USACO2008 Feb T1

思路:数据范围不大,只要求出所有的斜率然后排序判重就可以了。在特判不存在斜率的情况,也就是与坐标轴垂直的情况。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm> 
using namespace std;
int n,cnt,ans=1;
double a[205],b[205],c[40005];
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%lf%lf",&a[i],&b[i]);
    for(int i=1;i<n;i++)
        for(int j=i+1;j<=n;j++)
            if(a[i]==a[j])    c[++cnt]=0;
            else if(b[i]==b[j])    c[++cnt]=10000;
            else    c[++cnt]=(a[i]-a[j])/(b[i]-b[j]);
    sort(c+1,c+cnt+1);
    for(int i=2;i<=cnt;i++)
        if(c[i]-c[i-1]>1e-10) ans++;
    printf("%d",ans);
}

夫妻忆

枯眼望遥山隔水,往来曾见几心知?

壶空怕酌一杯酒,笔下难成和韵诗。

途路阻人离别久,讯音无雁寄回迟。

孤灯夜守长寥寂,夫忆妻兮父忆儿。

细雨斜风作晓寒。淡烟疏柳媚晴滩。入淮清洛渐漫漫。 雪沫乳花浮午盏,蓼茸蒿笋试春盘。人间有味是清欢。
原文地址:https://www.cnblogs.com/cangT-Tlan/p/8149964.html