Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- #include <iomanip>
- #include <vector>
- struct Interval {
- double begin;
- double end;
- double weight;
- Interval(double begin, double end, double weight) : begin(begin), end(end), weight(weight) {
- }
- };
- bool cmp(const Interval& x, const Interval& y) {
- return x.end < y.end;
- }
- double solve(const std::vector<Interval>& ints, std::vector<double>& res, const std::vector<int32_t>& v, int32_t n) {
- if (n <= 0) {
- return 0;
- }
- if (res[n - 1] != 0) {
- return res[n - 1];
- }
- res[n - 1] = std::max(solve(ints, res, v, n - 1),
- ints[n - 1].weight + solve(ints, res, v, v[n]));
- return res[n - 1];
- }
- int main() {
- std::ios_base::sync_with_stdio(false);
- std::cin.tie(nullptr);
- int32_t n = 0;
- std::cin >> n;
- std::vector<Interval> ints;
- std::vector<int32_t> v(n + 1);
- for (int32_t i = 0; i < n; ++i) {
- double b, e, w;
- std::cin >> b >> e >> w;
- ints.emplace_back(b, e, w);
- }
- std::sort(ints.begin(), ints.end(), cmp);
- for (int32_t i = 0; i < ints.size(); ++i) {
- for (int32_t j = i - 1; j >= 0; --j) {
- if (ints[j].end <= ints[i].begin) {
- v[i + 1] = j + 1;
- break;
- }
- }
- }
- std::vector<double> res(n);
- std::cout << std::fixed << std::showpoint << std::setprecision(4) << solve(ints, res, v, n);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement