Circle (sorting + modifying step size) with 1278 separation of 51nod, 51nod step size

Source: Internet
Author: User

Circle (sorting + modifying step size) with 1278 separation of 51nod, 51nod step size

Question meaning:

Http://www.51nod.com/onlineJudge/questionCode.html! ProblemId = 1278

There are N circles on the plane, and their centers are all on the X axis. The center and radius of all circles are given, and how many pairs of circles are separated. For example, four circles are located at the positions of 1, 2, 3, and 4, respectively. The radius is 1, 1, 2, and 1 respectively. Then {1, 2}, {1, 3} {2, 3} {2, 4} {3, 4} all have intersection points. Only {1, 4} are separated. Input
Row 1st: N indicates the number of circles (1 <= N <= 50000) 2nd-N + 1 row: 2 numbers in each row P. spaces are used to separate the numbers in R, P indicates the center of the circle, and R indicates the circle radius (1 <= P, R <= 10 ^ 9)
Output
Number of pairs of separation circles in the output.
Input example
41 12 13 24 1
Output example
1

Question Analysis:

/**
* Method 1
* The Circle is divided into left and right short points, represented by line segments. Now it is deformed to the intersection of line segments.
* Left endpoint left = c-r, right endpoint right = c + r, sorted in ascending order of left endpoint.
Sort by the right endpoint in ascending order, or scan. For each line segment I, when
* If the left endpoint of Line Segment j is greater than the right endpoint of I, all future line segments are separated from (I ).
* The time complexity is O (nlog (n) + n ^ 2)
* The data for this question is 50000, and the normal O (n * n) Time-out is certainly
* Now we can consider optimizing the algorithm by changing the step size (only reducing the coefficient, but sometimes it is good ),
* I optimized the query with another step of 100. If the current j satisfies the condition, the first step is met.
* The condition j must be less than 100 before j. You only need to query the first 100 before j,
* Time complexity O (n * log (n) + n * (n/100 + 100); for this question, we have reduced the coefficient of n ^ 2 by 100 times.
**************************************** *********************************
* Method 2 thanks to Dava
* Place the start and end points of all circles in an array and sort them by size (expressed by the left and right vertices of the circle)
* If the starting point of a circle is equal to the ending point of another circle, the starting point is placed in front.
* Define struct
* Struct {
* Long d; // a node of a line segment
* Int tmp; // mark start point (0) and end point (1 ),
*}
* Record all the start and end points in a quick way,
* If num = the number of circles, the start num -- and the end sum + = num
*/

AC code, method 1

# Include <iostream >#include <string> # include <algorithm> using namespace std; struct node {int left, right ;}; node p [50005]; int cmp (node a, node B) {if (. left <B. left) return 1; else if (. left = B. left &. right <B. right) return 1; return 0;} int main () {int c, r, n; while (cin> n) {for (int I = 0; I <n; I ++) {cin> c> r; p [I]. left = c-r; p [I]. right = c + r;} sort (p, p + n, cmp); int I, j, sum = 0, k; for (I = 0; I <n; I ++) {for (j = I + 100; j <n; j + = 100) {// The step size is 100 if (p [j]. left> p [I]. right) break;} if (j> n) j = n; for (k = J-1; k> 0 & k> j-101; k --) {// scan between 100 elements if (p [k]. left <= p [I]. right) {sum + = n-(k + 1); break ;}}cout <sum <endl ;}return 0 ;}

AC code, method 2

# Include <iostream> # include <algorithm> using namespace std; struct node {long d; // int flag of the Line Segment node; // mark the start point (0) and end (1),} p [100005]; int cmp (node a, node B) {if (. d <B. d) return 1; else if (. d = B. d &. flag = 0) return 1; return 0;} int main () {int c, r, n; while (cin> n) {int k = 0; for (int I = 0; I <n; I ++) {cin> c> r; p [k]. d = c-r; p [k ++]. flag = 0; // start point p [k]. d = c + r; p [k ++]. flag = 1; // end} // cout <k <endl; sort (p, p + k, cmp); int sum = 0, num = n; for (int I = 0; I <k; I ++) {// cout <p [I]. d <"" <p [I]. flag <endl; if (p [I]. flag = 0) num --; else if (p [I]. flag = 1) sum + = num;} cout <sum <endl;} return 0 ;}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.