题目链接:http://poj.org/problem?id=1180

题目描述:

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

 
看不懂题面的随便找一个翻译软件翻译一下效果都还是不错的,至少看得懂题
 
下面直接解题:
解法一:求出T,C的前缀和sumT和sumC,设f(i,j)表示把前i个任务分成j批去执行的最小费用,状态转移方程为
f(i,j)=min(0<=k<i){f(k,j-1)+(s*j+sumT[i])*(sumC[i]-sumC[k])}
时间复杂度为O(N3)
解法二:
本题其实没有规定把任务分成多少批,也就是说解法一其实有无用的状态
现在我们设f(i)表示把前i个任务分成若干批处理的最小费用,状态转移方程为
f[i]=min(0<=j<i){f[j]+sumT[i]*(sumC[i]-sumC[j])+s*(sumC[N]-sumC[j])}
这种思想叫做“费用提前计算”,先把每次s的贡献直接加起来
时间复杂度O(N2)
解法三:
我们考虑到这题的数据,对解法二的状态转移方程进行斜率优化。
去掉min,通过移项我们可以得到f[j]=(s+sumT[i])*sumC[j]+f[i]-sumT[i]*sumC[i]-s*sumC[N]
我们发现,在以sumC[j]为横坐标,f[j]为纵坐标的坐标系中,这是条以(s+sumT[i])为斜率,f[i]-sumT[i]*sumC[i]-s*sumC[N]为截距的直线
由于-sumT[i]*sumC[i]-s*sumC[N]是一个常数,斜率也是一个固定的值,这是一个线性规划问题,我们每次取最小的截距
对于点集(sumC[j],f[j])我们其实只需要维护一个下凸壳就行了。当我们需要找到当前的最优的点时,设k=s+sumT[i],最优的点和它左边的点的斜率比k小,和它右边的斜率比k大,参考下面的图。
 
另外我们还发现一点,由于sumC具有单调性,每次加入的点都会在最右边。并且sumT同样具有单调性,这说明斜率是递增的。因此我们只需要维护比当前斜率大的一条条线段,可以通过一个单调队列q来实现
具体操作如下:
1.检查队头的两个决策变量q[l]和q[l+1],斜率f[q[l+1]]-f[q[l]]/(sumC[q[l+1]]-sumC[q[l]])<=s+sumT[i],则把q[l]出队,继续检查新的队头。
2.直接取队头j=q[l]作为最优策略,执行状态转移,得到f[i]
3.把新决策i从队尾插入,在插入之前,若三个决策点j1=q[r-1],j2=q[r],j3=i不满足斜率单调递增(不满足下凸性,即i是无用决策),则直接从队尾把q[r]出队,继续检查新的队尾。
整个算法的时间复杂度O(N),完美的解决问题。
 
下面附上代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std; const int maxn=1e4+;
int n,s;
int sumt[maxn],sumc[maxn],q[maxn];
ll f[maxn];
int main()
{
scanf("%d%d",&n,&s);
for (int i=;i<=n;i++)
{
int t,c;
scanf("%d%d",&t,&c);
sumt[i]=sumt[i-]+t;
sumc[i]=sumc[i-]+c;
}
int l=,r=;
for (int i=;i<=n;i++)
{
while (l<r&&(f[q[l+]]-f[q[l]])<=(s+sumt[i])*(sumc[q[l+]]-sumc[q[l]])) l++;
f[i]=f[q[l]]-(s+sumt[i])*sumc[q[l]]+sumt[i]*sumc[i]+s*sumc[n];
while (l<r&&(f[q[r]]-f[q[r-]])*(sumc[i]-sumc[q[r]])>=(f[i]-f[q[r]])*(sumc[q[r]]-sumc[q[r-]])) r--;
q[++r]=i;
}
printf("%lld",f[n]);
return ;
}

声明:本博客内容参考李煜东算法竞赛进阶指南

POJ1180 Batch Scheduling 解题报告(斜率优化)的更多相关文章

  1. [POJ1180&POJ3709]Batch Scheduling&K-Anonymous Sequence 斜率优化DP

    POJ1180 Batch Scheduling Description There is a sequence of N jobs to be processed on one machine. T ...

  2. POJ-1180 Batch Scheduling (分组求最优值+斜率优化)

    题目大意:有n个任务,已知做每件任务所需的时间,并且每件任务都对应一个系数fi.现在,要将这n个任务分成若干个连续的组,每分成一个组的代价是完成这组任务所需的总时间加上一个常数S后再乘以这个区间的系数 ...

  3. POJ1180 Batch Scheduling -斜率优化DP

    题解 将费用提前计算可以得到状态转移方程: $F_i = \min(F_j + sumT_i * (sumC_i - sumC_j) + S \times (sumC_N - sumC_j)$ 把方程 ...

  4. poj1180 Batch Scheduling

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3590   Accepted: 1654 Description There ...

  5. 【LeetCode】1029. Two City Scheduling 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 小根堆 排序 日期 题目地址:https://lee ...

  6. P2365 任务安排 / [FJOI2019]batch(斜率优化dp)

    P2365 任务安排 batch:$n<=10000$ 斜率优化入门题 $n^{3}$的dp轻松写出 但是枚举这个分成多少段很不方便 我们利用费用提前的思想,提前把这个烦人的$S$在后面的贡献先 ...

  7. LeetCode :1.两数之和 解题报告及算法优化思路

    最近开始重拾算法,在 LeetCode上刷题.顺便也记录下解题报告以及优化思路. 题目链接:1.两数之和 题意 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 ...

  8. poj 1180 Batch Scheduling (斜率优化)

    Batch Scheduling \(solution:\) 这应该是斜率优化中最经典的一道题目,虽然之前已经写过一道 \(catstransport\) 的题解了,但还是来回顾一下吧,这道题其实较那 ...

  9. POJ 1180 Batch Scheduling(斜率优化DP)

    [题目链接] http://poj.org/problem?id=1180 [题目大意] N个任务排成一个序列在一台机器上等待完成(顺序不得改变), 这N个任务被分成若干批,每批包含相邻的若干任务. ...

随机推荐

  1. class.forName的官方使用方法说明

    原文地址:http://yanwushu.sinaapp.com/class_forname/ 使用jdbc方式链接数据库时会常常看到这句代码:Class.forName(String classNa ...

  2. bzoj1830: [AHOI2008]Y型项链(LCP+贪心)

    1830: [AHOI2008]Y型项链 题目:传送门 简要题意: 给出三个字符串,可以对任意字符串进行操作,每次操作都可以再其中一个字符串的末尾删除或添加一个字符,求最小操作数使得所有的字符串相同 ...

  3. string的一些操作

    //str.insert(1, "bbb"); //str.erase(5);//删除5以后的数字 //str.erase(str.begin()+2);//删除某个字符 //co ...

  4. java类型与Hadoop类型之间的转换

    java基本类型与Hadoop常见基本类型的对照Long LongWritableInteger   IntWritableBoolean   BooleanWritable String Text ...

  5. 项目结合activiti工作流框架使用

    项目结合activiti工作流框架使用: 1.项目与工作流框架的结合. 2.状态:草稿(待审批).审批中.审批通过.审批失败 3. 提交审批: 0 草稿(待审批),记录绑定工作流执行id,审批状态设置 ...

  6. python 3.x 学习笔记10 (析构函数and继承)

    1.类变量的用途:大家公用的属性,节省开销(内存) 2.析构函数 在实例释放和销毁的时候执行的,通常用于做一些收尾工作,如关闭一些数据库链接和打开的临时文件 3.私有方法两个下划线开头,声明该方法为私 ...

  7. JavaScript语法高亮库highlight.js使用

    highlight.js是一款基于JavaScript的语法高亮库,目前支持125种编程语言,有63种可供选择的样式,而且能够做到语言自动识别,和目前主流的JS框架都能兼容,可以混合使用. 这款高亮库 ...

  8. c#学习0216

    2017-03-02 out  关键字指定所给的参数为一个输出参数 该参数的值将返回给函数调用中使用的变量 注意事项 1未赋值的变量用作ref参数是非法的,但是可以把未赋值的变量用作out参数 2 在 ...

  9. POJ 2976 Dropping tests【二分 最大化平均值】

    题意:定义最大平均分为 (a1+a2+a3+---+an)/(b1+b2+---+bn),求任意去除k场考试的最大平均成绩 和挑战程序设计上面的最大化平均值的例子一样 判断是否存在x满足条件 (a1+ ...

  10. NYOJ 16 矩形嵌套【DP】

    解题思路:呃,是看的紫书上面的做法,一个矩形和另一个矩形之间的关系就只有两种,(因为它自己是不能嵌套自己的),可嵌套,不可嵌套,是一个二元关系,如果可嵌套的话,则记为1,如果不可嵌套的话则记为0,就可 ...