转换关系搞清楚了,但是关于那个单调性来处理DP的地方,还是不明白,应该是单调队列优化DP这个点不会,补一下代码,先学一下单调队列优化DP,
参考博客:
#include#include #include using namespace std;const int maxn = 100005;int f[maxn], g[maxn];int main(){ int l, n, p, t; scanf("%d%d%d%d", &l, &n, &p, &t); int head =1, tail = 1; f[head] = 0, g[head] = -t; int ans = 0; for (int i = 1; i <= n; i++) { int x, y,ans1,ans2; scanf("%d%d", &x, &y); ans1 = ans2 = 0; if (head>1) head--; while (head <= tail&&g[head] + t <= y) { int nx = max(x, g[head] + t); int ny = y; if (f[head] + (ny - nx) / p > ans1) { ans1 = f[head] + (ny - nx) / p; ans2 = nx + (ny - nx) / p*p; } else if (f[head] + (ny - nx) / p == ans1) { ans2 = min(ans2,nx + (ny - nx) / p*p); } ++head; } if (ans1 > ans) { ans = ans1; tail++; f[tail] = ans1; g[tail] = ans2; } } printf("%d\n", ans); return 0;}