深圳网站维护,焦作网站制作,建设银行官网首页网站南山片区,wordpress评分中文版有 N 个任务排成一个序列在一台机器上等待执行#xff0c;它们的顺序不得改变。
机器会把这 N 个任务分成若干批#xff0c;每一批包含连续的若干个任务。
从时刻 0 开始#xff0c;任务被分批加工#xff0c;执行第 i 个任务所需的时间是 Ti。
另外#xff0c;在每批任…有 N 个任务排成一个序列在一台机器上等待执行它们的顺序不得改变。
机器会把这 N 个任务分成若干批每一批包含连续的若干个任务。
从时刻 0 开始任务被分批加工执行第 i 个任务所需的时间是 Ti。
另外在每批任务开始前机器需要 S 的启动时间故执行一批任务所需的时间是启动时间 S 加上每个任务所需时间之和。
一个任务执行后将在机器中稍作等待直至该批任务全部执行完毕。
也就是说同一批任务将在同一时刻完成。
每个任务的费用是它的完成时刻乘以一个费用系数 Ci。
请为机器规划一个分组方案使得总费用最小。
输入格式
第一行包含两个整数 N 和 S。
接下来 N 行每行有一对整数分别为 Ti和 Ci表示第 i 个任务单独完成所需的时间 Ti及其费用系数 Ci。
输出格式
输出一个整数表示最小总费用。
数据范围
1≤N≤3×105 0≤S,Ci≤512 −512≤Ti≤512
输入样例
5 1
1 3
3 2
4 3
2 3
1 4输出样例
153
解析 斜率优化dp
AcWing 302. 任务安排3算法提高课 - AcWing
#include cstring
#include iostream
#include algorithmusing namespace std;typedef long long LL;const int N 300010;int n, s;
LL t[N], c[N];
LL f[N];
int q[N];int main()
{scanf(%d%d, n, s);for (int i 1; i n; i ){scanf(%lld%lld, t[i], c[i]);t[i] t[i - 1];c[i] c[i - 1];}int hh 0, tt 0;q[0] 0;for (int i 1; i n; i ){int l hh, r tt;while (l r){int mid l r 1;if (f[q[mid 1]] - f[q[mid]] (t[i] s) * (c[q[mid 1]] - c[q[mid]])) r mid;else l mid 1;}int j q[r];f[i] f[j] - (t[i] s) * c[j] t[i] * c[i] s * c[n];while (hh tt (double)(f[q[tt]] - f[q[tt - 1]]) * (c[i] - c[q[tt - 1]]) (double)(f[i] - f[q[tt - 1]]) * (c[q[tt]] - c[q[tt - 1]])) tt -- ;q[ tt] i;}printf(%lld\n, f[n]);return 0;
}代码2
#includeiostream
#includestring
#includecstring
#includecmath
#includectime
#includealgorithm
#includeutility
#includestack
#includequeue
#includevector
#includeset
#includemath.h
#includemapusing namespace std;
typedef long long LL;
const int N 3e5 5;
int n, s;
LL c[N], t[N];
LL f[N];
int q[N];int main() {scanf(%d%d, n, s);for (int i 1; i n; i) {scanf(%lld%lld, t[i], c[i]);t[i] t[i - 1];c[i] c[i - 1];}int hh 0, tt 0;q[0] 0;for (int i 1; i n; i) {/*while (hh tt (f[q[hh 1]] - f[q[hh]]) (t[i] s) * (c[q[hh 1]] - c[q[hh]]))hh;*/int l hh, r tt,mid;while (l r) {mid lr 1;//cout mid endl;if (f[q[mid1]]-f[q[mid]](LL)(t[i]s)*(c[q[mid1]]-c[q[mid]])) {l mid 1;}else {r mid;}}int j q[r];f[i] f[j] - (t[i] s) * c[j] t[i] * c[i] s * c[n];while (hh tt (double)(f[q[tt]] - f[q[tt - 1]]) * (c[i] - c[q[tt]]) (double)(f[i] - f[q[tt]]) * (c[q[tt]] - c[q[tt - 1]]))tt--;//务必加上强制类型转换q[tt] i;}printf(%lld\n, f[n]);return 0;
}