Advertisement
Guest User

Untitled

a guest
Sep 8th, 2013
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.49 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5. #include <stack>
  6. #include <queue>
  7. #include <set>
  8. #include <map>
  9. #include <string>
  10. #include <algorithm>
  11. #include <iostream>
  12. #include <functional>
  13. using namespace std;
  14.  
  15. struct Point {
  16.     int x, y;
  17. } points[60];
  18.  
  19. bool cmpCoord(Point a, Point b) {
  20.     return a.y == b.y ? a.x < b.x : a.y > b.y;
  21. }
  22.  
  23. int len2(Point a, Point b) {
  24.     return (b.x - a.x) * (b.x - a.x) + (b.y - a.y) * (b.y - a.y);
  25. }
  26.  
  27. int area(Point a, Point b, Point c) {
  28.     return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
  29. }
  30.  
  31. bool cmpAngle(Point a, Point b) {
  32.     return area(points[0], a, b) == 0 ? len2(points[0], a) < len2(points[0], b) : area(points[0], a, b) < 0;
  33. }
  34.  
  35. int p, n;
  36. vector<Point> hull;
  37.  
  38. int main() {
  39.     //freopen("input.txt", "r", stdin);
  40.     //freopen("output.txt", "w", stdout);
  41.  
  42.     scanf("%d", &p);
  43.     for (int tst = 1; tst <= p; tst++) {
  44.         scanf("%*d%d", &n);
  45.         for (int i = 0; i < n; i++)
  46.             scanf("%d%d", &points[i].x, &points[i].y);
  47.         sort(points, points + n, cmpCoord);
  48.         sort(points + 1, points + n, cmpAngle);
  49.         hull.clear();
  50.         hull.push_back(points[0]);
  51.         hull.push_back(points[1]);
  52.         for (int i = 2; i < n; i++) {
  53.             while (hull.size() > 1 && area(hull[hull.size() - 2], hull[hull.size() - 1], points[i]) >= 0)
  54.                 hull.pop_back();
  55.             hull.push_back(points[i]);         
  56.         }      
  57.         printf("%d %d\n", tst, hull.size());
  58.         for (int i = 0; i < hull.size(); i++)
  59.             printf("%d %d\n", hull[i].x, hull[i].y);
  60.     }
  61.    
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement