1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
| #include <bits/stdc++.h> using namespace std;
#ifdef Fread char buf[1 << 21], *iS, *iT; #define gc() (iS == iT ? (iT = (iS = buf) + fread (buf, 1, 1 << 21, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++) #define getchar gc #endif
template <typename T> void r1(T &x) { x = 0; char c(getchar()); int f(1); for(; c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1; for(; '0' <= c && c <= '9';c = getchar()) x = (x * 10) + (c ^ 48); x *= f; }
template <typename T,typename... Args> inline void r1(T& t, Args&... args) { r1(t); r1(args...); }
#ifdef Getmod const int mod = 1e9 + 7; template <int mod> struct typemod { int z; typemod(int a = 0) : z(a) {} inline int inc(int a,int b) const {return a += b - mod, a + ((a >> 31) & mod);} inline int dec(int a,int b) const {return a -= b, a + ((a >> 31) & mod);} inline int mul(int a,int b) const {return 1ll * a * b % mod;} typemod<mod> operator + (const typemod<mod> &x) const {return typemod(inc(z, x.z));} typemod<mod> operator - (const typemod<mod> &x) const {return typemod(dec(z, x.z));} typemod<mod> operator * (const typemod<mod> &x) const {return typemod(mul(z, x.z));} typemod<mod>& operator += (const typemod<mod> &x) {*this = *this + x; return *this;} typemod<mod>& operator -= (const typemod<mod> &x) {*this = *this - x; return *this;} typemod<mod>& operator *= (const typemod<mod> &x) {*this = *this * x; return *this;} int operator == (const typemod<mod> &x) const {return x.z == z;} int operator != (const typemod<mod> &x) const {return x.z != z;} }; typedef typemod<mod> Tm; #endif
const int maxn = 1e4 + 5; const int maxm = maxn << 1; typedef long long ll; int n, m, K; struct Node { int x, y, t; int operator < (const Node &z) const { return y < z.y; } }pt[maxn];
const ll inf = 0x3f3f3f3f3f3f3f3f;
vector<ll> merge(const vector<ll>& a, const vector<ll>& b) { vector<ll> c(a.size() + b.size() - 1, inf); for(int i = 0; (unsigned)i < a.size(); ++ i) { for(int j = 0; (unsigned)j < b.size(); ++ j) { c[i + j] = min(c[i + j], a[i] + b[j]); } } return c; }
vector<ll> Solve(const vector<Node>& a, int dis) {
vector<ll> res(a.size(), inf), tmp(a.size(), inf); res[0] = (dis == -1) ? 0 : (abs(dis - a[0].y)); tmp[0] = 0; for(int i = 1; (unsigned)i < a.size(); ++ i) { int Dis = abs(a[i].y - a[i - 1].y); for(int j = 0; j < i; ++ j) tmp[j] += Dis; for(int j = i; j >= 1; -- j) tmp[j] = min(tmp[j], a[i].t + tmp[j - 1]); for(int j = 0; j <= i; ++ j) { res[j] = min(res[j], tmp[j] + (dis == -1 ? 0 : abs(dis - a[i].y))); } } return res; }
signed main() { freopen("milk.in", "r", stdin); freopen("milk.out", "w", stdout); int i, T(1);
while(T --) { r1(n, m, K); vector<ll> nc; nc.push_back(1); vector<ll> f[2]; f[0].resize(K + 1); f[1].resize(K + 1); for(i = 1; i <= K; ++ i) { r1(pt[i].x, pt[i].y, pt[i].t); nc.push_back(pt[i].x); }
sort(nc.begin(), nc.end()); nc.erase(unique(nc.begin(), nc.end()), nc.end()); vector<vector<Node>> line; line.resize(K + 2); vector<ll> ans; ans.resize(K * 2 + 1); for(i = 1; i <= K; ++ i) { int tmp = lower_bound(nc.begin(), nc.end(), pt[i].x) - nc.begin();
line[tmp].push_back(pt[i]); ans[i] = inf; }
for(i = 0; (unsigned)i < nc.size(); ++ i) sort(line[i].begin(), line[i].end());
vector<Node> nl = line[0]; nl.insert(nl.begin(), (Node) {1, 1, 0}); f[0] = Solve(nl, (m + 1) >> 1); vector<ll> tmp = Solve(nl, -1);
for(int i = 0; (unsigned)i < tmp.size(); ++ i) ans[i] = min(ans[i], tmp[i]);
for(int _ = 1; (unsigned)_ < nc.size(); ++ _) { auto it = lower_bound(line[_].begin(), line[_].end(), (Node){_, (m + 1) >> 1, 0}); vector<Node> l0(line[_].begin(), it); vector<Node> l1(it, line[_].end());
l0.push_back((Node){_, (m + 1) >> 1, 0}); reverse(l0.begin(), l0.end());
l1.insert(l1.begin(), (Node) {_, (m + 1) >> 1, 0});
vector<ll> bl, br, gl, gr; bl = Solve(l0, (m + 1) >> 1); br = Solve(l1, (m + 1) >> 1); gl = Solve(l0, -1); gr = Solve(l1, -1);
gl = merge(br, gl); gr = merge(bl, gr);
vector<ll> ng; ng.resize(gl.size()); for(i = 0; (unsigned)i < gl.size(); ++ i) ng[i] = min(gl[i], gr[i]);
vector<ll> g = merge(f[(_ - 1) & 1], ng);
f[_ & 1] = merge(f[(_ - 1) & 1], merge(bl, br));
for(i = 0; (unsigned)i < g.size(); ++ i) { g[i] += nc[_] - nc[_ - 1]; ans[i] = min(ans[i], g[i]); f[_ & 1][i] += nc[_] - nc[_ - 1]; } } for(i = 1; i <= K; ++ i) printf("%lld%c", ans[i], " \n"[i == K]); } return 0; }
|