Advertisement
_takumi

c5d2023

Mar 1st, 2023
640
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.47 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <vector>
  5.  
  6. struct Interval {
  7.     double begin;
  8.     double end;
  9.     double weight;
  10.     Interval(double begin, double end, double weight) : begin(begin), end(end), weight(weight) {
  11.     }
  12. };
  13.  
  14. bool cmp(const Interval& x, const Interval& y) {
  15.     return x.end < y.end;
  16. }
  17.  
  18. double solve(const std::vector<Interval>& ints, std::vector<double>& res, const std::vector<int32_t>& v, int32_t n) {
  19.     if (n <= 0) {
  20.         return 0;
  21.     }
  22.     if (res[n - 1] != 0) {
  23.         return res[n - 1];
  24.     }
  25.     res[n - 1] = std::max(solve(ints, res, v, n - 1),
  26.                           ints[n - 1].weight + solve(ints, res, v, v[n]));
  27.     return res[n - 1];
  28. }
  29.  
  30. int main() {
  31.     std::ios_base::sync_with_stdio(false);
  32.     std::cin.tie(nullptr);
  33.     int32_t n = 0;
  34.     std::cin >> n;
  35.     std::vector<Interval> ints;
  36.     std::vector<int32_t> v(n + 1);
  37.     for (int32_t i = 0; i < n; ++i) {
  38.         double b, e, w;
  39.         std::cin >> b >> e >> w;
  40.         ints.emplace_back(b, e, w);
  41.     }
  42.     std::sort(ints.begin(), ints.end(), cmp);
  43.     for (int32_t i = 0; i < ints.size(); ++i) {
  44.         for (int32_t j = i - 1; j >= 0; --j) {
  45.             if (ints[j].end <= ints[i].begin) {
  46.                 v[i + 1] = j + 1;
  47.                 break;
  48.             }
  49.         }
  50.     }
  51.     std::vector<double> res(n);
  52.     std::cout << std::fixed << std::showpoint << std::setprecision(4) << solve(ints, res, v, n);
  53.     return 0;
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement