Luogu P1052 过河___动态规划

题目大意:

Luogu P1052 过河___动态规划_第1张图片

分析:

将两个石头间的距离压缩,然后dp即可

代码:

#include 


#define rep(i, st, ed) for (int i = st; i <= ed; i++)
#define rwp(i, ed, st) for (int i = ed; i >= st; i--)

#define inf 0x7fffffff
#define M 105 * 105
#define N 105

using namespace std;

int vis[M], dp[M], a[N], b[N], L, s, t, m, num, ans;

int read(int &x) {
	int f = 1; x = 0; char s = getchar();
	while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
	while (s >= '0' && s <= '9') { x = x * 10 + (s - '0'); s = getchar(); }
	return x * f;
}
 
int main() {
	read(L);
    read(s); read(t); read(m);
    num = t;
    rep(i, 1, m) read(b[i]); b[m + 1] = L;
    sort(b + 1, b + m + 2);
    rep(i, 1, m + 1) {
    	if (b[i] - b[i - 1] >= num) a[i] = a[i - 1] + (b[i] - b[i - 1]) % num + num; 
    	    else a[i] = a[i - 1] + (b[i] - b[i - 1]);
        vis[a[i]] = 1;
    }
    vis[a[m + 1]] = 0;
    memset(dp, 0x3f, sizeof(dp));
    dp[0] = 0;
    rep(i, 1, a[m + 1] + num - 1)
	    rep(j, s, t) if (i - j >= 0) dp[i] = min(dp[i], dp[i - j] + vis[i]);
    int ans = inf;
    rep(i, a[m + 1], a[m + 1] + num - 1) ans = min(ans, dp[i]);
    printf("%d\n", ans);
    return 0;
}

你可能感兴趣的:(C++,动态规划)