【HDOJ】1158 Employment Planning
简单DP。
1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <climits> 5 #define MAXN 105 6 7 int dp[15][MAXN]; 8 int a[15]; 9 10 int getMin(int a, int b) { 11 return a<b?a:b; 12 } 13 14 int main() { 15 int n, max, min; 16 int i, j, k, r, tmp, ans; 17 int hire, salary, fire; 18 #ifndef ONLINE_JUDGE 19 freopen("data.in", "r", stdin); 20 #endif 21 while (scanf("%d",&n)!=EOF && n) { 22 scanf("%d %d %d", &hire, &salary, &fire); 23 max = INT_MIN; 24 min = INT_MAX; 25 for (i=1; i<=n; ++i) { 26 scanf("%d", &a[i]); 27 if (a[i] > max) 28 max = a[i]; 29 if (a[i] < min) 30 min = a[i]; 31 } 32 for (r=1; r<=n; ++r) { 33 for (i=a[r]; i<=max; ++i) { 34 if (r == 1) { 35 dp[r][i] = i*(hire+salary); 36 continue; 37 } 38 ans = INT_MAX; 39 for (j=a[r-1]; j<=max; ++j) { 40 tmp = dp[r-1][j] + i*salary; 41 if (j >= i) { 42 ans = getMin(ans, tmp+(j-i)*fire); 43 } else { 44 ans = getMin(ans, tmp+(i-j)*hire); 45 } 46 } 47 dp[r][i] = ans; 48 } 49 } 50 ans = INT_MAX; 51 for (i=a[n]; i<=max; ++i) 52 if (dp[n][i] < ans) 53 ans = dp[n][i]; 54 printf("%d\n", ans); 55 } 56 57 return 0; 58 }