P1090 HELP!
  • 板块题目总版
  • 楼主conttyhello
  • 当前回复1
  • 已保存回复1
  • 发布时间2024/3/13 21:10
  • 上次更新2024/3/13 23:06:15
查看原帖
P1090 HELP!
1051492
conttyhello楼主2024/3/13 21:10
#include<iostream>
#include<stdio.h>
using namespace std;
int heap_size,n;
int heap[300001];
void swap(int &a,int &b){
    int t=a;a=b;b=t;
}
void put(int d){
    int son,pa;
    heap[heap_size++]=d;
    son = heap_size;
    while(son>1){
        pa=son>>1;
        if(heap[son]>=heap[pa])break;
        swap(heap[son],heap[pa]);
        son = pa;
    }
}
int get(){
    int pa,son,res;
    res = heap[1];
    heap[1]=heap[heap_size--];
    pa=1;
    while(pa*2<=heap_size){
        son = pa * 2;
        if(son<heap_size&&heap[son+1]<heap[son])son++;
        if(heap[pa]<=heap[son])break;
        swap(heap[pa],heap[son]);
        pa = son;
    }
    return res;
}
void work(){
    int x,y,ans=0;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin >> x;
        put(x);
    }
    for(int i=1;i<n;i++){
        x=get();
        y=get();
        ans+=x+y;
        put(x+y);
    }
    cout << ans <<endl;
}
int main(){
    ios::sync_with_stdio(false);
    work();
    return 0;
}

数据答案是九,可正确答案是15,错错在哪里了?

2024/3/13 21:10
加载中...