CodeForces - 650D:Zip-line (LIS & DP)
Vasya has decided to build a zip-line on trees of a nearby forest. He wants the line to be as long as possible but he doesn't remember exactly the heights of all trees in the forest. He is sure that he remembers correct heights of all trees except, possibly, one of them.
It is known that the forest consists of n trees staying in a row numbered from left to right with integers from 1 to n. According to Vasya, the height of the i-th tree is equal to hi. The zip-line of length k should hang over k (1 ≤ k ≤ n) trees i1, i2, ..., ik (i1 < i2 < ... < ik) such that their heights form an increasing sequence, that is hi1 < hi2 < ... < hik.
Petya had been in this forest together with Vasya, and he now has q assumptions about the mistake in Vasya's sequence h. His i-th assumption consists of two integers ai and bi indicating that, according to Petya, the height of the tree numbered ai is actually equal to bi. Note that Petya's assumptions are independent from each other.
Your task is to find the maximum length of a zip-line that can be built over the trees under each of the q assumptions.
In this problem the length of a zip line is considered equal to the number of trees that form this zip-line.
Input
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 400 000) — the number of the trees in the forest and the number of Petya's assumptions, respectively.
The following line contains n integers hi (1 ≤ hi ≤ 109) — the heights of trees according to Vasya.
Each of the following m lines contains two integers ai and bi (1 ≤ ai ≤ n, 1 ≤ bi ≤ 109).
Output
For each of the Petya's assumptions output one integer, indicating the maximum length of a zip-line that can be built under this assumption.
Examples
4 4
1 2 3 4
1 1
1 4
4 3
4 5
4
3
3
4
4 2
1 3 2 6
3 5
2 4
4
3
题意:给定一个数组,Q次询问,询问之间彼此独立。每次询问改动一个数,问改动后的LIS。
思路:同今年南京(湘潭)的邀请赛J题,当时思路差不多对了,但是想复杂了,我和陈队花了一个半小时还是写WA了。注意到新LIS和原来的LIS最多相差1。也就是max-1,max,max+1三种情况,那么我们分两步去验证即可:
第一步,考虑没了原来的值:假设原来的LIS一定经过更改点,那么先把ans赋值为max-1;否则赋值ans为max。
第二步,考虑有了新来的值:即用到新的值,那么预处理的时候从前向后后从后向前扫,两遍预处理完,ans=max(ans,f[i]+g[i]-1)。
第二步用线段树或者树状数组即可搞定,常规操作。
第一步,其实也不难想,当时就是这里想复杂了。我们得到LISmax,然后f[i]表示以i为结束的LIS,g[i]表示以i为起点的LIS。如果f[i]+g[i]-1==max,那么种数那么cnt[f[i]]++; 讨论替换i点时,如果cnt[f[i]]==1,则说明原来的LIS必定经过i点。
CodeForces - 650D:Zip-line (LIS & DP)的更多相关文章
- CodeForces - 1073E :Segment Sum (数位DP)
You are given two integers l l and r r (l≤r l≤r ). Your task is to calculate the sum of numbers from ...
- Codeforces Gym101201B:Buggy Robot(BFS + DP)
题目链接 题意 给出一个n*m的地图,还有一个操作序列,你原本是要按照序列执行操作的,但是你可以修改操作:删除某些操作或者增加某些操作,问从'R'到'E'最少需要多少次修改操作. 思路 和上次比赛做的 ...
- SPOJ:House Fence(分治&DP)
"Holiday is coming, holiday is coming, hurray hurray!" shouts Joke in the last day of his ...
- Codeforces Gym100502H:Clock Pictures(KMP算法)
http://codeforces.com/gym/100502/attachments 题意:有两个时钟上面有n个指针,给出的数字代表指针的角度.问能否在某一时刻使得两个时钟的指针重合. 思路:容易 ...
- Codeforces 777E:Hanoi Factory(贪心+栈)
http://codeforces.com/problemset/problem/777/E 题意:给出n个环状圆柱,每个圆环有一个内半径a,外半径b,和高度h,只有外半径bj <= bi并且b ...
- Codeforces 758C:Unfair Poll(思维+模拟)
http://codeforces.com/problemset/problem/758/C 题意:教室里有n列m排,老师上课点名从第一列第一排开始往后点,直到点到第一列第m排,就从第二列第一排开始点 ...
- codeforces 486 D. Valid Sets(树形dp)
题目链接:http://codeforces.com/contest/486/problem/D 题意:给出n个点,还有n-1条边的信息,问这些点共能构成几棵满足要求的树,构成树的条件是. 1)首先这 ...
- POJ 2533 Longest Ordered Subsequence (LIS DP)
最长公共自序列LIS 三种模板,但是邝斌写的好像这题过不了 N*N #include <iostream> #include <cstdio> #include <cst ...
- BZOJ 1026:windy数(数位DP)
http://www.lydsy.com/JudgeOnline/problem.php?id=1026 1026: [SCOI2009]windy数 Time Limit: 1 Sec Memor ...
随机推荐
- selenium网页没加载完成就停止加载并自动刷新
判断一个网页10秒没加载完成就停止加载并自动刷新 driver=webdriver.Chome() driver.set_page_load_timeout(10) while True: try: ...
- 【转】Linux查看物理CPU个数、核数、逻辑CPU个数
# 总核数 = 物理CPU个数 X 每颗物理CPU的核数 # 总逻辑CPU数 = 物理CPU个数 X 每颗物理CPU的核数 X 超线程数 # 查看物理CPU个数cat /proc/cpuinfo| g ...
- iOS self 和 super 学习
有人问我 这个问题 回答错了,题干大概是说 [self class] 和 [super class]打印结果 是不是一样的. 我睁着眼睛说是不一样的 .因为我明明记得 几天前 做 DFS 获取反射基类 ...
- json教程系列(4)-optXXX方法的使用
在JSONObject获取value有多种方法,如果key不存在的话,这些方法无一例外的都会抛出异常.如果在线环境抛出异常,就会使出现error页面,影响用户体验,针对这种情况最好是使用optXXX方 ...
- XML文件结构和基本语法
XML文件的结构性内容,包括节点关系以及属性内容等等.元素是组成XML的最基本的单位,它由开始标记,属性和结束标记组成.就是一个元素的例子,每个元素必须有一个元素名,元素可以若干个属性以及属性值. x ...
- WebSocket和HTTP的区别与联系
WebSocket是一个计算机通讯协议,在单个TCP连接上提供全双工的通讯模式.WebSocket设计用于Web浏览器和Web服务器之间的通讯,但也可以用于其他的客户端和应用服务器.WebSocket ...
- 安装配置Apollo-Prota web中心平台
首先要求2g以上内存哈,JDK1.8 搭建数据库 创建表 apollo-build-scripts-master 整个阿波罗环境包 使用一个命令启动整个阿波罗服务环境 创建两个数据库 分别为:apol ...
- 解析远程域名主机的IP地址
我们知道,计算机在访问远程主机的时候,本质上是通过IP地址来进行访问的,但我们实际在使用的时候,例如我们想访问百度的主页,我们是通过在浏览器的地址栏输入百度的域名来进行访问的,因此,计算机需要将百度的 ...
- linux系统内SAMBA共享问题
最近将项目迁移到了公司服务器上,以后客户端调试和服务端开发都要去链接这台服务器,但是开发就需要调试,也需要log信息,同一局域网内,如何链接服务器并随时查看服务器上的log信息呢? 今天搞了一下,把步 ...
- JavaScript -- 广告随鼠标移动, 点击一次后关闭
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...