洛谷 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. Elasticsearch由浅入深(十)搜索引擎:相关度评分 TF&IDF算法、doc value正排索引、解密query、fetch phrase原理、Bouncing Results问题、基于scoll技术滚动搜索大量数据

    相关度评分 TF&IDF算法 Elasticsearch的相关度评分(relevance score)算法采用的是term frequency/inverse document frequen ...

  2. dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Gold;第一次无效

    private void dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) {}//修改DataGrid ...

  3. Android 支持库迁移到AndroidX

    一.背景 Android系统版本在不断更新,从最初的Android 1.0到现在Google和各大手机厂商正在推的Android 10,平均下来每个年头都有一个大的版本更新.但用户正在用的手机上的An ...

  4. @Bean修饰的方法参数的注入方式

    @Bean修饰的方法参数的注入方式: 方法参数默认注入方式为Autowired,即先根据类型匹配,若有多个在根据名称进行匹配. 1:复杂类型可以通过@Qualifier(value=“XXX”)限定; ...

  5. 新的部署架构之下,如何拿shell?

    和朋友聊起一个话题,服务器部署架构升级对安全的影响.从最简单的一台服务器,到应用.数据库.文件服务器分离:从本地机房服务器到云服务器产品矩阵:从虚拟化到容器化部署,一直在往更安全的方向改变. 本文试图 ...

  6. C# 调用打印机打印文件

    C# 调用打印机打印文件,通常情况下,例如Word.Excel.PDF等可以使用一些对应的组件进行打印,另一个通用的方式是直接启用一个打印的进程进行打印.示例代码如下: using System.Di ...

  7. 安全漏洞系列(二)---站点信息侦测(C# MVC)

    参考地址:https://jingyan.baidu.com/article/77b8dc7fa657c26174eab631.html 概述:站点信息侦测漏洞会检测到用的版本信息等,然后借此进行一些 ...

  8. vue 强制刷新 demo 神器

    this.$forceUpdate() /*关键句,强制更新dom*/

  9. join 和子查询优化

    一次在家查看数据的时候,列表展示特别慢,就查看了一下,把sql语句拿出来运行居然要4,5秒,当时就感觉有问题,语句用的join链接2个表,感觉没啥错误,为啥会这么慢,然后改用了子查询链接,发现快了许多 ...

  10. HeadFirst设计模式(一)策略者模式

    最近在看HeadFirst设计模式一书,作为一个半路出家的程序员,感觉很多东西需要学习,学习的路程中有些东西学了当时觉得理解了,但日常工作中没有使用到渐渐的自己就忘记了.--------------- ...