Guest User

Untitled

a guest
Jun 19th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. template <typename T>
  7. class _vertex
  8. {
  9. public:
  10. bool operator <(const _vertex& v) const
  11. {
  12. return (y != v.y) ? (y < v.y) : (x < v.x);
  13. }
  14.  
  15. static T cross(const _vertex& o, const _vertex& a, const _vertex& b)
  16. {
  17. return (a.x - o.x)*(b.y - o.y) - (a.y - o.y)*(b.x - o.x);
  18. }
  19.  
  20. private:
  21. friend istream& operator >>(istream& is, _vertex& v)
  22. {
  23. is >> v.x >> v.y;
  24. return is;
  25. }
  26.  
  27. friend ostream& operator <<(ostream& os, const _vertex& v)
  28. {
  29. os << v.x << " " << v.y;
  30. return os;
  31. }
  32.  
  33. private:
  34. T x, y;
  35. };
  36.  
  37. using vertex = _vertex<int>;
  38.  
  39. vector<vertex> find_convex_hull(vector<vertex>&& contour)
  40. {
  41. sort(contour.begin(), contour.end());
  42.  
  43. if (contour.size() <= 3) return contour; // It should still be sorted to fulfill the required order.
  44.  
  45. auto back = [](const vector<vertex>& container, size_t offset)
  46. {
  47. return *(container.rbegin() + offset);
  48. };
  49.  
  50. vector<vertex> convex;
  51.  
  52. for (auto it = contour.cbegin(); it != contour.cend(); it++)
  53. {
  54. while (convex.size() > 1 && vertex::cross(back(convex, 1), back(convex, 0), *it) <= 0) convex.pop_back();
  55. convex.push_back(*it);
  56. }
  57.  
  58. size_t s = convex.size();
  59. for (auto it = contour.rbegin(); it != contour.rend(); it++)
  60. {
  61. while (convex.size() > s && vertex::cross(back(convex, 1), back(convex, 0), *it) <= 0) convex.pop_back();
  62. convex.push_back(*it);
  63. }
  64.  
  65. return convex;
  66. }
  67.  
  68. void test_case()
  69. {
  70. size_t n;
  71. cin >> n;
  72. vector<vertex> contour(n);
  73. for (auto& v : contour) cin >> v;
  74.  
  75. auto convex = find_convex_hull(move(contour));
  76.  
  77. cout << convex.size() << endl;
  78. for (const auto& v : convex) cout << v << endl;
  79. }
  80.  
  81. int main()
  82. {
  83. size_t n;
  84. cin >> n;
  85. cout << n << endl;
  86. while (n--)
  87. {
  88. test_case();
  89.  
  90. if (n > 0)
  91. {
  92. int s;
  93. cin >> s;
  94. cout << -1 << endl;
  95. }
  96. }
  97. return 0;
  98. }
Add Comment
Please, Sign In to add comment