【以前的空间】Poj 3071 Cut the Sequence
dp+单调性+平衡树
在看某篇论文中看到这道题,但是那篇论文不如这个http://www.cnblogs.com/staginner/archive/2012/04/02/2429850.html 大神的空间写的好(还是说我太弱需要详解……)。
其实要说的在大神的博客里面已经说的很好……
比如f[i],然后j表示满足a[j+1]+a[j+2]+……+a[i]<=m的最小值。然后我们假定a[j]--a[i]中最大数的下标为k,那么就有j+1<=l<=k时,f[j+1]+a[k]<=f[j+2]+a[k]<=f[j+3]+a[k]……<=f[k-1]+a[k],也就是只要在把在k前或者k到i的区间分为一个区间,那么这个区间的最大值就是a[k]。这样f数组就是递增的。
var
left,right,s,q,d:array[..]of longint;
key,a,sum,f:array[..]of int64;
i,j,k,l,n,tot,t,head,tail,deci:longint;
m:int64; procedure rightrotate(var t:longint);
var
k:longint;
begin
k:=left[t];
left[t]:=right[k];
right[k]:=t;
s[k]:=s[t];
s[t]:=s[left[t]]+s[right[t]]+;
t:=k;
end; procedure leftrotate(Var t:longint);
var
k:longint;
begin
k:=right[t];
right[t]:=left[k];
left[k]:=t;
s[k]:=s[t];
s[t]:=s[left[t]]+s[right[t]]+;
t:=k;
end; procedure maintain(var t:longint);
begin
if s[left[left[t]]]>s[right[t]] then begin
rightrotate(t);
maintain(right[t]);
maintain(t);
end;
if s[right[left[t]]]>s[right[t]] then begin
leftrotate(left[t]);
rightrotate(t);
maintain(left[t]);
maintain(right[t]);
maintain(t);
end;
if s[right[right[t]]]>s[left[t]] then begin
leftrotate(t);
maintain(left[t]);
maintain(t);
end;
if s[left[right[t]]]>s[left[t]] then begin
rightrotate(right[t]);
leftrotate(t);
maintain(left[t]);
maintain(right[t]);
maintain(t);
end;
end; procedure insert(var t:longint;v:int64);
begin
if t= then begin
inc(tot);
t:=tot;
key[t]:=v;
s[t]:=;
left[t]:=;
right[t]:=;
end
else begin
inc(s[t]);
if v<key[t] then insert(left[t],v)
else insert(right[t],v);
maintain(t);
end;
end; function delete(var t:longint;v:int64):int64;
begin
dec(s[t]);
if (key[t]=v) or( (v<key[t]) and (left[t]=) )or ((v>=key[t]) and (right[t]=)) then begin
delete:=key[t];
if (left[t]=) or (right[t]=) then
t:=left[t]+right[t]
else key[t]:=delete(left[t],key[t]+);
end
else
if v<key[t] then
delete:=delete(left[t],v)
else delete:=delete(right[t],v);
end; function searchmin(var t:longint):int64;
begin
if left[t]= then exit(key[t]);
exit(searchmin(left[t]));
end; function into:boolean;
begin
readln(n,m);
sum[]:=;
for i:= to n do begin
read(a[i]);
sum[i]:=sum[i-]+a[i];
if a[i]>m then exit(false);
end;
exit(true);
end; begin
if into then begin
t:=;
tot:=;
head:=;
tail:=;
deci:=;
for i:= to n do begin
while sum[i]-sum[deci]>m do inc(deci);
while (head<tail) and (q[head]<=deci) do begin
delete(t,f[d[head]]+a[q[head]]);
inc(head);
end;
while (head<tail) and (a[i]>=a[q[tail-]]) do begin
delete(t,f[d[tail-]]+a[q[tail-]]);
dec(tail);
end;
q[tail]:=i;
if head<tail then
d[tail]:=q[tail-]
else d[tail]:=deci;
insert(t,f[d[tail]]+a[i]);
inc(tail);
if d[head]<deci then begin
delete(t,f[d[head]]+a[q[head]]);
d[head]:=deci;
insert(t,f[deci]+a[q[head]]);
end;
f[i]:=searchmin(t);
end;
writeln(f[n]);
end
else writeln('-1');
readln;
readln;
end.
【以前的空间】Poj 3071 Cut the Sequence的更多相关文章
- poj 3017 Cut the Sequence(单调队列优化DP)
Cut the Sequence \(solution:\) 这道题出的真的很好,奈何数据水啊! 这道题当时看得一脸懵逼,说二分也不像二分,说贪心也不像贪心,说搜索吧这题数据范围怎么这么大?而且这题看 ...
- POJ 3017 Cut the Sequence
[题目链接] $O(n^2)$ 效率的 dp 递推式:${ dp }_{ i }=min\left( dp_{ j }+\overset { i }{ \underset { x=j+1 }{ max ...
- poj 3017 Cut the Sequence(单调队列优化 )
题目链接:http://poj.org/problem?id=3017 题意:给你一个长度为n的数列,要求把这个数列划分为任意块,每块的元素和小于m,使得所有块的最大值的和最小 分析:这题很快就能想到 ...
- POJ 3017 Cut the Sequence (单调队列优化DP)
题意: 给定含有n个元素的数列a,要求将其划分为若干个连续子序列,使得每个序列的元素之和小于等于m,问最小化所有序列中的最大元素之和为多少?(n<=105.例:n=8, m=17,8个数分别为2 ...
- 刷题总结——Cut the Sequence(POJ 3017 dp+单调队列+set)
题目: Description Given an integer sequence { an } of length N, you are to cut the sequence into sever ...
- [poj3017] Cut the Sequence (DP + 单调队列优化 + 平衡树优化)
DP + 单调队列优化 + 平衡树 好题 Description Given an integer sequence { an } of length N, you are to cut the se ...
- POJ3017 Cut the Sequence
题意 Language:Default Cut the Sequence Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 122 ...
- 【题解】Cut the Sequence(贪心区间覆盖)
[题解]Cut the Sequence(贪心区间覆盖) POJ - 3017 题意: 给定一大堆线段,问用这些线段覆盖一个连续区间1-x的最小使用线段的数量. 题解 考虑一个这样的贪心: 先按照左端 ...
- 【POJ 3071】 Football(DP)
[POJ 3071] Football(DP) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4350 Accepted ...
随机推荐
- rm 删除不掉文件,报错解决 以及 chattr的介绍
rm 强制删除一个文件 居然删除不掉! 出现这个错误: rm: cannot remove ‘default/.user.ini’: Operation not permitted 原来呀: 然后呢 ...
- MyBatis-自定义结果映射规则
1.自定义结果集映射规则 ①查询 <!-- public Employee getEmpById(Integer id); --> <select id="getEmpBy ...
- Windows Server 2008 R2 安装域
在Windows Server 2008 R2里面安装域. 1.首先在"服务"里面添加"角色": 2.选择对应的域角色 3.安装完成后要启动配置向导 4.选择新 ...
- 「日常训练」Balancing Act(POJ-1655)
题意与分析 树的重心板子题. 值得考虑的是,重心究竟有哪些优秀的性质? 这里是一些网上能看到的性质: (判定性质)找到一个点,其所有的子树中最大的子树节点数最少(子树可以"倒着看" ...
- Linux命令应用大词典-第30章 审计系统
30.1 auditctl:控制内核的审计系统 30.2 aureport:生成审计信息报表 30.3 ausearch:搜索审计记录 30.4 autrace:跟踪指定进程 30.5 audit-v ...
- 获取ip地址以及获取城市等信息
class Program { static void Main(string[] args) { string ip = GetIP(); if (ip != null) { string city ...
- 了解Python控制流语句——continue 语句
continue 语句用以告诉 Python 跳过当前循环块中的剩余语句,并继续该循环的下一次迭代. 案例(保存为 continue.py): while True: s = input('Enter ...
- 线性代数之——A 的 LU 分解
1. A = LU 之前在消元的过程中,我们看到可以将矩阵 \(A\) 变成一个上三角矩阵 \(U\),\(U\) 的对角线上就是主元.下面我们将这个过程反过来,通一个下三角矩阵 \(L\) 我们可以 ...
- HADOOP docker(八):hadoop本地库
前言2. Native Hadoop Library3. 使用本地库4. 本地库组件5. 支持的平台6. 下载7. 编译8. 运行时观察9. 检查本地库10. 如果共享本地库 小伙伴还记得每次启动hd ...
- [leetcode-779-K-th Symbol in Grammar]
On the first row, we write a 0. Now in every subsequent row, we look at the previous row and replace ...