您的位置:首页 > 编程语言 > PHP开发

【斜率优化DP】Batch_Scheduling

2012-03-27 15:38 405 查看
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
IOI 2002

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

这道题应该算是比较有难度的,因为需要逆向思维,从最后一次往前推,会容易得多。
假如正向推的话,就必须要知道当前的组数,以便乘上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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息