Advertisement
TheKabeton

Untitled

Nov 15th, 2020
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 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. double ans = 0;
  77. cin >> n >> h;
  78. vector<pair<ll, ll>> v(n, make_pair(0, 0));
  79.  
  80. for (ll i = 0; i < n; ++i) {
  81. cin >> x >> y;
  82. v[n - i - 1] = make_pair(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 = 1; i < n; ++i) {
  94. long double alpha2;
  95.  
  96. double xc = v[i].first - xh;
  97. double yc = v[i].second - yh;
  98. alpha2 = atan(yc / xc) + M_PI;
  99.  
  100. if ((alpha2 - alpha + 0.0000000001) > 0) {
  101. xm = v[i].first;
  102. ym = v[i].second;
  103. q = 1;
  104. } else {
  105. if (q == 1) {
  106. q = 0;
  107. pt ptt{0, 0};
  108. double a, b, c;
  109. a = yh - ym;
  110. b = xm - xh;
  111. c = -a * xh - b * yh;
  112. line line1{a, b, c};
  113.  
  114. a = v[i].second - v[i - 1].second;
  115. b = v[i - 1].first - v[i].first;
  116. c = -a * v[i].first - b * v[i].second;
  117. line line2{a, b, c};
  118.  
  119. intersect(line1, line2, ptt);
  120. cout << ptt.x << ' ' << ptt.y << endl;
  121. ans += sqrt((v[i].first - ptt.x) * (v[i].first - ptt.x) +
  122. (v[i].second - ptt.y) * (v[i].second - ptt.y));
  123. } else {
  124. alpha = alpha2;
  125. ans += sqrt((v[i].first - v[i - 1].first) * (v[i].first - v[i - 1].first) +
  126. (v[i].second - v[i - 1].second) * (v[i].second - v[i - 1].second));
  127. q = 0;
  128. }
  129. }
  130. }
  131.  
  132. cout << ans;
  133.  
  134. re 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement