noip 合并果子
#include using namespace std;int heap[maxn];int size=0;void input(int d){ heap[size++]=d; push_heap(heap,heap+size,greater ());}int get(){ pop_heap(heap,heap+size,greater ()); //pop_heap(heap,heap+size); return heap[--size];}int32_t main(){ int n; cin>>n; for(int i=0;i >d; input(d); } int ans=0; for(int i=1;i
好的代码
#include using namespace std;template void Print(T& V){ typename T::iterator iter=V.begin(); while(iter != V.end()) { cout<<*iter++<<" "; } cout<
ivec; for(int i=3;i<=7;++i) ivec.push_back(i); for(int i=5;i<=9;++i) ivec.push_back(i); for(int i=1;i<=4;++i) ivec.push_back(i); cout<<"原数据:"; Print(ivec); make_heap(ivec.begin(),ivec.end());//做最大堆排序,其实还在vector容器内 cout<<"堆排后:"; Print(ivec); pop_heap(ivec.begin(),ivec.end());//删除最大堆,其实是把数据放到最后了! cout<<"删除后:"; Print(ivec); ivec.pop_back(); pop_heap(ivec.begin(),ivec.end());//删除最大堆,其实是把数据放到最后了! cout<<"删除后:"; Print(ivec); ivec.pop_back(); ivec.push_back(15); cout<<"添加数据后:"; Print(ivec); push_heap(ivec.begin(),ivec.end());//放入最大堆,其实是把新加入的数据,按照堆排加入堆内 cout<<"把最后一个数加入堆里:\n"; Print(ivec); sort_heap(ivec.begin(),ivec.end());//把堆排顺序,还原成一般的排序算法 cout<<"还原堆排顺序:\n"; Print(ivec); return 0;}