#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+5;
inline char gc(){
static char buf[1000000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
inline int read(){
int res=0;
char ch=gc();
while(ch<'0'||ch>'9')
ch=gc();
while(ch>='0'&&ch<='9'){
res=(res<<1)+(res<<3)+(ch^'0');
ch=gc();
}
return res;
}
int n,a[maxn],k=1,ans,f;
struct node{
int len,check,id;
bool operator <(const node &t) const{
if(len==t.len)
return id>t.id;
return len<t.len;
}
};
vector<node> s;
priority_queue<node> q;
bool vis[maxn];
inline int dfs(int x){
int l=0,r=s.size()-1;
while(l<r){
int mid=l+r>>1;
int k=s[mid].id;
if(k==x)
return mid;
if(k>x)
r=mid-1;
else
l=mid+1;
}
return r;
}
int main(){
n=read();
for(int i=1;i<=n;++i){
a[i]=read();
if(i!=1&&a[i]!=a[i-1]){
s.push_back(node({i-k,a[i-1],++f}));
q.push(node({i-k,a[i-1],f}));
k=i;
}
if(i==n){
s.push_back(node({n-k+1,a[n],++f}));
q.push(node({n-k+1,a[n],f}));
}
}
while(!q.empty()){
int x=q.top().id;
q.pop();
if(vis[x])
continue;
vis[x]=1;
int k=dfs(x);
++ans;
if(k&&k!=s.size()-1){
node A=s[k-1],B=s[k+1];
if(A.check==B.check){
A.len+=B.len;
vis[B.id]=1;
q.push(A);
s[k-1]=A;
s.erase(s.begin()+k+1);
}
}
s.erase(s.begin()+k);
}
printf("%d\n",ans);
return 0;
}
时间复杂度是 O(nlog2n),但它为什么会 TLE 啊(常数?)?