4553: [Tjoi2016&Heoi2016]序列

来源:互联网 发布:电脑离线电子地图软件 编辑:程序博客网 时间:2024/06/13 02:45

4553: [Tjoi2016&Heoi2016]序列

Time Limit: 20 Sec  Memory Limit: 128 MB
Submit: 428  Solved: 207
[Submit][Status][Discuss]

Description

 佳媛姐姐过生日的时候,她的小伙伴从某宝上买了一个有趣的玩具送给他。玩具上有一个数列,数列中某些项的值

可能会变化,但同一个时刻最多只有一个值发生变化。现在佳媛姐姐已经研究出了所有变化的可能性,她想请教你
,能否选出一个子序列,使得在任意一种变化中,这个子序列都是不降的?请你告诉她这个子序列的最长长度即可
。注意:每种变化最多只有一个值发生变化。在样例输入1中,所有的变化是:
1 2 3
2 2 3
1 3 3
1 1 31 2 4
选择子序列为原序列,即在任意一种变化中均为不降子序列在样例输入2中,所有的变化是:3 3 33 2 3选择子序列
为第一个元素和第三个元素,或者第二个元素和第三个元素,均可满足要求

Input

 输入的第一行有两个正整数n, m,分别表示序列的长度和变化的个数。接下来一行有n个数,表示这个数列原始的

状态。接下来m行,每行有2个数x, y,表示数列的第x项可以变化成y这个值。1 <= x <= n。所有数字均为正整数
,且小于等于100,000

Output

 一个整数

Sample Input

3 4
1 2 3
1 2
2 3
2 1
3 4

Sample Output

3

HINT

Source

[Submit][Status][Discuss]

先考虑dp解决
令fi:以i结尾的最长不下降子序列长度
fi = max{fj + 1} (j∈[1,i))
令每个位置的初始值为ai,修改后最小值bi最大值ci
考虑题中的限制,转移合法,当aj <= ai && cj <= ai && aj <= bi
转换为max(aj,cj) <= ai && aj <= bi
把转移使用的状态看做二维平面上的点(max(ai,ci),ai),把询问看做二维平面上的点(ai,bi)
这就是询问一个从原点起至(ai,bi)的矩形中的最小值,二维线段树维护==
虽然复杂度都是O(nlog^2n)
不过可以先CDQ分治,,然后离散化+排序+树状数组写
常数缩小很多~~~
#include<iostream>#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>#include<vector>#include<queue>#include<set>#include<map>#include<stack>#include<bitset>#include<ext/pb_ds/priority_queue.hpp>using namespace std;const int maxn = 1E5 + 10;const int INF = ~0U>>1;struct data{int x,y,typ,num; data(){}data(int x,int y,int typ,int num): x(x),y(y),typ(typ),num(num){}bool operator < (const data &b) const{if (x < b.x) return 1;if (x > b.x) return 0;if (y < b.y) return 1;if (y > b.y) return 0;return typ < b.typ;}}D[maxn];int n,m,cur,tot,a[maxn],b[maxn],c[maxn],Max[maxn*2],A[maxn*2],f[maxn];void Pre_Work(int l,int r){int mid = (l + r) >> 1; tot = 0;for (int i = l; i <= mid; i++)A[++tot] = a[i],A[++tot] = c[i];for (int i = mid + 1; i <= r; i++)A[++tot] = a[i],A[++tot] = b[i];sort(A + 1,A + tot + 1); cur = 1;for (int i = 2; i <= tot; i++)if (A[i] != A[i-1]) A[++cur] = A[i];tot = 0;for (int i = l; i <= mid; i++){int x = max(a[i],c[i]),y = a[i];x = lower_bound(A + 1,A + cur + 1,x) - A;y = lower_bound(A + 1,A + cur + 1,y) - A;D[++tot] = data(x,y,0,i);}for (int i = mid + 1; i <= r; i++){int x = a[i],y = b[i];x = lower_bound(A + 1,A + cur + 1,x) - A;y = lower_bound(A + 1,A + cur + 1,y) - A;D[++tot] = data(x,y,1,i);}sort(D + 1,D + tot + 1);}void Solve(int l,int r){if (l == r) {++f[l]; return;}int mid = (l + r) >> 1;Solve(l,mid); Pre_Work(l,r);for (int i = 1; i <= cur; i++) Max[i] = 0;for (int i = 1; i <= tot; i++)if (!D[i].typ){for (int j = D[i].y; j <= cur; j += j&-j)Max[j] = max(Max[j],f[D[i].num]);}else{int now = 0;for (int j = D[i].y; j > 0; j -= j&-j)now = max(now,Max[j]);f[D[i].num] = max(f[D[i].num],now);}Solve(mid + 1,r);}int getint(){char ch = getchar();int ret = 0;while (ch < '0' || '9' < ch) ch = getchar();while ('0' <= ch && ch <= '9')ret = ret*10 + ch - '0',ch = getchar();return ret;}int main(){#ifdef DMCfreopen("DMC.txt","r",stdin);#endifn = getint(); m = getint();for (int i = 1; i <= n; i++)a[i] = getint(),b[i] = INF;while (m--){int x = getint(),y = getint();b[x] = min(b[x],y);c[x] = max(c[x],y);}Solve(1,n); int ans = 0;for (int i = 1; i <= n; i++)ans = max(ans,f[i]);cout << ans;return 0;}

0 0
原创粉丝点击