hdu 3738 The Sweat Shop 单调栈加贪心思想
来源:互联网 发布:js input 属性的值 编辑:程序博客网 时间:2024/05/16 11:11
The Sweat Shop
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 90 Accepted Submission(s): 28
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.
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)
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
思路: 单调不增栈中存的是满足i-1天的可能人数,相当于一个优化。第i天时,只用遍历栈中比a[i]小的和第一个比a[i]大的即可,不需要雇佣更多的人。
#include <iostream>#include <stdio.h>#include <string>#include <cstring>#include <cmath>#include <algorithm>#include <queue>typedef __int64 ll;using namespace std;ll a[100009];ll q[100009];ll dp[100009];int main(){ int T,n; ll x,y,z; scanf("%d",&T); while(T--) { scanf("%d",&n); scanf("%I64d%I64d%I64d",&x,&y,&z); for(int i=1;i<=n;i++) scanf("%I64d",&a[i]); int t=0; q[t]=0; a[0]=0; dp[0]=0; for(int i=1;i<=n;i++) { dp[i]=1ll<<60; while(t>=0 && a[i]>a[q[t]]) { ll tmp=dp[q[t]]+a[q[t]]*z*(i-q[t]); tmp+=(a[i]-a[q[t]])*(x+z); dp[i]=min(dp[i],tmp); t--; } if(t>=0)//处理第一个比当前大的情况 { ll tmp=dp[q[t]]+(a[q[t]]-a[i])*y+a[i]*(i-q[t])*z; dp[i]=min(dp[i],tmp); } q[++t]=i; } dp[n]+=a[n]*y; printf("%I64d\n",dp[n]); } return 0;}
0 0
- hdu 3738 The Sweat Shop 单调栈加贪心思想
- hdu 3738 The Sweat Shop(dp)
- HDU 1506 单调栈 (dp思想)
- hdu 4105 贪心思想
- hdu 4982(贪心思想)
- HDU 3410 Passing the Message (单调栈)
- HDU 4122--Alice's mooncake shop(单调队列)
- 单调队列-Hdu-4122-Alice's mooncake shop
- hdu 4122Alice's mooncake shop(单调队列)
- hdu 4122 Alice's mooncake shop(单调队列)
- hdu 4122 Alice's mooncake shop(单调队列)
- HDU - 4122 Alice's mooncake shop 单调队列
- 浅谈单调栈思想
- Hdu 1051(贪心思想)
- HDU 6205 card【单调队列思想+暴力】
- hdu 5802 搜索加贪心
- HDU-5662 YJQQQAQ and the function (枚举&&单调栈)
- 文章标题 HDU 3410 : Passing the Message (单调栈)
- 咒语。
- 欢迎使用CSDN-markdown编辑器
- 在 Unity 中使用 Android SDK
- 解读Android之Activity(1)基础知识
- 数据持久化
- hdu 3738 The Sweat Shop 单调栈加贪心思想
- Windows脚本-Tomcat部署WAR
- mongodb and 和 or语句写法
- HDU1166敌兵布阵(线段树or树状数组)
- android:padding和android:layout_margin的区别
- 数据设计是程序架构的灵魂
- 《从零开始学Swift》学习笔记(Day5)——我所知道的标识符和关键字
- [Java]哲学家就餐问题
- AS(强直性脊柱炎)完全手册