UVA 11123-counting trapizoid (counting problem + tolerance)

Source: Internet
Author: User
Tags cas

UVA 11123-counting trapizoid

Topic links

Test instructions: Given some points, do not repeat, find a total of a few trapezoidal

Idea: First put all two points into a straight line to find out, and then sort, slope the same c 2 n , and then deduct the overlapping straight line condition and the length equal case (this is parallelogram or rectangle), because the deduction will be repeated deduction of overlap and equality, so in addition, this is the principle of repulsion.

Code:

#include <stdio.h> #include <string.h> #include <math.h> #include <algorithm>using namespace Std;const double eps = 1e-9;const double pi = ACOs ( -1.0); const int N = 205;int N, ln;struct point {double X, y;} P[n]; struct Line {Double L, a, B, C, K, y;} l[n * N];bool CMPK (line A, line B) {return A.K < B.K;} BOOL Cmpl (line A, line B) {return A.L < B.L;}    BOOL Cmpy (line A, line B) {if (Fabs (A.Y-B.Y) < EPS) return A.L < B.L; return A.Y < B.y;}    Line build (Point A, point B) {line ans;    ANS.L = sqrt ((a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (A.Y-B.Y));    ANS.A = b.y-a.y, ans.b = a.x-b.x, ans.c = ANS.A * a.x + ans.b * A.Y;    ANS.K = atan2 (ANS.A,-ans.b);    if (ANS.K < 0 | | fabs (ANS.K) < EPS) ANS.K + = PI;    if (Fabs (ans.b) < EPS) ans.y = ANS.C/ANS.A;    else Ans.y = ans.c/ans.b; return ans;} Long Long C (long long N) {return n * (n-1)/2;}    Long long Solve () {sort (L, l + ln, CMPK); A long long ans = 0,CNT = 0;    Line Save[n * N];    int sn = 0;    L[LN++].K =-1;    CNT = 0;    for (int i = 0; i < ln; i++) {if (!i | | fabs (L[I].K-L[I-1].K) < EPS) {save[sn++] = L[i];    cnt++; Continue;} Ans + = C (CNT), CNT = 0;sort (save, Save + sn, Cmpl), for (int j = 0; J < SN; j + +) {if (!j | | fabs (save[j-1].l-save    [J].L) < EPS) {cnt++;continue;    } ans-= C (CNT); CNT = 1;} Ans-= C (CNT), CNT = 0;sort (save, Save + sn, cmpy), for (int j = 0; J < SN; j + +) {if (!j | | fabs (save[j-1].y-save    [J].y) < EPS) {cnt++;continue;    } ans-= C (CNT); CNT = 1;} Ans-= C (CNT); cnt = 0;for (int j = 0; J < SN; j + +) {if (!j | | (Fabs (SAVE[J-1].Y-SAVE[J].Y) < EPS && fabs (SAVE[J-1].L-SAVE[J].L) < EPS))    {cnt++;continue;    } ans + = C (CNT); CNT = 1;}    Ans + = C (CNT); sn = 0;save[sn++] = l[i];cnt = 1; } return ans;    int main () {int cas = 0; while (~SCANF ("%d", &n) && n) {ln = 0;for (int i = 0; i < n; i++) scanf ("%lf%lF ", &p[i].x, &p[i].y); for (int i = 0; i < n; i++) {for (int j = i + 1; j < N; j + +) {l[ln++] = build (P[i]    , P[j]);    }}printf ("Case%d:%lld\n", ++cas, Solve ()); } 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.