最长上升子序列。虽然数据可以直接n方但是另写了个nlogn的

转移:f[i]=max(f[j]+1)(a[j]<a[i])

O(n^2)

#include<iostream>
#include<cstdio>
using namespace std;
const int N=5005;
int n,a[N],f[N],ans;
int read()
{
int r=0,f=1;
char p=getchar();
while(p>'9'||p<'0')
{
if(p=='-')
f=-1;
p=getchar();
}
while(p>='0'&&p<='9')
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
int main()
{
n=read();
for(int i=1;i<=n;i++)
a[i]=read();
for(int i=1;i<=n;i++)
{
for(int j=0;j<i;j++)
if(a[j]<a[i]&&f[j]+1>f[i])
f[i]=f[j]+1;
ans=max(ans,f[i]);
}
printf("%d\n",ans);
return 0;
}

O(nlogn)树状数组+hash

#include<iostream>
#include<cstdio>
#include<map>
#include<algorithm>
using namespace std;
const int N=5005;
int n,a[N],f[N],ans,g[N],t[N];
map<int,int>mp;
int read()
{
int r=0,f=1;
char p=getchar();
while(p>'9'||p<'0')
{
if(p=='-')
f=-1;
p=getchar();
}
while(p>='0'&&p<='9')
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
inline int lb(int x)
{
return x&(-x);
}
void update(int x,int v)
{
for(int i=x;i<=n;i+=lb(i))
t[i]=max(t[i],v);
}
int ques(int x)
{
int re=0;
for(int i=x;i>=1;i-=lb(i))
re=max(re,t[i]);
return re;
}
int main()
{
n=read();
for(int i=1;i<=n;i++)
a[i]=g[i]=read();
sort(g+1,g+1+n);
for(int i=1;i<=n;i++)
mp[g[i]]=i;
for(int i=1;i<=n;i++)
a[i]=mp[a[i]];
for(int i=1;i<=n;i++)
{
f[i]=ques(a[i]-1)+1;
update(a[i],f[i]);
ans=max(ans,f[i]);
}
printf("%d\n",ans);
return 0;
}

bzoj 1669: [Usaco2006 Oct]Hungry Cows饥饿的奶牛【dp+树状数组+hash】的更多相关文章

  1. BZOJ 1669: [Usaco2006 Oct]Hungry Cows饥饿的奶牛( LIS )

    裸的LIS ----------------------------------------------------------------- #include<cstdio> #incl ...

  2. 【BZOJ】1669: [Usaco2006 Oct]Hungry Cows饥饿的奶牛(lis)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1669 水题太严重 #include <cstdio> #include <cstr ...

  3. BZOJ1669: [Usaco2006 Oct]Hungry Cows饥饿的奶牛

    1669: [Usaco2006 Oct]Hungry Cows饥饿的奶牛 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 665  Solved: 419 ...

  4. 【动态规划】bzoj1669 [Usaco2006 Oct]Hungry Cows饥饿的奶牛

    #include<cstdio> #include<algorithm> using namespace std; int n,a[5001],b[5001],en; int ...

  5. 「BZOJ1669」D 饥饿的牛 [Usaco2006 Oct] Hungry Cows 牛客假日团队赛5 (LIS,离散化树状数组)

    链接:https://ac.nowcoder.com/acm/contest/984/D 来源:牛客网 饥饿的牛 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言 ...

  6. BZOJ.4553.[HEOI2016&TJOI2016]序列(DP 树状数组套线段树/二维线段树(MLE) 动态开点)

    题目链接:BZOJ 洛谷 \(O(n^2)\)DP很好写,对于当前的i从之前满足条件的j中选一个最大值,\(dp[i]=d[j]+1\) for(int j=1; j<i; ++j) if(a[ ...

  7. BZOJ 4361 isn | DP 树状数组

    链接 BZOJ 4361 题面 给出一个长度为n的序列A(A1,A2...AN).如果序列A不是非降的,你必须从中删去一个数, 这一操作,直到A非降为止.求有多少种不同的操作方案,答案模10^9+7. ...

  8. bzoj:1666: [Usaco2006 Oct]Another Cow Number Game 奶牛的数字游戏

    Description 奶牛们又在玩一种无聊的数字游戏.输得很郁闷的贝茜想请你写个程序来帮她在开局时预测结果.在游戏的开始,每头牛都会得到一个数N(1<=N<=1,000,000).此时奶 ...

  9. bzoj 1666: [Usaco2006 Oct]Another Cow Number Game 奶牛的数字游戏【模拟】

    模拟 #include<iostream> #include<cstdio> using namespace std; int n,ans; int main() { scan ...

随机推荐

  1. [luoguP1272] 重建道路

    传送门 奇奇怪怪的分组背包. #include <cstdio> #include <cstring> #include <iostream> #define N ...

  2. [NOIP2005] 提高组 洛谷P1054 等价表达式

    题目描述 明明进了中学之后,学到了代数表达式.有一天,他碰到一个很麻烦的选择题.这个题目的题干中首先给出了一个代数表达式,然后列出了若干选项,每个选项也是一个代数表达式,题目的要求是判断选项中哪些代数 ...

  3. [USACO08NOV]时间管理Time Management

    题目描述 Ever the maturing businessman, Farmer John realizes that he must manage his time effectively. H ...

  4. Devu and Flowers lucas定理+容斥原理

    Devu wants to decorate his garden with flowers. He has purchased n boxes, where the i-th box contain ...

  5. AtCoder Grand Contest 012 B Splatter Painting(记忆化搜索)

    题意: 给一个包含N个顶点,M条边,无自环和重边的简单无向图,初始每个点颜色都为0,每条边的长度为1,连接着ai,bi两个节点.经过若干个操作, 每次将与某个点vi距离不超过di的所有点染成某种颜色c ...

  6. JSP的体系结构

    以下内容引用自http://wiki.jikexueyuan.com/project/jsp/architecture.html: Web服务器需要一个JSP引擎,即处理JSP页面的容器.JSP容器负 ...

  7. Tutorial: Synchronizing State with Mutexes in Go

    go语言中用mutex实现状态同步. 原文:https://kylewbanks.com/blog/tutorial-synchronizing-state-with-mutexes-golang - ...

  8. ubuntu Install Firefox

    Firefox 下载文件以.tar和.bz2格式保存,必须从这些压缩包中提取文件.不想删除当前安装的 Firefox,给每个版本的 Firefox 创建一个单独的文件夹. 例如:1.Firefox 版 ...

  9. yarn使用

    参数中有中括号和尖括号,我们要识别以下区别: [] :可选项 <>:必选项 初始化一个新的项目 yarn init 添加一个依赖包 yarn add [package] yarn add ...

  10. Android AsyncTask 分析内部实现

    sdk3.0前,使用内部的线程池,多线程并发运行.线程池大小等于5,最大达128 sdk3.0后,使用默认的serial线程池.运行完一个线程,再顺序运行下一个线程.sdk3.0<=curren ...