Advertisement
TheKabeton

Untitled

Nov 15th, 2020
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. /*-------------------- Code written by Kabeton --------------------*/
  2. /*--- Contacts: https://vk.com/kabeton and TheKabeton@yandex.ru ---*/
  3.  
  4. #define _USE_MATH_DEFINES
  5. #include <iostream>
  6. #include <iomanip>
  7. #include <cstdio>
  8. #include <cstdlib>
  9. #include <cmath>
  10. #include <algorithm>
  11. #include <vector>
  12. #include <map>
  13. #include <unordered_map>
  14. #include <set>
  15. #include <bitset>
  16. #include <tuple>
  17. #include <queue>
  18. #include <stack>
  19. #include <valarray>
  20. #include <sstream>
  21. #include <cstring>
  22. #include <numeric>
  23. #include <ctime>
  24. #include <cassert>
  25.  
  26. using namespace std;
  27.  
  28. #define endl "\n"
  29. #define re return
  30. #define fi first
  31. #define se second
  32. #define all(a) a.begin(), a.end()
  33.  
  34. typedef long long ll;
  35. typedef unsigned long long ull;
  36. typedef long double ld;
  37. typedef pair<ll, ll> pll;
  38. typedef vector<ll> vll;
  39. typedef vector<vll> vvll;
  40. typedef vector<pll> vpll;
  41. typedef vector<vpll> vvpll;
  42.  
  43.  
  44. const ll mod = 1e9 + 7;
  45. const ll inf = 1e15;
  46.  
  47. struct pt {
  48. double x, y;
  49. };
  50.  
  51. struct line {
  52. double a, b, c;
  53. };
  54.  
  55. const double EPS = 1e-9;
  56.  
  57. double det (double a, double b, double c, double d) {
  58. return a * d - b * c;
  59. }
  60.  
  61. bool intersect (line m, line n, pt & res) {
  62. double zn = det (m.a, m.b, n.a, n.b);
  63. if (abs (zn) < EPS)
  64. return false;
  65. res.x = - det (m.c, m.b, n.c, n.b) / zn;
  66. res.y = - det (m.a, m.c, n.a, n.c) / zn;
  67. return true;
  68. }
  69.  
  70. int main() {
  71. ios::sync_with_stdio(false);
  72. cin.tie(nullptr);
  73. cout.tie(nullptr);
  74.  
  75. ll n, h, x, y, xh, yh;
  76. long double ans = 0;
  77. cin >> n >> h;
  78. vector<pair<ll, ll>> v;
  79.  
  80. for (ll i = 0; i < n; ++i) {
  81. cin >> x >> y;
  82. v.emplace_back(x, y);
  83. }
  84.  
  85. xh = v[0].first;
  86. yh = v[0].second + h;
  87.  
  88. long double alpha = M_PI + M_PI / 2;
  89. ll xm = -1;
  90. ll ym = -1;
  91. ll q = 0;
  92.  
  93. for (ll i = 0; i < n; ++i) {
  94. long double alpha2;
  95.  
  96. ll xc = xh -= v[i].first;
  97. ll yc = yh -= v[i].second;
  98. if (xc == 0) {
  99. if (y > 0) {
  100. alpha2 = M_PI / 2;
  101. } else {
  102. alpha2 = M_PI + M_PI / 2;
  103. }
  104. } else {
  105. alpha2 = atan(yc / xc);
  106. }
  107.  
  108. if ((alpha2 - alpha + 0.0000000001) > 0) {
  109. if (q == 1) {
  110. q = 0;
  111. pt ptt{0, 0};
  112. double a, b, c;
  113. a = yh - ym;
  114. b = xm - xh;
  115. c = -a * xh - b * yh;
  116. line line1{a, b, c};
  117.  
  118. a = v[i].second - v[i - 1].second;
  119. b = v[i - 1].first - v[i].first;
  120. c = -a * v[i].first - b * v[i].second;
  121. line line2{a, b, c};
  122.  
  123. intersect(line1, line2, ptt);
  124.  
  125. ans += sqrt((v[i].first - ptt.x) * (v[i].first - ptt.x) +
  126. (v[i].second - ptt.y) * (v[i].second - ptt.y));
  127. } else {
  128. alpha = alpha2;
  129. xm = v[i].first;
  130. ym = v[i].second;
  131. }
  132. } else {
  133. ans += sqrt((v[i].first - v[i - 1].first) * (v[i].first - v[i - 1].first) +
  134. (v[i].second - v[i - 1].second) * (v[i].second - v[i - 1].second));
  135. q = 1;
  136. }
  137. }
  138.  
  139. cout << ans;
  140.  
  141. re 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement