[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. 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.
program poj1180;
const maxn=;
var i,head,tail,n,s:longint;
opt,dp,f,t:array[-..maxn]of int64; function g(x,y:longint):extended;
begin
exit((dp[x]-dp[y])/(t[x]-t[y]));
end; begin
readln(n);
readln(s);
for i:= to n do readln(t[i],f[i]);
for i:=n- downto do
begin
inc(t[i],t[i+]);
inc(f[i],f[i+]);
end;
t[n+]:=;f[n+]:=;
head:=;tail:=;opt[]:=n+;dp[n+]:=;
for i:=n downto do
begin
while (head<tail)and(g(opt[head+],opt[head])<f[i]) do inc(head);
dp[i]:=dp[opt[head]]+(s+t[i]-t[opt[head]])*f[i];
while (head<tail)and(g(opt[tail],opt[tail-])>g(i,opt[tail])) do dec(tail);
inc(tail);opt[tail]:=i;
end;
writeln(dp[]);
end.
POJ3709 K-Anonymous Sequence
Description
The explosively increasing network data in various application domains has raised privacy concerns for the individuals involved. Recent studies show that simply removing the identities of nodes before publishing the graph/social network data does not guarantee privacy. The structure of the graph itself, along with its basic form the degree of nodes, can reveal the identities of individuals.
To address this issue, we study a specific graph-anonymization problem. We call a graph k-anonymous if for every node v, there exist at least k-1 other nodes in the graph with the same degree asv. And we are interested in achieving k-anonymous on a graph with the minimum number of graph-modification operations.
We simplify the problem. Pick n nodes out of the entire graph G and list their degrees in ascending order. We define a sequence k-anonymous if for every element s, there exist at least k-1 other elements in the sequence equal to s. To let the given sequence k-anonymous, you could do one operation only—decrease some of the numbers in the sequence. And we define the cost of the modification the sum of the difference of all numbers you modified. e.g. sequence 2, 2, 3, 4, 4, 5, 5, with k=3, can be modified to 2, 2, 2, 4, 4, 4, 4, which satisfy 3-anonymous property and the cost of the modification will be |3-2| + |5-4| + |5-4| = 3.
Give a sequence with n numbers in ascending order and k, we want to know the modification with minimal cost among all modifications which adjust the sequence k-anonymous.
也是一道斜率优化的拓展题。首先我们可以非常熟练地推出斜率:
g[j,k]=(f[j]-s[j]+j*a[j]-(f[k]-s[k]+k*a[k]))/(a[j]-a[k])
推的过程大同小异这里就不详细列出了。
然后转移方程
f[i]=f[j]+s[i]-s[j]-(i-j)*a[j]
(a[j]=s[j+1])(为了形式更优美我们把下标换成j当然不换也没有什么关系)
这道题的问题有两个
其中一个是k要怎么控制?刚开始想了一个并不好的方法就是在求解的时候各种控制但是还要担心缩tail的时候会影响后面的答案...
其实只要从DP的角度考虑,将i点加入队列无非就是给i以后的点增加一个可转移的状态
那么只要保证当前在求i的时候,i-k+1..i-1的点不在单调队列里就行了
自然而然地想到了延迟入队这样问题就迎刃而解了
在上一道题中,保证每个零件的加工时间都是非负整数,因此表示前缀和的t数组数字各不相同
而这道题不一样,作为分母的a数组可能相同,所以斜率还要特判分母等于0的情况
刚开始就是这里出了错。返回值要根据分子的符号来决定是正无穷还是负无穷。
program poj3709;
const maxn=;INF=;
var t,test,n,k,head,tail,v,i:longint;
s,a,opt,f:array[-..maxn]of int64; function g(x,y:longint):extended;
begin
if a[x]<>a[y] then exit((f[x]-s[x]+x*a[x]-f[y]+s[y]-y*a[y])/(a[x]-a[y]));
if (f[x]-s[x]+x*a[x]-f[y]+s[y]-y*a[y])> then exit(INF) else exit(-INF);
//如果分子是正的就返回正无穷否则返回负无穷
end; begin
readln(test);
for t:= to test do
begin
readln(n,k);
for i:= to n do read(s[i]);s[n+]:=;
for i:= to n do a[i]:=s[i+];
for i:= to n do inc(s[i],s[i-]);
head:=;tail:=;opt[]:=;s[]:=;f[]:=;
for i:=k to n do
//这里我控制的目的是不要让<k的无用状态将head移向后面
begin
while (head<tail)and(g(opt[head+],opt[head])<i) do inc(head);
f[i]:=f[opt[head]]+(s[i]-s[opt[head]])-(i-opt[head])*a[opt[head]];
if i-k+>=k then
//为下一次作的准备,i+1的时候需要i+1-k的状态。而且显然<k的状态是不能被转移的
begin
while (head<tail)and(g(opt[tail],opt[tail-])>g(i-k+,opt[tail])) do dec(tail);
inc(tail);opt[tail]:=i-k+;
end;
end;
writeln(f[n]);
end;
end.
[POJ1180&POJ3709]Batch Scheduling&K-Anonymous Sequence 斜率优化DP的更多相关文章
- POJ 3709 K-Anonymous Sequence - 斜率优化dp
描述 给定一个数列 $a$, 分成若干段,每段至少有$k$个数, 将每段中的数减少至所有数都相同, 求最小的变化量 题解 易得到状态转移方程 $F_i = \min(F_j + sum_i - su ...
- POJ3709 K-Anonymous Sequence 斜率优化DP
POJ3709 题意很简单 给n个递增整数(n<=500000)和一种操作(选择任意个数 使他们减少整数值) 使得对于所有的整数 在数列中 有k个相等的数 O(n^2)的DP方程很容易得出 如下 ...
- POJ1180 Batch Scheduling -斜率优化DP
题解 将费用提前计算可以得到状态转移方程: $F_i = \min(F_j + sumT_i * (sumC_i - sumC_j) + S \times (sumC_N - sumC_j)$ 把方程 ...
- 斜率优化dp(POJ1180 Uva1451)
学这个斜率优化dp却找到这个真心容易出错的题目,其中要从n倒过来到1的确实没有想到,另外斜率优化dp的算法一开始看网上各种大牛博客自以为懂了,最后才发现是错了. 不过觉得看那些博客中都是用文字来描述, ...
- 【转】斜率优化DP和四边形不等式优化DP整理
(自己的理解:首先考虑单调队列,不行时考虑斜率,再不行就考虑不等式什么的东西) 当dp的状态转移方程dp[i]的状态i需要从前面(0~i-1)个状态找出最优子决策做转移时 我们常常需要双重循环 (一重 ...
- 【学习笔记】动态规划—斜率优化DP(超详细)
[学习笔记]动态规划-斜率优化DP(超详细) [前言] 第一次写这么长的文章. 写完后感觉对斜优的理解又加深了一些. 斜优通常与决策单调性同时出现.可以说决策单调性是斜率优化的前提. 斜率优化 \(D ...
- BZOJ 1010: [HNOI2008]玩具装箱toy 斜率优化DP
1010: [HNOI2008]玩具装箱toy Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再 ...
- BZOJ 3156: 防御准备 斜率优化DP
3156: 防御准备 Description Input 第一行为一个整数N表示战线的总长度. 第二行N个整数,第i个整数表示在位置i放置守卫塔的花费Ai. Output 共一个整数,表示最小的战 ...
- HDU2829 Lawrence(斜率优化dp)
学了模板题之后上网搜下斜率优化dp的题目,然后就看到这道题,知道是斜率dp之后有思路就可以自己做不出来,要是不事先知道的话那就说不定了. 题意:给你n个数,一开始n个数相邻的数之间是被东西连着的,对于 ...
随机推荐
- 27、理解js的继承机制(转载自阮一峰)
Javascript继承机制的设计思想 作者: 阮一峰 日期: 2011年6月 5日 我一直很难理解Javascript语言的继承机制. 它没有"子类"和"父类&qu ...
- 自动化测试--封装JDBCUnit
在进行测试的时候,经常需要对数据库进行操作.我们知道,通过代码与数据库交互,需要以下几步: 1.加载驱动 之前有盆友问我,为什么Selenium操作浏览器的时候,非要下载浏览器驱动?为啥对数据库进行操 ...
- LeetCode 4——两个排序数组中的中位数
1. 题目 2. 解答 2.1. 方法一 由于两个数组都是排好序的,因此首先可以想到的思路就是利用归并排序把两个数组合并成一个有序的长数组,然后直接取出中位数即可. class Solution: d ...
- 扩展欧几里得 求ax+by == n的非负整数解个数
求解形如ax+by == n (a,b已知)的方程的非负整数解个数时,需要用到扩展欧几里得定理,先求出最小的x的值,然后通过处理剩下的区间长度即可得到答案. 放出模板: ll gcd(ll a, ll ...
- truffle的调用nodeJs的问题
Truffle3.0集成NodeJS并完全跑通(附详细实例,可能的错误) 升级到Truffle3.0 如果之前安装的是Truffle2.0版本,需要主动升级到Truffle3.0,两者的语法变化有点大 ...
- IE浏览器报Promise未定义的错误
背景: 一个vue-cli构建的vue项目,一个使用angular的项目,两个项目在其他浏览器一切正常,但是ie中会报Promise未定义的错误 解决办法: vue的项目: 1. npm insta ...
- Delphi xe7组件和控件的安装方法
暂时我所遇到的所有控件安装方法大体与下面两种相同. 若有不同大家提出来,一起想办法解决. .dproj格式的组件安装方法: raise组件 安装详细步骤如下: 一.设置搜索路径1. 将本包中的文件连同 ...
- sqoop工具从oracle导入数据2
sqoop工具从oracle导入数据 sqoop工具是hadoop下连接关系型数据库和Hadoop的桥梁,支持关系型数据库和hive.hdfs,hbase之间数据的相互导入,可以使用全表导入和增量导入 ...
- ScrollBarsEnabled的使用
在WinForm中通过WebBrowser获取网页,我想把WebBrowser的ScollBar去掉,我的网页不需要滚动条. 设置方法如下:单击WebBrowser设计页面,在属性页面有一个Scrol ...
- Charles 抓包发现自动跳转为https 问题梳理
今天遇到个有点意思的问题.特此记录. 业务场景: 做了一个页面,但是对外是挂载在京东主站上.如:www.jd.com/yifu/123456.html. 现场情况: 在本地/测试环境/预发环境中,每次 ...