[CTSC2017]最长上升自序列(伪题解)(Dilworth's theorem+网络流)
部分分做法很多,但每想出来一个也就多5~10分。正解还不会,下面是各种部分分做法:
Subtask 1:k=1
LCS长度最长为1,也就是说不存在j>i和a[j]>a[i]同时成立。显然就是一个LDS,树状数组直接求即可。
Subtask 2:k=2
最多两个,也就是可以由两个LCS拼起来,f[i][j]表示第一个LCS以i结尾,第二个以j结尾的方案数,转移显然。
Subtask 3:k=2
树状数组优化DP,复杂度由$O(n^3)$降为$O(n^2 \log n)$
Subtask 4,5:B<=8
DP套DP:https://www.cnblogs.com/clnchanpin/p/7357564.html
一般与“子序列”同时出现,如最长上升自序列,最长公共自序列等。
Subtask 6,7:
一个显然的定理:一个序列的LCS最大为k意味着这个序列最少可以由k个不相交的LDS组成。
考虑网络流,下面(a,b)表示容量为a,费用为b的边。
拆点,in[x]向out[x]连(1,-1)的边,每次和下一个小于这个数的位置连(1,0)的边,增设源汇,最多增广k次即可。
$O(Kn^3)$
Subtask 8,9:
上面的方法点数为$n$,边数为$n^2$,启发我们用线段树优化建图。
这里用树状数组就行了。
点数$n\log n$,边数$n\log n$。
$O(K(n\log n)^2)$
Subtask 10,11,12:
Johnson多源最短路算法:
传统的Floyd是$O(n^3)$的,已经很优秀了。但是如果我们对每个点跑一次Dijkstra,可以得到$O(n^2\log m)$这个更好的复杂度。
但是Dijkstra不能跑有负权边的情况。
考虑增设超级源S并向每个点连长度为0的边,然后跑单源最短路,接着将每条边(u,v,w)改成(u,v,w+(dis[u]-dis[v])),其中dis[u]表示S到u的最短路。
这样跑Dijkstra就是正确的了,转移的时候记录pre方便最后求出最短路长度。
不了解如何运用到这道题上。
优化:可以直接用数组代替堆降低复杂度。
Subtask 13~20:
完全不理解的杨氏矩阵理论。
不断分析将复杂度依次降为:$O(n^2\log n+Q\log n)$,$O(n\sqrt{n}\log n)$,$O(n\sqrt{n\log n})$。
附:树状数组+网络流代码(15pts)
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#define rep(i,l,r) for (int i=(l); i<=(r); i++)
using namespace std; const int N=;
int x,y,fir[N],pre[N],a[N],b[N],inq[N],dis[N],n,m,cnt=;
int in[N],out[N],ans[N],BIT[N],S,T,cost,lim,obt,tot; struct edge{
int to,nxt,f,c;
edge () {}
edge (int x,int y,int z,int l){ to=y; nxt=fir[x]; f=z; c=l; fir[x]=cnt; }
}e[*N]; void add(int x,int y,int z,int l){ e[++cnt]=edge(x,y,z,l); e[++cnt]=edge(y,x,,-l); } bool spfa(){
int i,x; queue<int> q;
for(i=;i<=T;i++) dis[i]=;
dis[S]=; q.push(S);
while(!q.empty()){
x=q.front(); q.pop();
for(i=fir[x];i;i=e[i].nxt)
if(e[i].f&&dis[e[i].to]>dis[x]+e[i].c){
dis[e[i].to]=dis[x]+e[i].c; pre[e[i].to]=i;
if(!inq[e[i].to]) q.push(e[i].to),inq[e[i].to]=;
}
inq[x]=;
}
return dis[T]!=;
} int aug(){
int x,flow=1e9;
for(x=T;x!=S;x=e[pre[x]^].to) flow=min(flow,e[pre[x]].f);
for(x=T;x!=S;x=e[pre[x]^].to)
cost+=e[pre[x]].c*flow,e[pre[x]].f-=flow,e[pre[x]^].f+=flow;
return flow;
} int dinic(){ int res=; while(spfa()) res+=aug(); return res; } void modify(int p,int x){
for(;x<=obt;x+=x&-x){
tot++;
if(BIT[x]) add(tot,BIT[x],n,);
add(tot,p,,); BIT[x]=tot;
}
} void ask(int p,int x){ for (;x;x-=x&-x) if (BIT[x]) add(p,BIT[x],,); } int main(){
freopen("lis.in","r",stdin);
freopen("lis.out","w",stdout);
scanf("%d%d",&n,&m);
if (n>) { rep(i,,m) printf("0\n"); return ; }
rep(i,,n) scanf("%d",&a[i]),b[i]=a[i];
sort(b+,b+n+); obt=unique(b+,b+n+)-b-;
rep(i,,n) a[i]=lower_bound(b+,b+obt+,a[i])-b;
rep(i,,n) in[i]=i,out[i]=i+n,add(in[i],out[i],,-);
tot=*n;
for (int i=n;i;i--) ask(out[i],a[i]),modify(in[i],a[i]);
S=tot+; T=S+;
rep(i,,n) add(S,in[i],,),add(out[i],T,,);
cost=lim=;
while(spfa()) aug(),ans[++lim]=-cost;
while(m--) scanf("%d%d",&x,&y),printf("%d\n",ans[min(y,lim)]);
return ;
}
[CTSC2017]最长上升自序列(伪题解)(Dilworth's theorem+网络流)的更多相关文章
- 算法复习——求最长不下降序列长度(dp算法)
题目: 题目背景 161114-练习-DAY1-AHSDFZ T2 题目描述 有 N 辆列车,标记为 1,2,3,…,N.它们按照一定的次序进站,站台共有 K 个轨道,轨道遵从先进先出的原则.列车进入 ...
- JDOJ 1929: 求最长不下降序列长度
JDOJ 1929: 求最长不下降序列长度 JDOJ传送门 Description 设有一个正整数的序列:b1,b2,-,bn,对于下标i1<i2<-<im,若有bi1≤bi2≤-≤ ...
- [BZOJ1852] [MexicoOI06]最长不下降序列
[BZOJ1852] [MexicoOI06]最长不下降序列 额我也不知道是不是水过去的...和网上的另一篇题解对拍过了,但是拍不出来... 经过和神仙的讨论基本可以确定是对的了 考虑如下贪心 (我将 ...
- LeetCode 674. Longest Continuous Increasing Subsequence (最长连续递增序列)
Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...
- [LeetCode] Longest Continuous Increasing Subsequence 最长连续递增序列
Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...
- [Swift]LeetCode674. 最长连续递增序列 | Longest Continuous Increasing Subsequence
Given an unsorted array of integers, find the length of longest continuous increasing subsequence (s ...
- 问题 B: 【例9.3】求最长不下降序列(基础dp)
问题 B: [例9.3]求最长不下降序列 时间限制: 1 Sec 内存限制: 128 MB提交: 318 解决: 118[提交][状态][讨论版][命题人:quanxing] 题目描述 设有由n( ...
- leecode 978. Longest Turbulent Subarray(最长连续波动序列,DP or 滚动数组)
传送门:点我 978. Longest Turbulent Subarray A subarray A[i], A[i+1], ..., A[j] of A is said to be turbule ...
- 九度oj题目1342:寻找最长合法括号序列II
题目1342:寻找最长合法括号序列II(25分) 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:886 解决:361 题目描述: 假如给你一个由’(‘和’)’组成的一个随机的括号序列,当然 ...
随机推荐
- Asp.net MVC Combres的简单用法
第一步:添加nuget包 [1]添加 nuget包后,会自动在 webconfig里面 添加配置文件(不用改) <section name="dotless" type=&q ...
- 近期对于windows服务的理解
1.APP.config的作用 在开发环境下时,根目录下的APP.config里面会填写一些参数之类的.当生成之后,这些参数将会被自动生成在*.exe文件目录中.如图: 其中,.exe文件为Win ...
- java实现极简的LRU算法
import java.util.LinkedHashMap;import java.util.Map; /** * LRU (Least Recently Used) */public class ...
- 【BZOJ3237】【AHOI2013】连通图 [CDQ分治]
连通图 Time Limit: 20 Sec Memory Limit: 512 MB[Submit][Status][Discuss] Description Input Output Sampl ...
- [POJ1637]混合图的欧拉回路判定|网络流
混合图的欧拉回路判定 上一篇正好分别讲了有向图和无向图的欧拉回路判定方法 如果遇上了混合图要怎么做呢? 首先我们思考有向图的判定方法:所有点的出度=入度 我们可以先为无向边任意定一个向,算出此时所有顶 ...
- 【BZOJ】1827: [Usaco2010 Mar]gather 奶牛大集会
[算法]树型DP||树的重心(贪心) [题解] 两遍DFS,第一次得到所有节点子树的路径和,第二次给出除了该子树外其它部分的路径和,时时计算答案. long long!!! #include<c ...
- Android 全面插件化 RePlugin 流程与源码解析
转自 Android 全面插件化 RePlugin 流程与源码解析 RePlugin,360开源的全面插件化框架,按照官网说的,其目的是“尽可能多的让模块变成插件”,并在很稳定的前提下,尽可能像开发普 ...
- 基于SSM框架web搜索功能的实现
这里适合选用于jsp搭建的网站,数据库采用MySQL 一.HTML <div class="header_search"> <input type="t ...
- equestAnimationFrame
export const requestAnimationFrame = (() => { /* istanbul ignore next */ if (!inBrowser) { return ...
- ubuntu16.04搭建个人简易DLP
前言 最近一朋友让我帮忙搭建一台服务器,用做公司的服务器,但是该服务器需要满足一些安全要求,于是乎就有了下面的解决过程^_^ 需求 期望普通用户和管理员都能ssh登陆服务器,但禁止scp或者其他方式下 ...