Run ID 作者 问题 语言 测评结果 分数 时间 内存 代码长度 提交时间
134701 朱悦晨 紧急集合 C++ 通过 100 3 MS 292 KB 776 2024-03-02 17:04:19

Tests(10/10):


#include<bits/stdc++.h> using namespace std; int heap[100002],heap_size; void put(int x) { int f; heap_size++; heap[heap_size]=x; int b=heap_size; while(b!=1&&heap[b]<heap[b/2]) { swap(heap[b],heap[b/2]); b=b/2; } } int get() { int x=heap[1],c; heap[1]=heap[heap_size]; heap_size--; int f=1,b,t; while(f*2<=heap_size) { if(f*2+1>heap_size||heap[f*2]<heap[f*2+1]) { c=f*2; } else { c=f*2+1; } if(heap[f]>heap[c]) { swap(heap[f],heap[c]); f=c; } else { return x; } } return x; } int main() { int n; cin>>n; for(int i=1; i<=n; i++) { int x; cin>>x; put(x); } int ans=0; while(heap_size!=1) { int k=get(); int c=get(); ans+=k+c; put(c+k); } cout<<ans; return 0; }


测评信息: