A Nod 1107 slope less than 0 of the number of lines (converted to merge for reverse order number or direct tree array, super detailed solving!!!) )

Source: Internet
Author: User

1107 number of lines with slope less than 0 base time limit: 1 seconds space limit: 131072 KB Score: 40 Difficulty: 4-level algorithm There is a C (n,2) line between n points in the two-dimensional plane. The number of lines in this C (n,2) line that have a slope less than 0. A point on a two-dimensional plane, which can be represented as (x, y) according to the corresponding X-Y coordinate. For example: (2,3) (3,4) (1,5) (4,6), where (1,5) the line slope of the same (2,3) (3,4) < 0, so the number of lines with a slope of less than 0 is 2. Input
Line 1th: 1 number n,n is the number of points (0 <= N <= 50000) The first 2-n + 1 lines: The coordinates of N points, the coordinates are integers. (0 <= X[i], Y[i] <= 10^9)
Output
The number of lines with the output slope less than 0. (2,3) (2,4) and (2,3) (3,3) are not counted in 2 cases.
Input example
42 33 41) 54 6
Output example
2
Li Tao(topic Provider)topic Meaning:
ask you how many of the two points with a slope of less than 0Analysis:Method 1: Direct tree-like array solution
It's similar to POJ 2352 .
The problem is to find the number of points in the lower left corner of a point, the question is to find the number of points in the upper left corner
The input is by the rule (in ascending order of Y, y equals, x small in front)
so this is the beginning of the problem to follow this rule first line order
if we directly ask for the number of points in the upper left corner of a point, we can change the problem directly, but there is a bit of trouble here:
The problem is in the bottom corner.
but that's not the problem .
so a little trouble .
but we can transform our thinking .
because at first, it was sorted by that sort of rule.
so the current point is definitely the largest point of Y, so the point is the dividing point
and the point at which the point has a slope greater than or equal to 0 or if the skew column does not exist is definitely at the lower left corner of the point
so this time, the number of all points (excluding the current point) minus the number of points in the lower left corner of the current point (including the lower left corner of the border)
is the number of points with slopes less than 0
That is, the number of points in the lower right corner of the point (excluding the boundary, the bottom left corner of the boundary is counted)Another problem is that the data is too large, the C array cannot be opened and needs to be discretized
so
first, the input data is discretized according to the X, because every time getsum is passed x in
Specific operation:
The data is sorted first by x ascending, x equals Y and small by the previous rule
because every time getsum is X-in, it's sorted by X-precedence rules.
after sorting, give a number in order.
this is called discretization.
and then the getsum passed in the numbers.
In particular, the size order relationship between numbers is the same as the original size order of x .
only the range of data is compressed, but the relationship between the data is unchanged .
so it's possible.
this is discretization .after discretization is complete
It's just like the star problem, according to Y ascending, y equals x small put the preceding rule sort
and then every time I getsum, I get the number of the lower left corner .
the total number of current points (except the current point) minus the lower left corner of the current point
is the number in the lower right corner of the current point
is the number of points with a slope less than 0 of the current point
then add each getsum answer to the number of point pairs with slopes less than 0.
#include <queue>#include<Set>#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>#include<cmath>using namespacestd;#defineMax_v 500005structnode{intx, y; intnum;} P[MAX_V];BOOLCMP1 (node A,node b)//A tree array operation requires{    if(a.y!=b.y)returna.y<b.y; Else        returna.x<b.x;}BOOLCMP2 (node A,node b)//discretization requires{    if(a.x!=b.x)returna.x<b.x; Else        returna.y<b.y;}intC[max_v];intMaxx;intLowbit (intx) {    returnx& (-x);}voidUpdateintXintd) {     while(x<=Maxx) {C[x]+=D; X+=lowbit (x); }}intGetsum (intXintnum) {    intres=0;  while(x>0) {res+=C[x]; X-=lowbit (x); }    returnNum-res;//The total number of current points minus the lower-left corner of the point is the number of lower-right corner points}intMain () {intN;  while(~SCANF ("%d",&N) {memset (c,0,sizeof(c));  for(intI=0; i<n; i++) {scanf ("%d%d",&p[i].x,&p[i].y); }        //discretization ofSort (p,p+N,CMP2); intk=1; p[0].num=K;  for(intI=1; i<n; i++) {P[i].num=++K; } Maxx=K; //tree-like arraySort (p,p+N,CMP1); Long Longans=0;  for(intI=0; i<n; i++) {ans+=getsum (P[i].num,i);//Note is I, not i+1, because the total number of current points does not include the current point itselfUpdate (P[i].num,1); } printf ("%i64d\n", ans); }    return 0;}/*topic Meaning: Ask you slope is less than 0 of the two points of how many analysis: This problem with POJ 2352 is very similar to the question is to find the number of points in the lower left corner, the question is to find the number of points in the upper left corner of the point of the input is by the rule (in accordance with the ascending y, y equal, x small in front) so this is the beginning of the question to follow this rule first order if we directly ask for a point in the upper left corner of the number of points, directly to change the problem can be, But there is a bit of trouble: The problem is in the bottom corner of the situation is also counted, but this problem is not so a little trouble, but we can change the idea, because the collation is already sorted in the beginning, so the current point is definitely the y biggest point, Then the point at which the point is cut and the slope of the point is greater than or equal to 0, or the dot does not exist, is definitely at the bottom left corner of the point, then the number of all points at this time (excluding the current point subtracting the number of points in the lower-left corner of the current point (including the boundary of the lower-left corner) is the number of points that have a slope of less than 0, which is the amount of the lower-right corner of the point (excluding the boundary , the front left corner of the border has been counted) there is another problem is that the data is too large, C array can not be opened, it is necessary to discretization the input data in accordance with the X discretization, because every time the getsum is to pass X into the specific operation: the data in accordance with the X ascending, X is equal to Y small put in front of the rule sort because each getsum is passed x in, so the order of precedence according to X is sorted and given a number according to the sequence. This is the so-called discretization and then Getsum passed in is the number of specific understanding, The size order relationship between numbers and X is the same as the original size order relationship, but the data range is compressed, but the relationship between the data is not changed, so it is possible that this is discretized discretization is almost the same as the star question, according to the Y Ascending, Y equals x small put the preceding rule sort and then each time the getsum gets the number of points in the lower left corner the current point total (except the current point) minus the current point lower left corner the number is the number of points at the bottom right of the current point is the number of dots with the current point slope of less than 0 and then adds each getsum answer to the number of point pairs with slopes less than 0. */

Idea two:

Convert to reverse order number, then merge to reverse

start with the formula of the slope
(XI-XJ)/(YI-YJ) Number of point pairs for <0
now let's start with the X ascending, x the same, and Y small, put the rules in the front.
Suppose I<j
then XI-XJ must be less than 0.
then the slope requirement is less than 0, which requires yi-yj greater than 0
is the order of the sequence of all Y components after the sequence
its reverse number is the number of slopes less than 0 points!
so let's start with that rule.
then take y out to form an array
to find the inverse number of series by merging sort
Consolidation can be the reason for reverse order:in the process of merging and sorting, it is more critical to pass recursion,
merge two sorted arrays,
at this point, if a[i] > A[j], then the number between I and M is greater than a[j],
Merge A[j] before A[i], this time also produces the m-i+1 number of reverse order,
a situation less than or equal to is not generated.
#include <stdio.h>#include<memory>#include<algorithm>using namespacestd;#defineMax_v 500005typedefLong LongLL;structnode{intx, y;} P[MAX_V];intA[max_v];BOOLCMP (node A,node b) {if(a.x!=b.x)returna.x<b.x; Else        returna.y<b.y;}intTemp[max_v]; LL ans;voidMerintSintMintt) {    intI=s; intj=m+1; intk=s;  while(i<=m&&j<=t) {if(a[i]<=A[j]) {Temp[k++]=a[i++]; }Else{ans+=j-k;//find the number of reverse ordertemp[k++]=a[j++]; }    }     while(i<=m) {temp[k++]=a[i++]; }     while(j<=t) {temp[k++]=a[j++]; }}voidCopintSintt) {     for(inti=s;i<=t;i++) A[i]=temp[i];}voidMegsort (intSintt) {    if(s<t) {intM= (s+t)/2;        Megsort (S,M); Megsort (M+1, T);        Mer (S,M,T);    Cop (S,T); }}intMain () {intN;  while(~SCANF ("%d",&N)) {if(n==0)             Break; Ans=0;  for(intI=0; i<n;i++) scanf ("%d%d",&p[i].x,&p[i].y); Sort (p,p+n,cmp);  for(intI=0; i<n;i++) {A[i]=p[i].y; } megsort (0, N-1); printf ("%lld\n", ans); }    return 0;}/*Starting from the formula of the slope (XI-XJ)/(YI-YJ) The number of point pairs of <0 now we first follow the x ascending, X is the same as the Y small put the preceding rules in order to assume i<j then XI-XJ must be less than 0, then the slope requirement is less than 0, It is required that yi-yj greater than 0 is to be ordered after the sequence of all Y-sequences of the reverse order of its number is the slope is less than 0 points of the number of pairs! So first in accordance with that rule and then take the Y out to form an array with the order of the inverse number of the sequence of the merger can be the reason for the reverse order: in the process of merging sorting, the key is through the recursive, the two already sorted array merge, at this time, if a[i] > A[j], Then the number between I to M is greater than a[j], and the merge time A[j] before A[i], this time also produces the m-i+1 reverse order number, but less than equals the situation does not produce. */

A Nod 1107 slope less than 0 of the number of lines (converted to merge for reverse order number or direct tree array, super detailed solving!!!) )

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.