洛谷 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. nginx location笔记

    nginx location笔记= 开头表示精确匹配^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可.nginx不对url做编码,因此请求为/static/20%/aa,可以被规则 ...

  2. ThinkPHP连接Oracle数据库的详细教程

    一. 操作环境搭建 系统:Windows7 旗舰版 64位PHP环境:wampserver2.2e-php5.4.3-httpd2.2.22-mysql5.5.24 32位版ThinkPHP:3.0正 ...

  3. Knative 基本功能深入剖析:Knative Eventing 之 Sequence 介绍

    作者 | 元毅,阿里云容器平台高级开发工程师,负责阿里云容器平台 Knative 相关工作. 导读:在实际的开发中我们经常会遇到将一条数据需要经过多次处理的场景,称为 Pipeline.那么在 Kna ...

  4. web应急:新闻源网站劫持

    新闻源网站一般权重较高,收录快,能够被搜索引擎优先收录,是黑灰产推广引流的必争之地,很容易成为被攻击的对象.被黑以后主要挂的不良信息内容主要是博彩六合彩等赌博类内容,新闻源网站程序无论是自主开发的还是 ...

  5. [cf 1264 C] Beautiful Mirrors with queries

    题意: 你有$n$个魔镜,第$i$个魔镜有$p_{i}$的概率说你美. 从第1天开始,你会依次询问魔镜$1-n$你美不美. 若第$i$个魔镜说你美则你明天会继续询问第$i+1$个魔镜. 否则你明天会从 ...

  6. .Net 获取当前周是第几周

    最近项目中需要获取当前周是今年的第几周,这东西听起来不难,但是还挺有意思的. 在中国,一周是从周一开始算,周天结束,在国外就不是这样了,是从周天到周六为一个周. 有很多种方式去实现在这个功能,下面介绍 ...

  7. Redis配置过程中的问题

    记录一下配置过程中的坑~~ 当Redis在服务器上安装完成后,get.set没有问题了,接下来在程序中使用看看... 首先 在配置文件redis.conf中,默认的bind 接口是127.0.0.1, ...

  8. new/delete与malloc/free的区别

    new/delete与malloc/free的区别 参考: https://blog.csdn.net/u013485792/article/details/51329541 https://www. ...

  9. .net core 读取appsettings.json乱码

    .net core 读取配置文件乱码:vs2019读取appsettings.json乱码问题; .net core 读取appsettings.json乱码问题;用notepad++或者其他编辑器打 ...

  10. mac上使用Sequel Pro工具SSH连接数据库

    今天在使用Mac上的Sequel Pro连接线上的数据库时,一直报ssh通道连接失败.但是同样的公钥在另一台机器就可以,真是奇怪. 通过查找日志发现有一个关键字"key_load_publi ...