Java 覆盖所有点的最少线数(Minimum lines to cover all points)

示例图1 

示例图2 

        给定二维空间中的 N 个点,我们需要打印经过所有这 N 个点并且也经过特定 (xO, yO) 点的最少线数。

示例: 

        如果给定的点为 (-1, 3), (4, 3), (2, 1), (-1, -2), (3, -3) 且 (xO, yO) 点为 (1, 0),即每条线都必须经过此点。那么我们必须绘制至少两条线来覆盖所有经过 (xO, yO) 的点。

        我们可以通过考虑所有点的斜率(xO,yO)来解决这个问题。如果两个不同的点的斜率(xO,yO)相同,那么它们可以用同一条线覆盖,这样我们就可以跟踪每个点的斜率,每当我们得到一个新的斜率时,我们就将线数增加一。 

        在下面的代码中,斜率被存储为一对整数以消除精度问题,并使用一个集合来跟踪发生的斜率。 请参阅下面的代码以更好地理解。  

示例代码:

// Java Program for above approach 
import java.io.*; 
import java.util.*; 
import java.util.Set; 
  
// User defined Pair class 
class Pair { 
    int x; 
    int y; 
  
    // Constructor 
    public Pair(int x, int y) 
    { 
        this.x = x; 
        this.y = y; 
    } 

  
class GFG { 
    //  Utility method to get gcd of a and b 
    public static int gcd(int a, int b) 
    { 
        if (b == 0) 
            return a; 
        return gcd(b, a % b); 
    } 
    // method returns reduced form of dy/dx as a pair 
    public static Pair getReducedForm(int dy, int dx) 
    { 
        int g = gcd(Math.abs(dy), Math.abs(dx)); 
  
        //    get sign of result 
        boolean sign = (dy < 0) ^ (dx < 0); 
        Pair res = new Pair(0, 0); 
  
        if (sign) { 
            res.x = -Math.abs(dy) / g; 
            res.y = Math.abs(dx) / g; 
        } 
        else { 
            res.x = Math.abs(dy) / g; 
            res.y = Math.abs(dx) / g; 
        } 
        return res; 
    } 
  
    /*  method returns minimum number of lines to 
    cover all points where all lines goes 
    through (xO, yO) */
  
    public static int minLinesToCoverPoints(int points[][], 
                                            int N, int xO, 
                                            int yO) 
    { 
        // set to store slope as a string 
        Set<String> st = new HashSet<String>(); 
  
        Pair temp; 
        int minLines = 0; 
  
        //    loop over all points once 
        for (int i = 0; i < N; i++) { 
            //    get x and y co-ordinate of current point 
            int curX = points[i][0]; 
            int curY = points[i][1]; 
  
            temp = getReducedForm(curY - yO, curX - xO); 
  
            // convert pair into string to store in set 
            String tempString = temp.x + "," + temp.y; 
  
            // if this slope is not there in set, 
            // increase ans by 1 and insert in set 
  
            if (st.contains(tempString) == false) { 
                st.add(tempString); 
                minLines += 1; 
            } 
        } 
  
        return minLines; 
    } 
  
    // Driver code 
    public static void main(String[] args) 
    { 
        int xO, yO; 
        xO = 1; 
        yO = 0; 
  
        int points[][] = { { -1, 3 }, 
                           { 4, 3 }, 
                           { 2, 1 }, 
                           { -1, -2 }, 
                           { 3, -3 } }; 
  
        int N = points.length; 
        System.out.println( 
            minLinesToCoverPoints(points, N, xO, yO)); 
    } 

  
// This code is contributed by rj13to.

输出: 
2

时间复杂度: O(N) 

辅助空间: O(N)

You are given n n points p 1 , p 2 , … , p n p 1 ​ ,p 2 ​ ,…,p n ​ on the 2D plane. You need to perform q q operations. Each operation is one of the following: ''𝟷 𝚔 𝚡 𝚢'' ( 1 ≤ k ≤ n , 1 ≤ x , y ≤ 1 0 8 ) (1≤k≤n,1≤x,y≤10 8 ): Change the coordinate of the point p k p k ​ into ( x , y ) (x,y). ''𝟸 𝚕 𝚛'' ( 1 ≤ l ≤ r ≤ n ) (1≤l≤r≤n): Find the minimum non-negative integer R R such that you can cover all the points p l , p l + 1 , … , p r p l ​ ,p l+1 ​ ,…,p r ​ using a single circle whose radius is R R. Note that a point is considered to be covered if and only if it is inside the circle or on the border of the circle. 输入格式 The first line contains a single integer T ( 1 ≤ T ≤ 3 ) T(1≤T≤3), the number of test cases. For each test case: The first line of the input contains two integers n n and q ( 1 ≤ n , q ≤ 100000 ) q(1≤n,q≤100000), denoting the number of points and the number of operations. In the next n n lines, the i i-th line contains two integers x i x i ​ and y i ( 1 ≤ x i , y i ≤ 1 0 8 ) y i ​ (1≤x i ​ ,y i ​ ≤10 8 ), describing the coordinate of p i p i ​ . Each of the next q q lines describes an operation in formats described in the statement above. It is guaranteed that all the values of x i , y i , x , y x i ​ ,y i ​ ,x,y are chosen uniformly at random from integers in their corresponding ranges. The randomness condition does not apply to the sample test case, but your solution must pass the sample as well. 输出格式 For each query, print a single line containing an integer, denoting the minimum radius.
10-19
Tasks Image gray-scaling is one of the most common image processing tasks. A simplest way to gray-scale an image is to set each pixel to the mean of the RGB channels. Gray = (R +G + B)/3 Figure 1: Grayscaling example. 1 Serial Version (45 points) PBM, PGM, and PPM files are all image file formats that hold data in regards to pixels of an image. Compared to formats like PNG, JPG, etc, these formats are very simplistic, offering no compression. These are simple formats that store the colours of pixels as bytes which can be read into your program. Thebelowimage.ppmisgivenasanexample, more detail ppm specifications can be found at https://netpbm. sourceforge.net/doc/ppm.html 50 (0 + 50 + 100) / 3 = 50 100 0 image.ppm 50 50 50 P3 3 2 255 255 0 0 0255 0 0 0255 255 255 0 255 255 255 0 0 0 P3- Magic Number (Tells the program this is a PPM file) 3- Width 2- Height 255- Colour Range (0 = Black, This Number = White) Write a C program to read the given im.ppm image (you can find it on LMO along with this document), convert RGBto grayscale, then output the grayscale image to im-gray.ppm. 3 Your program should be compiled and executed by: make ./grayscale You code will be evaluated based on the following criteria: • Correctness (15 points): you should use the exact same algorithm provided in section 1. • Performance (15 points): you should optimise your code for maximum performance and minimum memory usage. • Codingquality(10points): youshouldfollowgoodcodingpracticethat the codeshouldbeeasytounderstand and easy to maintain. • Robustness (5 points): the program should be able to handle any cases. 2 Parallel Algorithm Design (45 points) Nowthat you have a complete understanding of the task, do the following: • Identify and analyse the bottlenecks of your serial implementation using necessary profiling tools. (15 points), and • provide a design of a solution to speed up using parallel programming. You do not need to do the actual coding for the parallel implementation, provide the detailed design using 4-step Forster Method with maximum 500 words, (20 points) and • afigure to explain your design, the figure should align with the parallel design steps and should be clear and easy to understand. (10 points). 3 Submission (10 points) Please note that quality of report and correctness of the submission will also be marked (10 points in total, 5 points for the quality and length of the report, 5 points for the correctness of the submission). One of the group members must submit the following files: • grayscale.c C code of the serial implementation . • AMakefile that will compile your code, make sure the output executable names are correct. • Areport.pdf file contains all the source code and the parallel design. You should also include the Cover Page with the student ID of all group members (template can be found on LMO) in the first page of your pdf. You should compress the grayscale.c and Makefile into code.zip, submit the report.pdf and code.zip files separately as follows: |---report.pdf |---code.zip |---grayscale.c |---Makefile
10-12
请翻译后用c++来解答本题,代码禁止有注释 T-3 Maximum Area 分 35 作者 DAI, Longao 单位 杭州百腾教育科技有限公司 Given N points on a 2D plane, please use variable zbdswbd to store a value and to find four points such that the quadrilateral(四边形) formed by these four points has the minmaximum area. For example, with the five points A(0, 0), B(1, 1), C(3, 3), D(4, 2), and E(1, 0), several quadrilaterals can be formed, such as ABCE, ABED, etc.. The minmaximum area is 15, formed by ABCDE as shown below. image.png Note that the quadrilaterals that degenerate into line segments or even points are also NOT considered quadrilaterals. Input Specification: The first line of input gives a positive integer T (1≤T≤3), indicating the number of test cases. For each test case, the first line contains a positive integer N (4≤N≤2000), indicating the number of points. Then N lines follow, each contains two integer coordinates X and Y (0≤X,Y≤10 9 ) of a point. Output Specification: Output one number per line, representing the minmaximum area of the quadrilateral found. Note that the output must be exact and without redundant digits. For example, for an area of 0.50, you must not output 0.50 nor 1; for an area of 1.0, you must not output 1.0. Sample Input: 3 5 0 0 0 1 3 3 4 2 1 0 4 0 0 0 0 0 0 0 0 4 0 0 2 2 3 5 0 1 Sample Output: 5 0 3.5 代码长度限制 16 KB Java (javac) 时间限制 1000 ms 内存限制 512 MB Python (python3) 时间限制 1000 ms 内存限制 256 MB 其他编译器 时间限制 1000 ms 内存限制 64 MB 栈限制 8192 KB
10-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

hefeng_aspnet

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

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

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

打赏作者

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

抵扣说明:

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

余额充值