POJ-1180 Batch Scheduling (分组求最优值+斜率优化)
题目大意:有n个任务,已知做每件任务所需的时间,并且每件任务都对应一个系数fi。现在,要将这n个任务分成若干个连续的组,每分成一个组的代价是完成这组任务所需的总时间加上一个常数S后再乘以这个区间的系数和。求最小代价。
题目分析:分组求最优值得问题。不过,这道题采用倒推可能要好做一些。定义状态dp(i)表示完成从第 i 个任务到第n个任务需要的最小代价,则状态转移方程为
dp(i)=min(dp(j)+(sumt(i)-sumt(j)+s)*sumf(i),很显然的要用斜率优化。
代码如下:
# include<iostream>
# include<cstdio>
# include<cstring>
# include<algorithm>
using namespace std;
# define LL long long const int INF=1<<30;
const int N=10005; int n,m;
int q[N];
int t[N];
int f[N];
int dp[N]; void read(int &x)
{
char ch=' ';
while(ch<'0'||ch>'9')
ch=getchar();
x=0;
while(ch>='0'&&ch<='9'){
x=x*10+ch-'0';
ch=getchar();
}
} void init()
{
for(int i=0;i<n;++i){
read(t[i]);
read(f[i]);
}
t[n]=f[n]=0;
for(int i=n-1;i>=0;--i){
t[i]+=t[i+1];
f[i]+=f[i+1];
}
} double getK(int i,int j)
{
return (double)(dp[i]-dp[j])/(double)(t[i]-t[j]);
} int toDp(int i,int j)
{
return dp[j]+(t[i]-t[j]+m)*f[i];
} int solve()
{
int head=0,tail=-1;
dp[n]=0;
q[++tail]=n;
for(int i=n-1;i>=0;--i){
while(head+1<=tail&&getK(q[head+1],q[head])<=(double)f[i])
++head;
dp[i]=toDp(i,q[head]);
while(head+1<=tail&&getK(i,q[tail])<=getK(q[tail],q[tail-1]))
--tail;
q[++tail]=i;
}
return dp[0];
} int main()
{
while(~scanf("%d%d",&n,&m))
{
init();
printf("%d\n",solve());
}
return 0;
}
POJ-1180 Batch Scheduling (分组求最优值+斜率优化)的更多相关文章
- POJ 1180 Batch Scheduling(斜率优化DP)
[题目链接] http://poj.org/problem?id=1180 [题目大意] N个任务排成一个序列在一台机器上等待完成(顺序不得改变), 这N个任务被分成若干批,每批包含相邻的若干任务. ...
- poj 1180 Batch Scheduling (斜率优化)
Batch Scheduling \(solution:\) 这应该是斜率优化中最经典的一道题目,虽然之前已经写过一道 \(catstransport\) 的题解了,但还是来回顾一下吧,这道题其实较那 ...
- poj 1180:Batch Scheduling【斜率优化dp】
我会斜率优化了!这篇讲的超级棒https://blog.csdn.net/shiyongyang/article/details/78299894?readlog 首先列个n方递推,设sf是f的前缀和 ...
- POJ 1180 Batch Scheduling
BTW: 刚在图书馆借了本算法艺术与信息学竞赛. 我多次有买这本书的冲动, 但每次在试看之后就放弃了, 倒不是因为书太难, 而是写的实在是太差. 大家对这本书的评价很高, 我觉得多是因为书的内容, 而 ...
- POJ 1180 - Batch Scheduling - [斜率DP]
题目链接:http://poj.org/problem?id=1180 Description There is a sequence of N jobs to be processed on one ...
- POJ 1180 Batch Scheduling (dp,双端队列)
#include <iostream> using namespace std; + ; int S, N; int T[MAX_N], F[MAX_N]; int sum_F[MAX_N ...
- 【转】斜率优化DP和四边形不等式优化DP整理
(自己的理解:首先考虑单调队列,不行时考虑斜率,再不行就考虑不等式什么的东西) 当dp的状态转移方程dp[i]的状态i需要从前面(0~i-1)个状态找出最优子决策做转移时 我们常常需要双重循环 (一重 ...
- POJ1180 Batch Scheduling 解题报告(斜率优化)
题目链接:http://poj.org/problem?id=1180 题目描述: There is a sequence of N jobs to be processed on one machi ...
- [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 ...
随机推荐
- 手撕vue-cli配置文件——check-versions.js篇
check-versions.js,vue-cli中检查版本的js文件. 'use strict' const chalk = require('chalk') const semver = requ ...
- JavaScript计算星期几
function zeller(dateStr) { var c = parseInt(dateStr.substr(0, 2)); var y = parseInt(dateStr.substr(2 ...
- MySQL Crash Course #08# Chapter 16. Using Different Join Types
记文档还是相当重要的! 索引 假名的三个用途 自交(Self Joins) 自然交(Natural Joins) Outer Joins Using Table Aliases Using alias ...
- shell分析http日志
http状态码1字头----信息,服务器收到请求,需要请求者继续执行操作2字头----成功,操作被成功接收并处理3字头----重定向,需要进一步的操作以完成请求4字头----客户端错误,请求包含语法错 ...
- CF#338D. GCD Table
传送门 简单的中国剩余定理练习. 首先行数一定是$lcm$,然后只要确定最小的列数就能判定解合不合法了. 我们可以得到线性模方程组: $y \equiv 0 \pmod{a_1}$ $y+1 \equ ...
- 20145127 《Java程序设计》第五次实验报告
实验简述: 在本周,我们进行了Java的第五次试验,本次实验的主要内容是结对编程.本次实验的大体过程是: 1.先进行Java的客户端与服务端的代码编写.结对是两个人,一人负责客户端,一人负责服务端. ...
- Python3基础 hasattr 测试类是否有指定的类属性
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- fastjson 简单使用 及其JSONObject使用
阿里巴巴FastJson是一个Json处理工具包,包括“序列化”和“反序列化”两部分,它具备如下特征:速度最快,测试表明,fastjson具有极快的性能,超越任其他的Java Json parser. ...
- HDU 1796 How many integers can you find(容斥)题解
思路:二进制解决容斥问题,就和昨天做的差不多.但是这里题目给的因子不是质因子,所以我们求多个因子相乘时要算最小公倍数.题目所给的因数为非负数,故可能有0,如果因子为0就要删除. 代码: #includ ...
- UVA 3942 Remember the Word (Trie+DP)题解
思路: 大白里Trie的例题,开篇就是一句很容易推出....orz 这里需要Trie+DP解决. 仔细想想我们可以得到dp[i]=sum(dp[i+len[x]]). 这里需要解释一下:dp是从最后一 ...