Poj 2187 Beauty Contest (convex hull), poj2187

Source: Internet
Author: User

Poj 2187 Beauty Contest (convex hull), poj2187

Link: poj 2187

Obtain the Square Value of the distance between the two points in the plane.

Analysis: This question is simple, but it will definitely time out if the enumeration is used directly, but it can be converted into a convex hull,

Because the two farthest points must be the two vertices of their convex packets, so that the distance between the points on the enumeration convex packets is greatly reduced.

# Include <cstdio> # include <cstring> # include <algorithm> using namespace std; struct stu {int x, y;} p [50010], s [50010]; int m, top; int chaji (struct stu p1, struct stu p2, struct stu p3) // Cross Product {return (p1.x-p2.x) * (p3.y-p2.y) -(p3.x-p2.x) * (p1.y-p2.y);} double dis (struct stu p1, struct stu p2) // the square of the distance between two points {return (p1.x-p2.x) * (p1.x-p2.x) + (p1.y-p2.y) * (p1.y-p2.y);} int cmp (struct stu p1, struct stu p2) {int k; k = chaji (p1, p [0], p2); if (k> 0 | (k = 0 & dis (p1, p [0]) <dis (p2, p [0]) return 1; return 0;} void graham () {int I, k = 0; struct stu t; for (I = 1; I <m; I ++) if (p [I]. y <p [k]. y | (p [I]. y = p [k]. y & p [I]. x <p [k]. x) // first locate the leftmost vertex as the first vertex of the convex hull k = I; t = p [k]; p [k] = p [0]; p [0] = t; sort (p + 1, p + m, cmp); // except for the first vertex of the convex hull, s [0] = p [0]; s [1] = p [1]; top = 1; for (I = 2; I <m; I ++) {while (top> = 1 & chaji (s [top-1], s [top], p [I])> = 0) top --; top ++; s [top] = p [I] ;}} int main () {int n, I, j, k; while (scanf ("% d", & m )! = EOF) {if (m = 0) break; for (I = 0; I <m; I ++) scanf ("% d ", & p [I]. x, & p [I]. y); graham (); // calculate the vertex k = 0 on the convex hull using the Graham algorithm; for (I = 0; I <= top; I ++) // enumerate the points on the convex hull and obtain the square for (j = 0; j <I; j ++) {n = dis (s [I], s [j]); if (n> k) k = n;} printf ("% d \ n", k);} return 0 ;}



Beauty Contest poj2187. WA helps you identify the cause of the error.

This is the AC code. You can see where the error is. Also, you should not have a strong grasp of the convex bag, otherwise such questions should be killed in seconds!
# Include <cstdio>
# Include <iostream>
# Include <cmath>
# Include <cstring>
# Include <algorithm>
# Define N 50005
Using namespace std;
Struct point {
Int x, y;
} Pt [N];
Int convex [N];
Int n;
Int xmult (point a, point B, point c ){
Return (B. x-a.x) * (c. y-a.y)-(c. x-a.x) * (B. y-a.y ));
}
Int dist (point a, point B ){
Return (a. x-b.x) * (a. x-b.x) + (a. y-b.y) * (a. y-b.y );
}
Bool cmp (point a, point B)
{
If (xmult (pt [0], a, B)> 0 | (xmult (pt [0], a, B) = 0 & dist (a, B, pt [0]) <dist (B, pt [0]) return true;
Return false;
}
Bool left (int a, int B, int c ){
If (xmult (pt [a], pt [B], pt [c])> 0) return true;
Return false;
}
Int max (int a, int B ){
Return a> B? A: B;
}
Int main ()
{
// Freopen ("in.txt", "r", stdin );
Int I, j, k, ans, top;
While (scanf ("% d", & n )! = EOF ){
K = top = ans = 0;
For (I = 0; I <n; I ++ ){
Scanf ("% d", & pt [I]. x, & pt [I]. y );
If (pt [I]. y <pt [k]. y)
K = I;
Else if (pt [I]. y = pt [k]. y & pt [I]. x <pt [k]. x)
K = I;
}
If (k ){
Swap (pt [k]. x, pt [0]. x );
Swap (pt [k]. y, pt [0]. y );
}
Sort (pt + 1, pt + n, cmp );
Convex [top ++] = 0;
Convex [top ++] = 1;
Convex [top ++] = 2;
For (I = 3; I <n ;){
If (top <2 | left (convex [top-2], convex [top-1], I ))
Convex [top ++] = I ++;
Else top --;
}
For (I = 0; I <top; I ++ ){
For (j = I + 1; j <top; j ++)
Ans = max (ans, dist (pt [convex [I], pt [convex [j]);
}
Printf ("% d \ n", ans );
}
Return 0;
}... Remaining full text>

ACM solution report

Category of a poj question
Mainstream algorithms:
1. Search // trace back
2. DP (Dynamic Planning)
3. Greedy
4. Graph Theory // Dijkstra, Minimum Spanning Tree, and network stream
5. Number Theory // solves the modulus linear equation
6. Calculate the area and perimeter of the joint of the geometric/Convex Shell with the same placement of the rectangle
7. Composite math // Polya Theorem
8. Simulation
9. Data Structure // check the collection and heap
10. Game Theory
1. Sorting
1423,169 4, 1723,172 7, 1763,178 8, 1828,183 8, 1840,220 1, 2376,
2377,238 0, 1318,187 1971,197, 1990,200 4, 2002,209 1, 2379 2,
1002 (character processing is required, and sorting can be done in a fast way) 1007 (stable sorting) 2159 (difficult to understand)
2231 2371 (simple sorting) 2388 (sequence statistics algorithm) 2418 (Binary sorting tree)
2. Search, backtracking, and traversal
2329
Simple: 1128,116 6, 1176,123 1, 1256,127 0, 1321,154 3, 1606,166 4,
1731,174 2, 1745,184, 1950,203, 2157,218 8, 2183,238 2, 2386,242 1, 6
Not easy: 1024,105 4, 1117,116 7, 1708,174 6, 1775,187 8, 1903,196 6, 2046,
2197,234 9
Recommended: 1011,119 0, 1191,141 6, 1579,163 2, 1639,165 9, 1680,168 3, 1691,
1709,171 4, 1753,1771, 1826,185 5, 1856,189 0, 1924,193 5, 1948,197 9, 1980,217 2331,233 1979, 1980 9, (similar to the maze), (higher requirements for pruning)
3. Calendar
1008 2080 (Be careful with such questions)
4. Enumeration
1387,141, 2245,232, 2363,238 1, 1650 6, (higher pruning requirements), (decimal precision problem)
5. Typical data structure Algorithms
Easy: 1182,165 6, 2021,202 3, 2051,215 3, 2227,223 6, 2247,235 2,
2395
Not easy: 1145,117 7, 1195,122 7, 1661,183 4
Recommended: 1330,133 8, 1451,147 0, 1634,168 9, 1693,170 3, 1724,198 8, 2004,
2010,211 9, 2274
1125 (fresh algorithm), 2421 (Minimum Spanning Tree of the graph)
6. Dynamic Planning
1037 A decorative fence,
1050 To the Max,
1088 skiing,
1125 Stockbroker Grapevine,
1141 Brackets Sequence,
1159 Palindrom ...... remaining full text>

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.