小天使我爱你~muaaaaa~跟随小天使的步伐学会了【模板】于是可以在20分钟内A简单的dp题啦~虽然dp方程还是不太会推,但是只要会优化就完成近阶段任务了嘛。
这个题的dp方程:
dp[i]=(a[i]+10)*p[i]+dp[i-1]
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct Node {
int a, p;
}pearl[105];
int t, n, dp[105], queue[105], sum[105], head, tail;
int getx( int k, int j ) {
return dp[k] - dp[j];
}
int gety( int k, int j ) {
return sum[k] - sum[j];
}
void updata() {
head = 0, tail = 1;
memset(queue, 0, sizeof(queue));
memset(dp, 0, sizeof(dp));
memset(sum, 0, sizeof(sum));
}
int main() {
scanf("%d", &t);
while(t--) {
updata();
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
scanf("%d%d", &pearl[i].a, &pearl[i].p);
sum[i] = sum[i - 1] + pearl[i].a;
}
for(int i = 1; i <= n; i++) {
while(head + 1 < tail && getx(queue[head + 1], queue[head]) <= gety(queue[head + 1], queue[head]) * pearl[i].p)
head++;
dp[i] = dp[queue[head]] +( sum[i] - sum[queue[head]] + 10 ) * pearl[i].p;
while(head + 1 < tail && getx(i, queue[tail - 1]) * gety(queue[tail - 1], queue[tail - 2]) <= getx(queue[tail - 1], queue[tail - 2]) * gety(i, queue[tail - 1]))
tail--;
queue[tail++] = i;
}
printf("%d\n", dp[n]);
}
return 0;
}