hdu 3738 The Sweat Shop(dp)

来源:互联网 发布:血色衣冠4.0数据出错 编辑:程序博客网 时间:2024/05/16 07:11

The Sweat Shop

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 35 Accepted Submission(s): 15


Problem Description
Many people spend thier lives in sweat shop in China. The boss of one of the famous sweat shop, GXX, is considering about the employment plan of her company.
He already knows that in the next N days, how many worker are needed for this day's work, a[i]. In additional, he will spend x yuan on recruit a worker, y yuan on fire a worker. If a worker spend a day on work in his company, GXX must pay z yuan for the wages per day.
GXX has no work before the first day and he decide to fire all the workers after these N days. Now comes to the question, how much he will pay on the employment plan in minimum.

Input
The first line contains only one integer T, denoting the number of test cases.
For each test cases, the first line contains only one integer N, denoting the number of days. (1 <= N <= 100000)
The next line contains three integers, x, y and z, denoting the amount of spend on recruit, fire a worker, and the daily salary of a worker. (1 <= x,y,z <= 100000)
The third line contains N integers, a[i], denoting the minimum number of workers needed for the i-th day. (1 <= a[i] <= 100000)

Output
For each test cases, output a single number denoting the minimum amount GXX should spend on employment plan.

Sample Input
1310 10 12 1 2

Sample Output
46

Author
永远的魔灵

Source
ACMDIY第二届群赛


#include <stdio.h>#include <string.h>#include <math.h>#include <stdlib.h>#define N 100010int n;__int64  a[N], x, y, z;int q[N], ed;__int64 min(__int64 a,__int64 b ){ return a<b?a:b;   }void solve() {    __int64 res = a[0] * x + a[n - 1] * y;    for (int i = 0; i < n; ++i) {        res += a[i] * z;        if(i) {            if (a[i - 1] > a[i])                res += (a[i - 1] - a[i]) * y;            else                res += (a[i] - a[i - 1]) * x;        }    }    ed = 0;    q[ed++] = 0;    for (int i = 1; i < n; ++i) {        if (a[q[ed - 1]] >= a[i]) q[ed++] = i;        else {            while (ed > 1 && a[q[ed - 1]] < a[i]) {                --ed;               /* t是解雇工人的数目 ,                  tmp---是留着这个工人省钱还是解雇这个工人省的钱 ??                */                __int64 tmp, t = min(a[i], a[q[ed - 1]]) - a[q[ed]];                                if ((tmp = (x + y - (i - q[ed - 1] - 1) * z) * t) > 0) {                                         res -= tmp;                     //printf("\n........\nq[ed]=%d l=%d r=%d tmp=%d\n",q[ed],q[ed - 1],i,tmp);                }            }            if (a[q[ed - 1]] <= a[i]) --ed;            q[ed++] = i;                    }    }    printf("%I64d\n", res);}int main() {    freopen("in.txt","r",stdin);  freopen("out.txt","w",stdout);    int cas;    scanf("%d", &cas);    while (cas--) {    scanf("%d", &n);    scanf("%I64d%I64d%I64d", &x, &y, &z);    for (int i = 0; i < n; ++i) {        scanf("%I64d", &a[i]);    }        solve();    }    return 0;}


原创粉丝点击