60pts,后四个点mle了,求助
查看原帖
60pts,后四个点mle了,求助
798144
SukiYuri楼主2023/9/9 17:34
#include<bits/stdc++.h>
using namespace std;
struct tr{
    long long int me;
    tr* lch;
    tr* rch;
};
void push(tr* &node,long long int data){
    tr* newNode = new tr;
    newNode->me = data;
    newNode->lch = NULL;
    newNode->rch = NULL;
    if(node == NULL){
        node = newNode;
    }
    else if(data <= node->me){
        push(node->lch,data);
    }else{
        push(node->rch, data);
    }
}
void pop(tr* &node){
    if(node->lch == NULL && node->rch == NULL){
        delete node;
        node = NULL;
    }
    else if(node->lch == NULL){
        tr* temp = node->rch;
        delete node;
        node = temp;
    }
    else{
        pop(node->lch);
    }
}
long long int top(tr* node){
    if(node==NULL) return -1;
    else if(node->lch==NULL)return node->me;
    else return top(node->lch);
}
int main(){
    tr* head=NULL;
    int n;
    long long int ans=0;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        long long int t;
        scanf("%lld",&t);
        push(head,t);
    }
    while(true){
        int x=top(head);
        pop(head);
        int y=top(head);
        pop(head);
        ans+=x+y;
        if(head==NULL){
            break;
        }
        push(head,x+y);
    }
    printf("%lld",ans);
    return 0;
}
2023/9/9 17:34
加载中...