最长上升子序列。虽然数据可以直接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. 【05】emmet系列之各种缩写

    [01]emmet系列之基础介绍 [02]emmet系列之HTML语法 [03]emmet系列之CSS语法 [04]emmet系列之编辑器 [05]emmet系列之各种缩写 各种缩写   缩写:! & ...

  2. MTK android L使用汇顶TP如何使用B协议

    使用B协议上报之前的代码: #include "tpd.h" #include "tpd_custom_gt9xx.h" #ifndef TPD_NO_GPIO ...

  3. Eclipse配置SVN的几种方法及使用详情

    此文章对Myeclipse同样适用. 一.在Eclipse里下载Subclipse插件 方法一:从Eclipse Marketplace里面下载 具体操作:打开Eclipse --> Help ...

  4. 【BZOJ4403】序列统计(Lucas定理,组合计数)

    题意:给定三个正整数N.L和R, 统计长度在1到N之间,元素大小都在L到R之间的单调不降序列的数量. 输出答案对10^6+3取模的结果. 对于100%的数据,1≤N,L,R≤10^9,1≤T≤100, ...

  5. 【ZJOI2017 Round1练习&&BZOJ5353】D7T2 guess(费用流)

    题意: 思路: ..]of longint; pre:..,..]of longint; inq:..]of boolean; q:..]of longint; n,m,i,j,ans,tot,sou ...

  6. IntentService用于服务中开启子线程的自动关闭

    package com.pingyijinren.test; import android.app.IntentService; import android.content.Intent; impo ...

  7. 洛谷 P3609 [USACO17JAN]Hoof, Paper, Scissor蹄子剪刀…

    P3609 [USACO17JAN]Hoof, Paper, Scissor蹄子剪刀… 题目背景 欢迎提供翻译,请直接在讨论区发帖,感谢你的贡献. 题目描述 You have probably hea ...

  8. wget下载网络图片

    wget http.......  --no-check-certificate

  9. Spring集成Redis方案(spring-data-redis)(基于Jedis的单机模式)(待实践)

    说明:请注意Spring Data Redis的版本以及Spring的版本!最新版本的Spring Data Redis已经去除Jedis的依赖包,需要自行引入,这个是个坑点.并且会与一些低版本的Sp ...

  10. html5 编辑

    在html中想获得矢量图形可以用svg标签.该标签画出的图形全部用代码实现. 可以用在线html编辑工具来进行所见即所得编辑,然后到处源码. 比较好用的工具有http://editor.method. ...