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; }