#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#ifdef ONPC
#include "t_debug.cpp"
#else
#define debug(...) 42
#endif
#define allit(a) (a).begin(), (a).end()
#define sz(a) ((int) (a).size())
#define cut(s) {cout << s << '\n'; return 0;}
using namespace std;
using ll = long long;
using vi = vector<int>;
namespace pd = __gnu_pbds;
template<typename K> using ordered_set = pd::tree<K, pd::null_type, less<K>, pd::rb_tree_tag, pd::tree_order_statistics_node_update>;
template<typename... T> using hash_table = pd::gp_hash_table<T...>;
const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937 rng(RANDOM);
const int INF = 1e9;
const ll INFL = 1e18;
const int N = 1000;
const int M = 10;
struct device {
int left, right, center, cost;
};
ostream& operator <<(ostream& os, device& a) {
return os << "{" << a.left << "," << a.right << ", " << a.center << " $" << a.cost << "}";
}
int n, m;
device dev[M];
struct comp {
bool operator()(int i, int j) { return dev[i].left < dev[j].left; }
};
int solve() {
cin >> m >> n;
for (int i = 0; i < n; i++) {
cin >> dev[i].left >> dev[i].right >> dev[i].center >> dev[i].cost;
dev[i].left--; dev[i].right--; dev[i].center--;
}
ll memoLeft[m], memoRight[m];
memset(memoLeft, 0, sizeof(memoLeft));
memset(memoRight, 0, sizeof(memoRight));
function<ll(int)> dpLeft = [&](int i) {
ll* memo = memoLeft;
auto& dp = dpLeft;
if (i < 0) return INFL;
if (memo[i]) return memo[i];
if (dev[i].left == 0) {
ll res = dev[i].cost;
for (int prev = 0; prev < i; prev++) {
if (dev[prev].left == 0) res = min(res, dp(prev));
}
return memo[i] = res;
} else {
ll res = INFL;
for (int prev = 0; prev < i; prev++) {
int l1 = dev[i].left, r1 = dev[i].center;
int l2 = dev[prev].left, r2 = dev[prev].center;
if (l2 <= l1 && l1 <= r2) res = min(res, dp(prev));
}
return memo[i] = dev[i].cost + res;
}
};
function<ll(int)> dpRight = [&](int i) {
ll* memo = memoRight;
auto& dp = dpRight;
if (i < 0) return INFL;
if (memo[i]) return memo[i];
if (dev[i].right == n-1) {
ll res = dev[i].cost;
for (int prev = 0; prev < i; prev++) {
if (dev[prev].right == n-1) res = min(res, dp(prev));
}
return memo[i] = res;
} else {
ll res = INFL;
for (int prev = 0; prev < i; prev++) {
int l1 = dev[i].center, r1 = dev[i].right;
int l2 = dev[prev].center, r2 = dev[prev].right;
if (l2 <= r1 && r1 <= r2) res = min(res, dp(prev));
}
return memo[i] = dev[i].cost + res;
}
};
ll res = INFL;
for (int i = 0; i < m; i++) {
debug(i, dpLeft(i), dpRight(i));
res = min(res, dpLeft(i) + dpRight(i) - dev[i].cost);
}
cout << (res >= INFL ? -1 : res);
return 0;
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
int t = 1;
//cin >> t;
while (t-- && cin) {
if (solve()) break;
#ifdef ONPC
cout << "____________________" << endl;
#endif
}
return 0;
}
Compilation message
pinball.cpp: In lambda function:
pinball.cpp:66:39: warning: unused variable 'r1' [-Wunused-variable]
66 | int l1 = dev[i].left, r1 = dev[i].center;
| ^~
pinball.cpp: In lambda function:
pinball.cpp:88:21: warning: unused variable 'l1' [-Wunused-variable]
88 | int l1 = dev[i].center, r1 = dev[i].right;
| ^~
pinball.cpp: In function 'int solve()':
pinball.cpp:7:24: warning: statement has no effect [-Wunused-value]
7 | #define debug(...) 42
| ^~
pinball.cpp:97:9: note: in expansion of macro 'debug'
97 | debug(i, dpLeft(i), dpRight(i));
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |