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 ...
随机推荐
- 在英文Windows操作系统上使用SQL Server Management Studio(SSMS)导入Excel 97-2003文件时报错:Failure creating file
今天在公司服务器上使用SQL Server Management Studio(SSMS)导入Excel 97-2003文件(.xls)时报错: Failure creating file. (Mic ...
- 使用ajax进行汽车详情表的查询
主界面代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...
- php token 生成
php token的生成 接口特点汇总: 1.因为是非开放性的,所以所有的接口都是封闭的,只对公司内部的产品有效: 2.因为是非开放性的,所以OAuth那套协议是行不通的,因为没有中间用户的授权过 ...
- debian内核代码执行流程(二)
继续上一篇文章<debian内核代码执行流程(一)>未完成部分. acpi_bus_init调用acpi_initialize_objects,经过一系列复杂调用后输出下面信息: [ IN ...
- SpringBoot Mybatis keyProperty和useGeneratedKeys的作用
在使用mybatis时,常常会出现这种需求: 当主键是自增的情况下,添加一条记录的同时,其主键是不能被使用的(为空),但是有时我们需要该主键,这时我们该如何处理呢?这时我们只需要在其对应xml中加入以 ...
- HTML5 JS 压缩图片,并取得图片的BASE64编码上传
基本过程 1) 调用 FileReader 的 reader.readAsDataURL(img); 方法, 在其onload事件中, 将用户选择的图片读入 Image对象. 2) 在image对象的 ...
- C++中substr的用法
C++中substr函数的用法 #include<string> #include<iostream> using namespace std; void main() { s ...
- python的pexpect详解
Pexpect 是一个用来启动子程序并对其进行自动控制的纯 Python 模块. Pexpect 可以用来和像 ssh.ftp.passwd.telnet 等命令行程序进行自动交互.继第一部分< ...
- js获取select标签选中的值[转]
var obj = document.getElementByIdx_x(”testSelect”); //定位id var index = obj.selectedIndex; // 选中索引 va ...
- scapy学习笔记(2)
一.包 包(Packet)是TCP/IP协议通信传输中的数据单位,一般也称“数据包”.其主要由“目的IP地址”.“源IP地址”.“净载数据”等部分构成,包括包头和包体,包头是固定长度,包体的长度不定, ...