最长递增子序列问题[网络流24题之6]
来源:互联网 发布:休闲鞋女淘宝 编辑:程序博客网 时间:2024/05/22 06:57
问题描述:
给定正整数序列
编程任务:
设计有效算法完成
数据输入:
第
结果输出:
输出共
第
第
第
输入文件示例:
4
3 6 2 5
输出文件示例:
2
2
3
分析:
第
第
1 :新增源S 和汇T
2 :若cntx=1 ,则在S 和x 中连一条容量为1 的有向边
3 :若cnty=ans(第一问) ,则在x 与T 之间连一条容量为1 的有向边
4 :若ax<=ay且cntx=cnty+1 ,则在x 与y 之间连一条容量为1 的有向边
第
代码:
#include <cstdio>#include <algorithm>#include <cstring>using namespace std;const int inf = 0x3f3f3f3f;int cnt[547],ans;int cur[547];int head[547],nxt[4747],to[4747],wei[4747],tot=1;int c[547];int que[547];int n;int a[547];int read();void add(int,int,int);bool bfs();bool dinic(int);int get_cnt();int main(){ n = read(); for(int i=1;i<=n;++i) a[i] = read(); ans = get_cnt(); printf("%d\n",ans); for(int i=n;i>=1;--i){ if(cnt[i] == 1) add(i,546,1); if(cnt[i] == ans) add(545,i,1); for(int j=i+1;j<=n;++j) if(cnt[i]==cnt[j]+1 && a[j]>=a[i]) add(i,j,1); } int num=0; while(bfs()){ memcpy(cur,head,sizeof head); while(dinic(545)) ++num; } printf("%d\n",num); for(int i=2;i<tot;i+=2){ wei[i] = 1; wei[i^1] = 0; } for(int i=head[545];i;i=nxt[i]) if(to[i] == 1){ wei[i] = inf; break; } for(int i=head[n];i;i=nxt[i]) if(to[i] == 546){ wei[i] = inf; break; } num = 0; while(bfs()){ memcpy(cur,head,sizeof head); while(dinic(545)) num++; } printf("%d",num); return 0;}int read(){ int in = 0; char ch = getchar(); while(ch>'9'||ch<'0') ch = getchar(); while(ch<='9'&&ch>='0'){ in = in*10+ch-'0'; ch = getchar(); } return in;}void add(int from,int tp,int value){ ++tot;nxt[tot]=head[from];head[from]=tot;to[tot]=tp;wei[tot]=value; ++tot;nxt[tot]=head[tp];head[tp]=tot;to[tot]=from;wei[tot]=0;}int get_cnt(){ int m = 1; for(int i=n;i>=1;--i){ cnt[i] = 1; for(int j=i+1;j<=n;++j) if(a[i]<=a[j] && cnt[j]>=cnt[i]) cnt[i] = cnt[j]+1; } for(int i=1;i<=n;++i) if(cnt[i] > m) m = cnt[i]; return m;}bool bfs(){ int now,H=0,T=1; memset(c,0,sizeof c); c[545] = 1; que[1] = 545; do{ now = que[++H]; for(int i=head[now];i;i=nxt[i]) if(!c[to[i]] && wei[i]){ c[to[i]] = c[now]+1; que[++T] = to[i]; } }while(H<T); return c[546];}bool dinic(int place){ if(place == 546) return true; for(int i=head[place];i;i=nxt[i]) if(c[to[i]]==c[place]+1 && wei[i]) if(dinic(to[i])){ --wei[i]; ++wei[i^1]; return true; } return false;}
- 网络流24题之最长递增子序列问题
- [网络流24题 #6]最长递增子序列问题
- 最长递增子序列问题[网络流24题之6]
- 【网络流24题】最长递增子序列问题
- 【codevs1906】[网络流24题]最长递增子序列问题
- 网络流24题6. 最长递增子序列问题
- 网络流24题:最长递增子序列问题
- 网络流24题之六 最长递增子序列
- 网络流24题6 最长递增子序列
- 网络流 6最长递增子序列问题
- COGS 731. [网络流24题] 最长递增子序列
- 「网络流 24 题」最长递增子序列
- 「网络流 24 题」最长递增子序列
- [网络流24题] 最长递增子序列问题 最大流/
- 线性规划与网络流24题 06最长递增子序列问题
- 网络流与线性规划24题06最长递增子序列问题
- kyeremal-网络流24题T6-最长递增子序列问题
- nefu487最长递增子序列问题【网络流24题】超详细讲解+模板
- jquery下拉选择生日
- java容器源码解析
- 一般处理程序(后缀为.ashx)里面使用Session,直接使用Session会报实例错误
- Socket编程(二)---TCP简单实例
- 电视行业竞争日趋白热化,差异化或成竞争焦点
- 最长递增子序列问题[网络流24题之6]
- js判断密码强度
- SwitchButton 开关按钮 的多种实现方式 (附源码DEMO)
- Omnibus Gitlab + Runner 配置
- 手把手教你搭建和配置Jenkins环境
- Unity 基础 - 预设
- apk文件反编译
- Leetcode no. 165
- iOS开发-OC中TabView的编辑