Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7097 Accepted Submission(s): 3034
php
#define _CRT_SECURE_NO_DepRECATE #define _CRT_SECURE_NO_WARNINGS #include <cstdio> #include <iostream> #include <cmath> #include <iomanip> #include <string> #include <algorithm> #include <bitset> #include <cstdlib> #include <cctype> #include <iterator> #include <vector> #include <cstring> #include <cassert> #include <map> #include <queue> #include <set> #include <stack> #define ll long long #define INF 0x3f3f3f3f #define ld long double const ld pi = acos(-1.0L), eps = 1e-8; int qx[4] = { 0,0,1,-1 }, qy[4] = { 1,-1,0,0 }, qxx[2] = { 1,-1 }, qyy[2] = { 1,-1 }; using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; int hire, salary, fire, need[20], dp[20][300], max, ans; while (cin >> n && n) { memset(dp, 0, sizeof(dp)); max = 0; ans = INF; cin >> hire >> salary >> fire; for (int i = 0; i < n; i++) { cin >> need[i]; max = ::max(need[i], max);//記錄最多須要的人 } for (int i = need[0]; i <= max; i++)//先初始化第一個月須要的錢,枚舉從一月最少須要的人到最多須要的人 { dp[0][i] = i * (hire + salary); } for (int i = 1; i < n; i++) { for (int f = need[i]; f <= max; f++)//枚舉從最少須要的人到最多須要的人的狀況花費的錢 { dp[i][f] = INF; for (int h = need[i - 1]; h <= max; h++)//與上個月的每種狀況進行對比 { if (h <= f)//人數少於或等於須要的人數 { dp[i][f] = min(dp[i][f], dp[i - 1][h] + (f - h) * hire + f * salary); } else//人數多於須要的人數 { dp[i][f] = min(dp[i][f], dp[i - 1][h] + (h - f) * fire + f * salary); } } } } for (int i = need[n - 1]; i <= max; i++)//找到最小值 { ans = min(ans, dp[n - 1][i]); } cout << ans << endl; } return 0; }