怎么没dfs的
查看原帖
怎么没dfs的
849602
封禁用户楼主2024/6/15 09:56
int n,m,t,ans,stone[50005];
void dfs(int k){
    int tmp=stone[k-1]+stone[k];
    ans+=tmp;
	t--;
    for(int i=k;i<t;++i)stone[i]=stone[i+1];
    int j=0;
	k--;
    for(j=k;stone[j-1]<tmp;--j)stone[j]=stone[j-1];
    stone[j]=tmp;
    while(j>=3&&stone[j-2]<=stone[j]){
        int d=t-j;
        dfs(j-1);
        j=t-d;
    }
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;++i)scanf("%d",&stone[i]);
    t=2,ans=0;stone[0]=stone[n+1]=0x7fffffff;
    for(int i=2;i<=n;++i){
        stone[t++]=stone[i];
        while(t>3&&stone[t-3]<=stone[t-1])dfs(t-2);
    }
    while(t>2)dfs(t-1);
    printf("%d",ans);
    return 0;
}```
[1](https://www.luogu.com.cn/record/162172822)
2024/6/15 09:56
加载中...