洛谷 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. 在 Vue 中使用 装饰器 Decorator

    Decorator 的语法还没有通过提案,所以项目中很少用.不过最近刚好有一个需求用到了. 装饰器的语法 http://es6.ruanyifeng.com/#docs/decorator 需求是,有 ...

  2. BizTalk证书相关操作

    OPEN SSL 神技能 从PFX文件中导出私钥 openssl pkcs12 -in Cert.pfx -nocerts -nodes -out private_pc.key 从PFX文件中导出CS ...

  3. 用友U8删除采购转固卡片后,再次转固找不到采购订单,且其他订单转固报错

    问题描述 1.用户反馈删除两张之前通过采购转固定资产的卡片后,想要再次生成卡片时候,却找不到这两张卡片. 2.用户尝试对其他未转固定资产的订单进行转固,在保存卡片时提示“本次结转将放弃全部已完成的业务 ...

  4. laravel中如何执行请求

    laravel中如何执行request请求?本篇文章给大家介绍关于laravel中执行请求的方法,需要的朋友可以参考一下,希望对你有所帮助. 我们先来看一下request是什么? 客户端(例如Web浏 ...

  5. 明解C语言 入门篇 第九章答案

    练习9-1 /* 将字符串存储在数组中并显示(其2:初始化) */ #include <stdio.h> int main(void) { char str[] = "ABC\0 ...

  6. 使用SonarQube和SonarQube Scanner分析项目

    一.概述 SonarQube的安装,请参考链接:https://www.cnblogs.com/xiao987334176/p/12011623.html 配置好sonar的服务端后,接下来就要使用s ...

  7. Redis(六)Lua脚本的支持

    Redis为什么需要Lua脚本的支持 当应用需要Redis完成一些Redis命令不支持的特性时,要么扩展Redis client或者更甚至编写c扩展Redis server.这都大大造成了应用的实现的 ...

  8. git账户配置

    一.生成github的ssh key ssh-keygen ssh-keygen -t rsa -f ~/.ssh/zzf073_rsa -C zzf073@163.com 二.配置账户公钥 1.查看 ...

  9. 《 .NET并发编程实战》阅读指南 - 第2章

    先发表生成URL以印在书里面.等书籍正式出版销售后会公开内容.

  10. 并发编程需要场景化:CountDownLatch