JDOJ 2157 Increasing
洛谷 P3902 递增
JDOJ 2157: Increasing
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的更多相关文章
- [LeetCode] Increasing Triplet Subsequence 递增的三元子序列
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- [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 ...
- [LeetCode] Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- 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 ...
- 【LeetCode】Increasing Triplet Subsequence(334)
1. Description Given an unsorted array return whether an increasing subsequence of length 3 exists o ...
- [tem]Longest Increasing Subsequence(LIS)
Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&& ...
- [LintCode] Longest Increasing Subsequence 最长递增子序列
Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...
- LintCode-Longest Increasing Subsequence
Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...
- 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 ...
随机推荐
- ZROI1153 【线上训练3】数个数
ZROI1153 [线上训练3]数个数 传送门 一道非常有意思的题,涵盖了各种知识点. 首先,很显然,这是个容斥.容斥可以过掉\(30pts\). 这里我们考虑容斥+DP. 我们令\(dp[i][j] ...
- PLSQL安装过程和SCOTT用户被锁的解决方法
一.PLSQL安装: PLSQL基本就是一键式安装,没有什么需要修改的东西,一路Next就行了. 二.SCOTT用户被锁问题解决 1.问题如下: SCOTT用户默认是被锁的,需要通过system管理员 ...
- C#程序只允许运行一个实例的解决方案
最近在做winform的程序中,需要只能打开一个程序,如果已经存在,则激活该程序的窗口,并显示在最前端.在网上google了一哈,找到了很多的解决方案.这里我整理了3种方案,并经过了测试,现和朋友们分 ...
- mvn手动上传jar到本地仓库
mvn install:install-file -Dfile=G:\elastic-project\workspace\out\artifacts\xxl_job_core_jar\xxl-job- ...
- vins_fusion学习笔记
Vins-Fusion源码:https://github.com/HKUST-Aerial-Robotics/VINS-Fusion 摘要 应项目需要,侧重学习stereo+gps融合 转载几篇写的比 ...
- HashMap 源码分析 基于jdk1.8分析
HashMap 源码分析 基于jdk1.8分析 1:数据结构: transient Node<K,V>[] table; //这里维护了一个 Node的数组结构: 下面看看Node的数 ...
- Redis学习之zskiplist跳跃表源码分析
跳跃表的定义 跳跃表是一种有序数据结构,它通过在每个结点中维持多个指向其他结点的指针,从而达到快速访问其他结点的目的 跳跃表的结构 关于跳跃表的学习请参考:https://www.jianshu.co ...
- Sentry异常捕获平台
本文包括Sentry平台的介绍,以及环境搭建两部分,更多细节请查阅官方文档. 简介 Sentry是一个实时事件的日志聚合平台.它专门监测错误并提取所有有用信息用于分析,不再麻烦地依赖用户反馈来定位问题 ...
- CAS5单点登录
看这篇文章即可:https://www.jianshu.com/p/c1273d81c4e4>https://www.jianshu.com/p/c1273d81c4e4
- Linux 安装Mysql1.8【yum安装】
.下载mysql的yum源 wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm2.安装yum源yum lo ...