题意:
要在N个教室中的一些建造糖果屋,花费为在该教室建造的花费+其右侧未建造糖果的教室到该教室的距离
分析:
直接DP,d[i]表示在该点建造糖果屋的情况下[i..n]所有教室的最小花费
代码:
#define inf 0x3f3f3f3f3f3f3f3f
#define maxn 3007
ll n, a[maxn], c[maxn];
ll b[maxn], d[maxn];
ll work(int x) {
if (d[x] != -1) return d[x];
if (x == n) return d[x] = 0;
d[x] = inf;
ll cost = b[c[x]];
for (int j = x + 1; j <= n; j++) {
d[x] = min(d[x], work(j) + cost);
cost += 0ll + a[c[j]] - a[c[x]];
}
return d[x];
}
int cmp(int i, int j) {
return a[i] < a[j];
}
int main()
{
while (~scanf("%lld", &n)) {
for (int i = 0; i < n; i++) {
scanf("%lld%lld", &a[i], &b[i]);
c[i] = i;
}
memset(d, -1, sizeof d);
sort(c, c + n, cmp);
work(0);
printf("%lld\n", d[0]);
}
}