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 ...
随机推荐
- LeetCode:数据库技术【180-185】
LeetCode:数据库技术[180-185] 180.连续出现的数字 题目描述 编写一个 SQL 查询,查找所有至少连续出现三次的数字. +----+-----+ | Id | Num | +--- ...
- Android 工具类 SharedPreferences 封装
SharedPreferences 是 Android 数据存储方式中的一种,特别适合用来存储少量的.格式简单的数据,比如应用程序的各种配置信息,如是否打开音效,是否开启震动等等. SharedPre ...
- Loadrunder脚本篇——关联数组(参数数组)
导言 前面说过可以用关联取出服务器相关的一些动态变化的信息,前面也提过web_reg_save_param中可以设置ord=all,代表从服务器中取出的是一个数组,它试用的场景是当我访问一个发帖网站, ...
- Python学习进程(9)序列
序列是Python中最基本的数据结构. (1)序列简介: 序列中的每个元素都分配一个数字标明它的位置或索引,第一个索引是0,第二个索引是1,依此类推.序列都可以进行的操作包括索引,切片,加,乘 ...
- 自己动手编译Android源码(超详细)
http://www.jianshu.com/p/367f0886e62b 在Android Studio代码调试一文中,简单的介绍了代码调试的一些技巧.现在我们来谈谈android源码编译的一些事. ...
- P4271 [USACO18FEB]New Barns
题目 P4271 [USACO18FEB]New Barns 做法 这题很长见识啊!! 知识点:两棵树\((A,B)\)联通后,新树的径端点为\(A\)的径端点与\(B\)的径端点的两点 不断加边,那 ...
- 数字代币ICO
随着比特币.莱特币.以太币的逐步兴起,越来越多的数字代币开始衍生,虚拟货币扑朔迷离,一不小心就被人割了韭菜..... 从荷兰IPO的故事说起 400多年前,西方有一群精英海盗开了一家公司.为了顺利拓展 ...
- 理解$watch、$apply与$digest
Angular环境 浏览器里面有一个事件队列(event queue),用户触发啥事儿,或者网络请求,延时操作(例如定时器之类),都是一个event,浏览器会轮询这些事件,然后调用这些回调(这里的回调 ...
- 【转载】用Scikit-Learn构建K-近邻算法,分类MNIST数据集
原帖地址:https://www.jiqizhixin.com/articles/2018-04-03-5 K 近邻算法,简称 K-NN.在如今深度学习盛行的时代,这个经典的机器学习算法经常被轻视.本 ...
- [cqoi2012]交换棋子
2668: [cqoi2012]交换棋子 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 1334 Solved: 518[Submit][Stat ...