洛谷 P1934 封印
题目传送门
解题思路:
f[i]表示打到第i层的最小值
AC代码:
1 #include<iostream> 2 #include<cstdio> 3 4 using namespace std; 5 6 long long n,t,a[1001],sum[1001]; 7 long long f[1001]; 8 9 inline long long min(long long s,long long d) { 10 if(s > d) return d; 11 return s; 12 } 13 14 int main() { 15 scanf("%d%d",&n,&t); 16 for(int i = 1;i <= n; i++) { 17 scanf("%d",&a[i]); 18 sum[i] = sum[i-1] + a[i]; 19 } 20 for(int i = 1;i <= n; i++) { 21 f[i] = a[i] * n * n + f[i-1]; 22 for(int j = i - 1;j >= 1; j--) 23 if(a[i] + a[j] <= t) 24 f[i] = min(f[i],f[j-1] + (a[j] + a[i]) * (sum[i] - sum[j-1])); 25 } 26 printf("%lld",f[n]); 27 return 0; 28 }