# 【斜率优化DP】Batch_Scheduling

Batch Scheduling
 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2272 Accepted: 1014

Description

There is a sequence of N jobs to be processed on one machine. The jobs are numbered from 1 to N, so that the sequence is 1,2,..., N. The sequence of jobs must be partitioned into one or more batches, where each batch consists of consecutive jobs in the sequence. The processing starts at time 0. The batches are handled one by one starting from the first batch as follows. If a batch b contains jobs with smaller numbers than batch c, then batch b is handled before batch c. The jobs in a batch are processed successively on the machine. Immediately after all the jobs in a batch are processed, the machine outputs the results of all the jobs in that batch. The output time of a job j is the time when the batch containing j finishes.

A setup time S is needed to set up the machine for each batch. For each job i, we know its cost factor Fi and the time Ti required to process it. If a batch contains the jobs x, x+1,... , x+k, and starts at time t, then the output time of every job in that batch is t + S + (Tx + Tx+1 + ... + Tx+k). Note that the machine outputs the results of all jobs in a batch at the same time. If the output time of job i is Oi, its cost is Oi * Fi. For example, assume that there are 5 jobs, the setup time S = 1, (T1, T2, T3, T4, T5) = (1, 3, 4, 2, 1), and (F1, F2, F3, F4, F5) = (3, 2, 3, 3, 4). If the jobs are partitioned into three batches {1, 2}, {3}, {4, 5}, then the output times (O1, O2, O3, O4, O5) = (5, 5, 10, 14, 14) and the costs of the jobs are (15, 10, 30, 42, 56), respectively. The total cost for a partitioning is the sum of the costs of all jobs. The total cost for the example partitioning above is 153.

You are to write a program which, given the batch setup time and a sequence of jobs with their processing times and cost factors, computes the minimum possible total cost.

Input

Your program reads from standard input. The first line contains the number of jobs N, 1 <= N <= 10000. The second line contains the batch setup time S which is an integer, 0 <= S <= 50. The following N lines contain information about the jobs 1, 2,..., N in that order as follows. First on each of these lines is an integer Ti, 1 <= Ti <= 100, the processing time of the job. Following that, there is an integer Fi, 1 <= Fi <= 100, the cost factor of the job.

Output

Your program writes to standard output. The output contains one line, which contains one integer: the minimum possible total cost.

Sample Input

5
1
1 3
3 2
4 3
2 3
1 4

Sample Output

153

Source

（因为G函数就相当于是斜率，根据sFi的增加，G是上凸的函数。但是G和i是无关的，可以这样理解，不管i取任何值，这整个图像的形状是不会变的。）

#include <cstdio>
#include <cmath>

typedef long long ll;

long n;long S;
long T[10010];
long F[10010];
ll ST[10010];
ll SF[10010];
long que[10010];
ll f[10010];

#define ST(a,b) (ST[a]-ST[b+1])

int main()
{
freopen("batch.in","r",stdin);
freopen("batch.out","w",stdout);
scanf("%ld%ld",&n,&S);
for (long i=1;i<n+1;i++)
{
scanf("%ld%ld",T+i,F+i);
}
for (long i=n;i>0;i--)
{
ST[i] = ST[i+1] + (ll)T[i];
SF[i] = SF[i+1] + (ll)F[i];
}
long l = 0;
long r = 0;
que[r] = n+1;
for (long i=n;i>0;i--)
{
while (l<r && f[que[l]]-f[que[l+1]] >= SF[i]*(ST[que[l]]-ST[que[l+1]])) l++;
f[i] = f[que[l]] + ((ll)S+ST(i,que[l]-1))*SF[i];
while (l<r && (f[que[r-1]]-f[que[r]])*(ST[que[r]]-ST[i]) >=
(f[que[r]]-f[i])*(ST[que[r-1]]-ST[que[r]])) --r;
que[++r] = i;
//		printf("%ld",f[1]);
}
printf("%I64d",f[1]);
return 0;
}


• 本文已收录于以下专栏：

## 斜率优化DP学习笔记

• Tag_king
• 2015年04月17日 10:19
• 1466

## 斜率优化dp小结

• lxc779760807
• 2016年05月10日 21:02
• 2752

## [模板] 斜率优化Dp详解

• Bill_Yang_2016
• 2017年01月22日 19:29
• 1567

## DP斜率优化总结

DP斜率优化总结 寒假事情比较多，刚回来的一周都是聚会，外加自己不务正业了几天浪费了大半的时间，春节前后还是抽空学习了一下斜率优化DP。 理论基础见NOI2004年周源的论...
• pi9nc
• 2013年07月27日 21:45
• 7620

## 动态规划（DP）优化之斜率优化讲解

“DP的斜率优化——对不必要的状态量进行抛弃，对不优的状态量进行搁置，使得在常数时间内找到最优解成为可能。斜率优化依靠的是数形结合的思想，通过将每个阶段和状态的答案反映在坐标系上寻找解答的单调性，来在...
• PomeCat
• 2017年06月01日 16:23
• 553

## 模型化理解单调队列优化和斜率优化DP

• Leo_h1104
• 2016年06月30日 20:32
• 1046

## 斜率优化总结&基础题表

• u010336344
• 2016年09月28日 21:42
• 2215

## POJ - 1180 Batch Scheduling(斜率优化DP)

• L123012013048
• 2015年10月07日 14:10
• 266

## 【笔记篇】斜率优化dp（三） APIO特别行动队

• Enzymii
• 2018年01月26日 07:13
• 52

## BZOJ 3675 斜率优化DP

• LZJ209
• 2016年12月26日 14:26
• 234

举报原因： 您举报文章：【斜率优化DP】Batch_Scheduling 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)