Advertisement
Guest User

Untitled

a guest
May 21st, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <string>
  4. #include <vector>
  5. #include <map>
  6. #include <set>
  7. #include <unordered_map>
  8. #include <unordered_set>
  9. #include <algorithm>
  10. #include <queue>
  11. #include <cmath>
  12. #include <stdio.h>
  13.  
  14. #define _ << " " <<
  15. #define debug(x) #x << " = " << x
  16.  
  17. #define ll long long
  18. #define ull unsigned long long
  19.  
  20. #define int ll
  21. struct line {
  22. int m, b;
  23. float l;
  24. bool q = false;
  25. int val;
  26.  
  27. bool operator < (const line& other) const {
  28. if (other.q) {
  29. return l < other.val;
  30.  
  31. }
  32. else
  33. return m > other.m;
  34. }
  35. };
  36.  
  37.  
  38. const double eps = 1e-9;
  39. const float some_very_special_value = 1234567;
  40. float xinter (line a, line b)
  41. {
  42. return 1. * (b.b - a.b) / (a.m - b.m);
  43. }
  44.  
  45. bool rel(line a, line b, line c)
  46. {
  47. //std::cout << "xinter" _ debug(xinter(b, c)) _ debug(xinter(a, c)) << std::endl;
  48. //std::cout << debug(a.m) _ debug(a.b) _ debug(c.m) _ debug(c.b) << std::endl;
  49. if (a.m == b.m)
  50. return b.b > a.b;
  51. if (xinter(b, c) <= xinter(a, c))
  52. return false;
  53. return true;
  54. }
  55.  
  56. bool rel2 (line a, line b, line c)
  57. {
  58. //std::cout << "xinter" _ debug(xinter(b, c)) _ debug(xinter(a, c)) << std::endl;
  59. //std::cout << debug(a.m) _ debug(a.b) _ debug(c.m) _ debug(c.b) << std::endl;
  60.  
  61. if (xinter(a, c) <= xinter(a, b))
  62. return false;
  63. return true;
  64. }
  65.  
  66. std::set<line> s;
  67.  
  68. void push(line k)
  69. {
  70. //std::cout << "begin push" _ debug(k.m) _ debug(k.b) << std::endl;
  71.  
  72. auto sit = s.upper_bound(k);
  73.  
  74. auto it = sit;
  75. if (it != s.begin() && it != s.end()) {
  76. --it;
  77. auto it2 = sit;
  78. if (it2 != s.end()) {
  79. if (!rel(*it, k, *it2)) /* check if the lines
  80. on the two sides don't make us irrelevant */
  81. return;
  82. }
  83. }
  84.  
  85. it = sit;
  86. std::vector<std::set<line>::iterator> to_remove;
  87.  
  88. if (it != s.begin()) {
  89. --it;
  90. k.l = xinter(*it, k);
  91. while(it != s.begin()) {
  92. auto c = it;
  93. --it;
  94.  
  95. if (!rel(*it, *c, k)) {
  96. to_remove.push_back(c);
  97. k.l = xinter(*it, k);
  98. } else {
  99. break;
  100. }
  101. }
  102. } else {
  103. if (it != s.end() && it->m > k.m)
  104. k.l = xinter(*it, k);
  105. else
  106. k.l = -100000000;
  107. }
  108.  
  109. it = sit;
  110. if (it != s.end()) {
  111. auto c = it;
  112. ++it;
  113. while(it != s.end()) {
  114. if (!rel2(k, *c, *it)) {
  115. to_remove.push_back(c);
  116. } else {
  117. break;
  118. }
  119. c = it;
  120. ++it;
  121. }
  122. }
  123.  
  124. for (auto x : to_remove) {
  125. //std::cout << "removing" << debug(x) << std::endl;
  126. s.erase(x);
  127. }
  128.  
  129. s.insert(k);
  130. it = s.find(k);
  131.  
  132. it++;
  133. if (it != s.end()) {
  134. line &l = const_cast<line&> (*it);
  135. l.l = xinter(k, *it);
  136. }
  137.  
  138. /*
  139. std::cout << "##################### push" _ debug(k.l) _ debug(k.m) << std::endl;
  140. std::cout << "after update we have" << std::endl;
  141. for (auto x : s) {
  142. std::cout << debug(x.second.m) _ debug(x.second.b) _ debug(x.second.l) << std::endl;
  143. } */
  144. //std::cout << debug(k.l) << std::endl;
  145. }
  146.  
  147. int query(int x)
  148. {
  149. /*
  150. std::cout << "query available are" << std::endl;
  151. for (auto x : s) {
  152. std::cout << debug(x.m) _ debug(x.b) _ debug(x.l) << std::endl;
  153.  
  154. } */
  155. line k;
  156. k.q = true;
  157. k.val = x;
  158.  
  159. auto it = s.lower_bound(k);
  160.  
  161. //std::cout << debug(it->m) _ debug(it->b) << std::endl;
  162. if (it != s.begin())
  163. --it;
  164.  
  165. //std::cout << "query is" _ debug(x) << std::endl;
  166. //std::cout << "chose" _ debug(it->m) _ debug(it->b) << std::endl;
  167. // if (true || it != left.end()) {
  168. return it->m * x + it->b;
  169. //} else {
  170. // std::cout << "WA" << std::endl;
  171. // std::exit(0);
  172. // }
  173. }
  174.  
  175.  
  176. const int maxn = 1e6+10;
  177. int d[maxn], p[maxn], t[maxn];
  178. int dp[maxn];
  179. #undef int
  180. int main() {
  181. #define int ll
  182. std::ios::sync_with_stdio(false);
  183.  
  184. int n;
  185. scanf("%lld", &n);
  186.  
  187. for (int i =0; i < n; i++) scanf("%lld %lld %lld", d + i, p + i, t + i);
  188.  
  189. int dist = d[0];
  190.  
  191. //dp[k] + p[k] + (d[i] - d[k]) * t[k]
  192.  
  193. /* dp[i] = min(dp[k] + b[k] * a[i]) */
  194.  
  195. dp[0] = 0;
  196.  
  197. line k;
  198. k.m = t[0];
  199. k.b = p[0];
  200. push(k);
  201. for (int i = 1; i < n; i++) {
  202. dp[i] = query(dist - d[i]);
  203. // std::cout << debug(i) _ debug(dp[i]) << std::endl;
  204. k.m = t[i];
  205. k.b = dp[i] + p[i] - (dist - d[i]) * t[i];
  206. push(k);
  207. }
  208.  
  209. printf("%lld\n", query(dist));
  210.  
  211. return 0;
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement