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 ...
随机推荐
- OAuth2.0 自我领悟
grant_type 授权模式 authorization_code 标准的Server授权模式,授权码模式 password 基于用户密码的授权模式,用户密码模式 client_credential ...
- 刷完欧拉计划中难度系数为5%的所有63道题,我学会了Rust中的哪些知识点?
我为什么学Rust? 2019年6月18日,Facebook发布了数字货币Libra的技术白皮书,我也第一时间体验了一下它的智能合约编程语言MOVE,发现这个MOVE是用Rust编写的,看来想准确理解 ...
- nginx 安装ab小工具方法
nginx 安装ab小工具方法测试工具安装(以centos系统为例)yum -y install httpd-tools 然后测试下ab -V
- golang学习笔记---string && strconv
1.字符串的组成?Golang的字符串都是由单个字节连接起来的,每个字节都是UTF8编码标识的Unicode文本.(不需要在考虑中文不兼容问题) 2.如何遍历字符串?先看一个例子: package m ...
- 基于贝叶斯网(Bayes Netword)图模型的应用实践初探
1. 贝叶斯网理论部分 笔者在另一篇文章中对贝叶斯网的理论部分进行了总结,在本文中,我们重点关注其在具体场景里的应用. 2. 从概率预测问题说起 0x1:条件概率预测模型之困 我们知道,朴素贝叶斯分类 ...
- FileChannel(API详解)
1.两种获取通道的方法FileChannel.open()的方式 FileChannel channell = FileChannel.open(Paths.get("a.txt" ...
- VUE面刷新
1.这种方法页面会一瞬间的白屏 ) 2.这种也是一样,画面一闪 location.reload() 3.搭配provide.inject使用 首先在主页面 app.vue 设置: <keep-a ...
- webapi 集成NLog
参考项目代码:SwaggerDemoApi 安装 打开nuget管理器--->搜索nlog,安装箭头所指的两个文件到你的项目中,config安装到你的API项目即可,nlog文件安装到你用得到n ...
- 如何设计提高服务API的安全性(一)基础介绍
场景 现今越来越多公司提供了Sass平台服务,大部分也直接提供API.如快递鸟.微信Api.云服务.如何保证这些服务的安全性是一门重要的课题.如快递跟踪.机票查询等很便捷地影响着我们d的生活,对这些技 ...
- Nginx实现防盗链的方式
一.ngx_http_referer_module(阻挡来源非法的域名请求),配置如下: location ~.*\. (gif|jpg|png|flv|swf|rar|zip)$ { valid_r ...