洛谷 P3902 递增

洛谷传送门

JDOJ 2157: Increasing

JDOJ传送门

Description

数列A1,A2,……,AN,修改最少的数字,使得数列严格单调递增。

Input

第1 行,1 个整数N

第2 行,N 个整数A1,A2,……,AN

Output

1 个整数,表示最少修改的数字

Sample Input

3 1 3 2

Sample Output

1

HINT

• 对于50% 的数据,N <= 103

• 对于100% 的数据,1 <= N <= 105, 1 <= Ai <= 109

最优解声明

8ms卡的我好苦

题解:

这是一道单调队列的题。

首先我们想到这样的一个定理:

我们先维护一个单调队列,这个队列的元素是严格单调递增的,那么,现在我们要增加一个元素的时候,先与队尾元素比较,如果比队尾元素大,OK,入队。否则的话,就把这个元素插入到它应该到的位置,使这个东西还是一个单调队列。这时就进行了修改操作,我们就需要把答案++。

于是我们敏锐的察觉到,把这个元素插入到它应该到的地方是这道题的难点。

然后我们又敏锐地察觉到,可以用二分解决。

于是有了代码1:

#include<cstdio>
#include<algorithm>
using namespace std;
int n,a[100010],d[100010];
int l,r,ans;
int find(int x)
{
int mid,left=l,right=r;
while(left<right)
{
mid=(left+right)>>1;
if(x==d[mid])
return mid;
if(x>d[mid])
left=mid+1;
else
right=mid;
}
return right;
}
int main()
{
l=1;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=n;i++)
{
if(a[i]>d[r])
d[++r]=a[i];
else
d[find(a[i])]=a[i];
}
printf("%d",n-r);
return 0;
}

但是这个方法很麻烦。麻烦就在于必须全部读入之后再处理,需要跑两边循环,无数遍二分。所以我们想到了在讲二分地时候学习的lower_bound和upper_bound函数,它们是我们实现二分的有利帮手。

代码2:

#include<cstdio>
#include<algorithm>
using namespace std;
int n,q[100001],now,ans;
int main()
{
scanf("%d",&n); for(int i=1;i<=n;i++)
{
int num;
scanf("%d",&num);
if(num>q[now])
q[++now]=num;
else
{
*lower_bound(q+1,q+now+1,num)=num;
ans++;
}
}
printf("%d",ans);
return 0;
}

JDOJ 2157 Increasing的更多相关文章

  1. [LeetCode] Increasing Triplet Subsequence 递增的三元子序列

    Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...

  2. [LeetCode] Longest Increasing Path in a Matrix 矩阵中的最长递增路径

    Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...

  3. [LeetCode] Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  4. git error: unable to rewind rpc post data - try increasing http.postBuffer

    error: unable to rewind rpc post data - try increasing http.postBuffererror: RPC failed; curl 56 Rec ...

  5. 【LeetCode】Increasing Triplet Subsequence(334)

    1. Description Given an unsorted array return whether an increasing subsequence of length 3 exists o ...

  6. [tem]Longest Increasing Subsequence(LIS)

    Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&& ...

  7. [LintCode] Longest Increasing Subsequence 最长递增子序列

    Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...

  8. LintCode-Longest Increasing Subsequence

    Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...

  9. Longest Increasing Path in a Matrix -- LeetCode 329

    Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...

随机推荐

  1. qt no doubments matching "ui..h" could be found

    问题情境描述: 自己单独添加的UI文件,然后添加一个类来使用这个UI文件,第一次输入UI Form名称时是大写,被添加到工程里面就是大写, 大写的情况下,添加action转到槽就会提示这个错误. 修改 ...

  2. 分布式事物解决方案-TCC

    分布式框架下,如何保证事物一致性一直是一个热门话题.当然事物一致性解决方案有很多种(请参考:分布式事物一致性设计思路),我们今天主要介绍TCC方案解决的思路.以下是参与设计讨论的一种解决思路,大家有问 ...

  3. 【2019年07月22日】A股最便宜的股票

    查看更多A股最便宜的股票:androidinvest.com/CNValueTop/ 便宜指数 = PE + PB + 股息 + ROE,四因子等权,数值越大代表越低估. 本策略只是根据最新的数据来选 ...

  4. Knative Serving 进阶: Knative Serving SDK 开发实践

    作者 | 阿里云智能事业群技术专家 牛秋霖(冬岛) 导读:通过前面的一系列文章你已经知道如何基于 kubectl 来操作 Knative 的各种资源.但是如果想要在项目中集成 Knative 仅仅使用 ...

  5. Java中调用存储过程

    dao层: import java.util.Map; public interface AppGthdDao { public String gthd(Map map); } mapper层 < ...

  6. vins_fusion学习笔记

    Vins-Fusion源码:https://github.com/HKUST-Aerial-Robotics/VINS-Fusion 摘要 应项目需要,侧重学习stereo+gps融合 转载几篇写的比 ...

  7. Zookeeper在linux上的安装

    1:进入 cd  /usr/local目录下 2:创建zookeeper目录  midir zookeeper 3:将压缩包复制到zookeeper目录下  cp /root/zookeeper/zo ...

  8. FormData的Ajax提交注意事项

    Ajax提交表单一般有下面两种形式: var form_data = $('#form').serialize(); var form_data = new FormData($('#form')[0 ...

  9. 解决plsql中文显示问号(???)问题

    最近新买的电脑,配置好数据库连接后,plsql查看数据与插入中文数据都显示问号(???),同事的都正常显示,查看了很多资料,有的说是数据库字符集的原因让修改数据库的字符集,但是我的数据库都是远程连接正 ...

  10. python \r与\b的应用、光标的含义

    参考链接:https://www.jianshu.com/p/eb5c23cd6e34 \r 能将光标定位到当前行的行首 \b则是将光标回退一位 光标的含义: 光标后面的输出内容均会消失,光标回退后, ...