D. Pair Of Lines

本文探讨了一个几何问题:是否存在两条直线能够覆盖平面上给定的所有整数坐标点。通过枚举三个点并检查它们是否共线来寻找解决方案。如果找到两组共线的点,则问题得解。
摘要由CSDN通过智能技术生成

D. Pair Of Lines
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given n points on Cartesian plane. Every point is a lattice point (i. e. both of its coordinates are integers), and all points are distinct.

You may draw two straight lines (not necessarily distinct). Is it possible to do this in such a way that every point lies on at least one of these lines?

Input
The first line contains one integer n (1 ≤ n ≤ 105) — the number of points you are given.

Then n lines follow, each line containing two integers xi and yi (|xi|, |yi| ≤ 109)— coordinates of i-th point. All n points are distinct.

Output
If it is possible to draw two straight lines in such a way that each of given points belongs to at least one of these lines, print YES. Otherwise, print NO.

Examples
inputCopy
5
0 0
0 1
1 1
1 -1
2 2
outputCopy
YES
inputCopy
5
0 0
1 0
2 1
1 1
2 3
outputCopy
NO
Note
In the first example it is possible to draw two lines, the one containing the points 1, 3 and 5, and another one containing two remaining points.
存在n个点,判断连两条直线是否可以使得这些点都在这两条直线上,可以这样考虑,假定存在解,则1可设其在直线A上,而2有两种情况,要么在A上,要么在B上,3也有两种情况,要么在A上,要么在B上,如果这三个点在同一条线上的话,必定是可以确定这条直线的,因为如果说不把这条直线确定下来的话,这三个点中必定存在1个点不能被经过,由此可想到,只要找到两点确定一条直线,如果说这条直线经过了另外的一个点则必定确定该直线。因为只存在两条直线,如果说A直线经过1点的话,2,3点要么存在于A,要么存在于B,1,2,3点等价起来的话,含义在于,1,2,3点至少存在两个点在同一条直线上,枚举一下这3种情况即可,即1,2在同一直线,1,3在同一直线,2,3在同一直线。

#include<iostream>
#include<cstdio>
#include<string>
#include <cstring>
#include <stack>
#include <algorithm>
#include <map>g
#include <queue>
#include <set>
#include <cmath>
using namespace std;
#define INF 0x3f3f3f3f
#define mod 998244353
typedef struct point{
    long long x, y;
    void getvalue(point& a,point& b) {
        x = a.x - b.x;
        y = a.y - b.y;
    }
}Vector;
point s[100005];
bool flag[100005];
int n;
bool cross(const Vector& a, const Vector& b) {
    return (a.x * b.y - a.y * b.x) == 0;
}
bool cmp(const point& a, const point& b) {
    return a.x * b.y < a.y * b.x;
}
void test() {
    for (int i = 0; i < n; ++i) {
        printf("%d:\n", i);
        for (int j = i + 1; j < n; ++j) {
            for (int k = j + 1; k < n; ++k) {
                Vector a, b;
                a.getvalue(s[i], s[j]);
                b.getvalue(s[i], s[k]);
                if (cross(a, b)) {
                    printf("%d %d\t", j, k);
                }
            }
        }
    }
}
bool judge(int x,int y) {
    Vector a, b;
    a.getvalue(s[x], s[y]);
    memset(flag, false, sizeof(flag));
    flag[x] = flag[y] = true;
    for (int i = 0; i < n; ++i) {
        b.getvalue(s[x], s[i]);
        if (cross(a, b)) {
            flag[i] = true;
        }
    }
    int u[2], k = 0;
    for (int i = 0; i < n; ++i) {
        if (!flag[i]) {
            u[k++] = i;
            if (k == 2) {
                break;
            }
        }
    }
    if (k != 2) {
        return true;
    }
    flag[u[0]] = flag[u[1]] = true;
    a.getvalue(s[u[0]], s[u[1]]);
    for (int i = 0; i < n; ++i) {
        b.getvalue(s[u[0]], s[i]);
        if (!cross(a, b) && !flag[i]) {
            return false;
        }
    }
    return true;
}
int main() {
    cin >> n;
    for (int i = 0; i < n; ++i) {
        scanf("%I64d%I64d", &s[i].x, &s[i].y);
    }
    if (n < 5) {
        cout << "YES" << endl;
    }
    else {
        //1与2在同一条直线上
        if (judge(1, 2) || judge(1, 3) || judge(2, 3)) {
            cout << "YES" << endl;
        }
        else {
            cout << "NO" << endl;
        }
    }
    return 0;
}
好的,那么根据实验要求,我们需要先创建一个English.txt文件,其中包含多行选择题信息,每行都包含了一个选择题的题干、可选答案、正确答案,且题干及每个答案之间都用“#”分隔,最后一行为字符串end。例如: He cut cloth with__sissors#a couple of#a pair of#two#a#a pair of She is a__doctor. A. woman B. man C. child D. girl The baby is__the bed. A. in B. on C. at D. to The sun__in the east. A. rose B. rise C. rises D. raised end 然后我们需要创建一个选择题类ChoiceQuestion,包含成员变量:题干(字符串)、可选答案(字符串数组)、正确答案(字符串),并包含构造函数和成员函数:输出题目,判断答案是否正确。输出题目的格式如下: He cut cloth with__sissors. A. a couple of B. a pair of C. two D. a 接着,我们需要编写程序,读取English.txt文件,定义一个ChoiceQuestion对象数组,每次读一行数据构造一个ChoiceQuestion对象,调用ChoiceQuestion的输出题目函数输出题目。下面是一个可能的实现: import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class EnglishTest { public static void main(String[] args) { // 读取文件内容到List中 List<String> lines = new ArrayList<>(); try (BufferedReader br = new BufferedReader(new FileReader("English.txt"))) { String line; while ((line = br.readLine()) != null) { lines.add(line); } } catch (IOException e) { e.printStackTrace(); } // 将每行内容构造成ChoiceQuestion对象,并输出题目 for (String line : lines) { if (!line.equals("end")) { String[] parts = line.split("#"); String question = parts[0]; String[] options = new String[4]; for (int i = 0; i < 4; i++) { options[i] = parts[i + 1]; } String answer = parts[5]; ChoiceQuestion cq = new ChoiceQuestion(question, options, answer); cq.showQuestion(); } } } } class ChoiceQuestion { private String question; private String[] options; private String answer; public ChoiceQuestion(String line) { String[] parts = line.split("#"); question = parts[0]; options = new String[4]; for (int i = 0; i < 4; i++) { options[i] = parts[i + 1]; } answer = parts[5]; } public ChoiceQuestion(String question, String[] options, String answer) { this.question = question; this.options = options; this.answer = answer; } public void showQuestion() { System.out.println(question); for (int i = 0; i < 4; i++) { System.out.println((char)('A' + i) + ". " + options[i]); } } public boolean checkAnswer(String userAnswer) { return userAnswer.equals(answer); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值