这题做完之后整场比赛就AK了(虽然是赛后),感觉赛后得把题全做了比赛才会更有意义撒。。
题意为找最大重复序列并依次删除。。
解法一:在汪聚聚的提醒下才发现其实用线段树就能实现啊。。orz。。维护的东西有点多,实现起来的难度比想象的要小很多,连pushdown都没有,果然CF是注重想法的。。
update时考虑维护最长区间,能够产生最长区间的有三种情况 ,做子树的最长区间,右子树的最长区间以及左子树右端重复序列和右子树左端重复序列合并起来的序列,因此需要维护左右端的重复序列。。然后考虑到序列中间有空缺。。所以需要记录长度去比较(我竟然用傻叉到用区间长度去比较。。。)然后注意一下最长区间要从左到右取就可以了。。
解法二:然后不小心看到了别人用STL的做法。。然后发现自己真的是智障。。有优先队列不用却去手写数据结构。。有个现成的堆用多好啊。。。先附上链接:
http://blog.csdn.net/yxm980918/article/details/78887737
其实有想过用平衡树实现。。不过说到平衡树我就不想写了。。所以思路是能避开就避开。。这个模拟链表之后其实就只剩找最大区间了。。用优先队列去找就可以。。
然后优先队列其实自己并不擅长。。在做题的过程中被卡了好多次均因为不熟悉STL所致。。优先队列里面最好是不能放指针的,一个是因为如果指针指向内容改变,那么优先队列是不会再排序的,这个参照堆的维护过程是不难想象的。。所以在指向内容改变的时候还要再入队。。然后问题又来了。。入队的过程中如果按照堆的路径不巧碰到了原来的指针,那么原指针的内存也相应改变,因此可能就会停在原指针后面。。(orz..膜拜造这组数据的神犇
解法一:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define inf 1e9
#define eps 1e-8
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define NM 200005
#define pi 3.141592653
using namespace std;
int read(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return f*x;
}
int n,s,_x,_y;
struct info{
int size,l,r,_l,_r,s,ls,rs,lt,rt;
info operator+(const info&o){
info f;f.size=size+o.size;
if(size==0){
if(o.size==0)return f;
return o;
}
if(o.size==0)return *this;
f.l=l;f.r=r;f.s=s;
if(rt==o.lt&&f.s<o.ls+rs)f.l=_r,f.r=o._l,f.s=o.ls+rs;
if(o.s>f.s)f.l=o.l,f.r=o.r,f.s=o.s;
f._l=_l;f._r=o._r;f.lt=lt;f.rt=o.rt;f.ls=ls;f.rs=o.rs;
if(_l>=_r&<==o.lt)f._l=o._l,f.ls+=o.ls;
if(o._l>=o._r&&o.rt==rt)f._r=_r,f.rs+=rs;
return f;
}
}T[4*NM];
void build(int i,int x,int y){
int t=x+y>>1;
if(x==y){
T[i].size=T[i].s=T[i].ls=T[i].rs=1;
T[i].l=T[i].r=T[i]._l=T[i]._r=x;
T[i].lt=T[i].rt=read();
return;
}
build(i<<1,x,t);build(i<<1|1,t+1,y);
T[i]=T[i<<1]+T[i<<1|1];
}
void mod(int i,int x,int y){
int t=x+y>>1;
if(y<_x||_y<x||!T[i].size)return;
if(_x<=x&&y<=_y){T[i].size=0;return;}
mod(i<<1,x,t);mod(i<<1|1,t+1,y);
T[i]=T[i<<1]+T[i<<1|1];
}
int main(){
//freopen("data.in","r",stdin);
n=read();
build(1,1,n);
while(T[1].size){
_x=T[1].l;_y=T[1].r;
mod(1,1,n);
s++;
}
printf("%d\n",s);
return 0;
}
解法二:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define inf 1000000007
#define eps 1e-8
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define NM 200005
#define nm 800005
#define pi 3.141592653
using namespace std;
int read(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return f*x;
}
int n,_x,cnt,pre[NM],next[NM],c[NM],a[NM],ans;
struct tmp{
int s,id;
tmp(int s=0,int id=0):s(s),id(id){}
bool operator<(const tmp&o)const{return s<o.s||(s==o.s&&id>o.id);}
};
priority_queue<tmp>q;
bool v[NM];
int main(){
n=read();
inc(i,1,n){
_x=read();
if(a[cnt]==_x)c[cnt]++;
else{a[++cnt]=_x;c[cnt]=1;}
}
inc(i,1,cnt)q.push(tmp(c[i],i));
inc(i,1,cnt)pre[i]=i-1,next[i]=i+1;
while(cnt>0){
tmp t=q.top();q.pop();
while(v[t.id])t=q.top(),q.pop();
int i=t.id,x=pre[i],y=next[i];
if(a[x]==a[y]){
next[x]=next[y];pre[next[y]]=x;
c[x]+=c[y];v[y]++;q.push(tmp(c[x],x));cnt--;
}else{
next[x]=y;pre[y]=x;
}
ans++;v[i]++;cnt--;
}
printf("%d\n",ans);
return 0;
}
Vasya has an array of integers of length n.
Vasya performs the following operations on the array: on each step he finds the longest segment of consecutive equal integers (the leftmost, if there are several such segments) and removes it. For example, if Vasya's array is[13, 13, 7, 7, 7, 2, 2, 2], then after one operation it becomes[13, 13, 2, 2, 2].
Compute the number of operations Vasya should make until the array becomes empty, i.e. Vasya removes all elements from it.
The first line contains a single integer n (1 ≤ n ≤ 200 000) — the length of the array.
The second line contains a sequence a1, a2, ..., an (1 ≤ ai ≤ 109) — Vasya's array.
Print the number of operations Vasya should make to remove all elements from the array.
4 2 5 5 2
2
5 6 3 4 1 5
5
8 4 4 4 2 2 100 100 100
3
6 10 10 50 10 50 50
4
In the first example, at first Vasya removes two fives at the second and third positions. The array becomes[2, 2]. In the second operation Vasya removes two twos at the first and second positions. After that the array becomes empty.
In the second example Vasya has to perform five operations to make the array empty. In each of them he removes the first element from the array.
In the third example Vasya needs three operations. In the first operation he removes all integers4, in the second — all integers100, in the third — all integers 2.
In the fourth example in the first operation Vasya removes the first two integers10. After that the array becomes[50, 10, 50, 50]. Then in the second operation Vasya removes the two rightmost integers50, so that the array becomes[50, 10]. In the third operation he removes the remaining50, and the array becomes [10] after that. In the last, fourth operation he removes the only remaining10. The array is empty after that.