POJ1180 Batch Scheduling -斜率优化DP
题解
将费用提前计算可以得到状态转移方程: $F_i = \min(F_j + sumT_i * (sumC_i - sumC_j) + S \times (sumC_N - sumC_j)$
把方程进行分离, 得到 $S\times sumC_j + F_j = sumT_i \times sumC_j + F_i - S \times sumC_N$。
将等号左边看成纵坐标, $sumC_j$看成横坐标, $sumT_i$为斜率来进行斜率优化。
由于 $sumT_i$是递增的, 即斜率是递增的, 维护一个单调队列, 第一个斜率大于$sumT_i$的端点就为决策点
斜率优化dp还是很套路的
代码
#include<cstdio>
#include<algorithm>
#include<cstring>
#define rd read()
#define rep(i,a,b) for( int i = (a); i <= (b); ++i )
#define per(i,a,b) for( int i = (a); i >= (b); --i )
using namespace std; const int N = 1e4 + 1e3; int n, m, sumT[N], sumC[N], S, f[N], q[N]; int cross(int a, int b, int c) { //点积
int ax = sumC[a], bx = sumC[b], cx = sumC[c];
int ay = f[a], by = f[b], cy = f[c];
int x = bx - ax, xx = cx - ax, y = by - ay, yy = cy - ay;
return x * yy - xx * y;
}// 向量ab, ac double calk(int a, int b) {
int ax = sumC[a], bx = sumC[b], ay = f[a], by = f[b];
return 1.0 * (by - ay) / (bx - ax);
} int read() {
int X = , p = ;char c = getchar();
for(; c > '' || c < ''; c = getchar() ) if( c == '-' ) p = -;
for(; c >= '' && c <= ''; c = getchar() ) X = X * + c - '';
return X * p;
} int main()
{
n = rd; S = rd;
rep(i, , n) {
int t = rd, c = rd;
sumT[i] = sumT[i - ] + t;
sumC[i] = sumC[i - ] + c;
}
memset(f, 0x3f, sizeof(f));
int l = , r = ;
q[] = f[] = ;
rep(i, , n) {
while(l < r && calk(q[l], q[l + ]) <= S + sumT[i]) l++;
if(l <= r) f[i] = f[q[l]] + sumT[i] * (sumC[i] - sumC[q[l]]) + S * (sumC[n] - sumC[q[l]]);
while(l < r && cross(q[r - ], q[r], i) <= ) r--;
q[++r] = i;
}
printf("%d\n",f[n]);
}
POJ1180 Batch Scheduling -斜率优化DP的更多相关文章
- P2365 任务安排 / [FJOI2019]batch(斜率优化dp)
P2365 任务安排 batch:$n<=10000$ 斜率优化入门题 $n^{3}$的dp轻松写出 但是枚举这个分成多少段很不方便 我们利用费用提前的思想,提前把这个烦人的$S$在后面的贡献先 ...
- [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 ...
- 斜率优化dp(POJ1180 Uva1451)
学这个斜率优化dp却找到这个真心容易出错的题目,其中要从n倒过来到1的确实没有想到,另外斜率优化dp的算法一开始看网上各种大牛博客自以为懂了,最后才发现是错了. 不过觉得看那些博客中都是用文字来描述, ...
- 【转】斜率优化DP和四边形不等式优化DP整理
(自己的理解:首先考虑单调队列,不行时考虑斜率,再不行就考虑不等式什么的东西) 当dp的状态转移方程dp[i]的状态i需要从前面(0~i-1)个状态找出最优子决策做转移时 我们常常需要双重循环 (一重 ...
- 【学习笔记】动态规划—斜率优化DP(超详细)
[学习笔记]动态规划-斜率优化DP(超详细) [前言] 第一次写这么长的文章. 写完后感觉对斜优的理解又加深了一些. 斜优通常与决策单调性同时出现.可以说决策单调性是斜率优化的前提. 斜率优化 \(D ...
- bzoj-4518 4518: [Sdoi2016]征途(斜率优化dp)
题目链接: 4518: [Sdoi2016]征途 Description Pine开始了从S地到T地的征途. 从S地到T地的路可以划分成n段,相邻两段路的分界点设有休息站. Pine计划用m天到达T地 ...
- bzoj-1096 1096: [ZJOI2007]仓库建设(斜率优化dp)
题目链接: 1096: [ZJOI2007]仓库建设 Description L公司有N个工厂,由高到底分布在一座山上.如图所示,工厂1在山顶,工厂N在山脚.由于这座山处于高原内陆地区(干燥少雨),L ...
- [BZOJ3156]防御准备(斜率优化DP)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3156 分析: 简单的斜率优化DP
- 【BZOJ-1096】仓库建设 斜率优化DP
1096: [ZJOI2007]仓库建设 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3719 Solved: 1633[Submit][Stat ...
随机推荐
- solr .Net端(SolrNet)
首先 引用SolrNet.dll Microsoft.Practices.ServiceLocation HttpWebAdapters 也可以用.net IDe 中的 nuget下载 solrnet ...
- Python预编译语句防止SQL注入
这个月太忙,最近不太太平,我的愿望是世界和平! ================================== 今天也在找python的预编译,早上写的sql是拼接来构成的.于是找了2篇文章,还 ...
- Executor框架(五)Executors工厂类
Executors 简介 Executors 是一个工厂类,其提供的是Executor.ExecutorService.ScheduledExecutorService.ThreadFactory 和 ...
- Python常量工具类
1.定义常量类constant.py # -*- coding: utf-8 -* """常量工具类 author: Jill usage: from constant ...
- 28. centos 5.6添加用户时报copydir(): preserving permissions for /home/xxx/.mozilla: Operation not supported错
当执行useradd xxx报如下错:copydir(): preserving permissions for /home/xxx/.mozilla: Operation not supported ...
- 怎么才能将文件流或者图片转化为base64,传到前台展示
图片转化为base64,传到前台展示 public String getBase64(){ String imgStr = ""; try { File file = new Fi ...
- VBA 浏览文件夹
Private Function SelectFolder() As String With Application.FileDialog(msoFileDialogFolderPick ...
- iOS 真机调试 Xcode 显示 device Error: device unavailable
一般来说有两个原因: 1. iphone没有加到test device里,将iphone的设备id加到test device列表里 2. Xcode不支持当前的iOS版本,将Xcode升级到最新版
- ubuntu wifi连接不上或经常断网,重启就好 [ 转]
转自 http://blog.csdn.net/chinabing/article/details/47184093 问题描述:最近安装了win7和ubuntu 14.04.2双系统,每次进入ubun ...
- a,b = b,a 换值问题
a = "hello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhe ...