【斜率优化DP】Batch_Scheduling

原创 2012年03月27日 15:38:09
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



做了这道题,才感觉自己斜率优化入门了。


这道题应该算是比较有难度的,因为需要逆向思维,从最后一次往前推,会容易得多。

假如正向推的话,就必须要知道当前的组数,以便乘上S。状态会多出一维,不便处理。

假设逆向推的话,则可以迭代,当前增加,则把后面组全都增加,最后推到第一组的时候,刚好最后一组加了组数那么多个S,其他类似。



这道题是经指点做的,因此比较顺利,但是后来有一个地方减号打成了乘号,硬是调了一上午。。。。。。

还有一个地方,纠结了很久,就是比较J和J‘的G函数g(i,j)、g(i,j')的时候,我想用除法化乘法来减小误差,我以为是负数所以要变号。。。结果两个分母都是负数,不变号。。。数学水平真的很重要呀(就是最后一个不等式处)

有个地方弄明白了,就是分离参数必须一边与i无关,一边只和i有关。否则就不能用斜率优化。

(因为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;
}


相关文章推荐

poj 1180 Batch Scheduling ( 斜率优化DP )

/* N个任务排成一个序列在一台机器上等待完成(顺序不得改变),这N个任务被分成若干批,每批包含相邻的若干任务。 从时刻0开始,这些任务被分批加工,第i个任务单独完成所需的时间是Ti。在每批任务开始...
  • SYLG_li
  • SYLG_li
  • 2012年09月29日 14:09
  • 423

poj1180Batch Scheduling【斜率优化dp】

Description There is a sequence of N jobs to be processed on one machine. The jobs are numbered f...

poj 1180 Batch Scheduling 斜率优化dp

poj 1180 Batch Scheduling  http://poj.org/problem?id=1180 题意:N个任务排成一个序列在一台机器上等待完成(顺序不得改变),这N个任...

Poj1180 Batch Scheduling --- DP的斜率优化

题意 N个任务排成一个序列在一台机器上等待完成(顺序不得改变),这N个任务被分成若干批,每批包含相邻的若干任务。从时刻0开始,这些任务被分批加工,第i 个任务单独完成所需的时间是Ti。在每批任务开始...

poj 1180 Batch Scheduling(DP-单调性优化)

poj 1180 Batch Scheduling(DP-单调性优化 斜率优化)

bzoj 1597: [Usaco2008 Mar]土地购买(斜率优化dp 例题)

Description农夫John准备扩大他的农场,他正在考虑N (1

[BZOJ3963][WF2011][CDQ分治][斜率优化][DP]MachineWorks

被精度卡死……令d[i]为第i台机器可以购买的日期,f[i]表示第d[i]天的最大收入, 则f[i]=max{f[j]-p[j]+r[j]+(d[i]-d[j]-1)*g[j]} 令A[i]=f[...
  • Coldef
  • Coldef
  • 2017年02月17日 22:27
  • 124

hdu3045 Picnic Cows(斜率优化DP)

点击打开链接 题意: 有n个奶牛分别有对应的兴趣值,现在对奶牛分组,每组成员不少于t,    在每组中所有的成员兴趣值要减少到该组的最小值,问总共最少需要减少的兴趣值是多少。 思路:   ...
  • yxg_123
  • yxg_123
  • 2017年02月23日 01:58
  • 83

[hdu3045] Picnic Cows DP斜率优化

Picnic CowsProblem Description It’s summer vocation now. After tedious milking, cows are tired and ...

CSU-1963 Feed the rabbit(斜率优化dp)

1963: Feed the rabbit Submit Page     Summary    Time Limit: 2 Sec     Memory Limit: 128 Mb     S...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:【斜率优化DP】Batch_Scheduling
举报原因:
原因补充:

(最多只允许输入30个字)