G. Let Them Eat Cake
一道考察链表的题目,具体含义不再阐述,根据题意涉好程序就行
#include<bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
//构建链表
int ne[n+1],pa[n+1],la[n+1];
int cnt = 0;
int k = n;
for(int i=1;i<=n;i++){
cin>>pa[i];
la[i] = i-1;
ne[i-1] = i;
ne[i] = -1;
}
int ans=0;
//根据题意,完成对列表的缩减
while(k!=1)
{
ans++;
int z=0;
vector<int> qq;
while(true)
{
z = ne[z];
if(z!=-1)
{
bool find = false;
if(la[z]!=0){
if(pa[z]<pa[la[z]]) find = true;
}
if(ne[z]!=-1){
if(pa[z]<pa[ne[z]]) find = true;
}
if(find) qq.push_back(z);
}
else break;
}
k-=qq.size();
for(int i=0;i<qq.size();i++) {
// cout<<qq[i]<<" ";
int j = qq[i];
ne[la[j]] = ne[j];
if(ne[j]!=-1)
la[ne[j]] = la[j];
}
// cout<<endl;
}
cout<<ans;
}