[哈夫曼树 堆] BZOJ 2923 [Poi1998]The lightest language
来源:互联网 发布:php protobuf 编辑:程序博客网 时间:2024/06/02 01:28
详见BYVOID大神的blog
可以看出这是一棵哈夫曼树的若干叶子节点
那么我们开始考虑从一棵空树扩展
如果叶节点数小于n 那么取最小的扩展
否则 如果取最小的扩展 答案更优的话继续 不优的话之后同样不会变优
扩展时记得删去一些最大的数 保持堆的大小
#include<cstdio>#include<cstdlib>#include<algorithm>#include<queue>using namespace std;inline char nc(){ static char buf[100000],*p1=buf,*p2=buf; if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; } return *p1++;}inline void read(int &x){ char c=nc(),b=1; for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1; for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;}const int N=10005;struct Heap1{ priority_queue<int> Q,del; void maintain(){ while (!Q.empty() && !del.empty() && Q.top()==del.top()) Q.pop(),del.pop(); } int top(){ maintain(); return Q.top(); } void pop(){ maintain(); Q.pop(); } void push(int x){ maintain(); Q.push(x); } void erase(int x){ maintain(); del.push(x); } bool empty(){ maintain(); return Q.empty(); } int size(){ maintain(); return Q.size()-del.size(); }}Max; struct Heap2{ priority_queue<int,vector<int>,greater<int> > Q,del; void maintain(){ while (!Q.empty() && !del.empty() && Q.top()==del.top()) Q.pop(),del.pop(); } int top(){ maintain(); return Q.top(); } void pop(){ maintain(); Q.pop(); } void push(int x){ maintain(); Q.push(x); } void erase(int x){ maintain(); del.push(x); } bool empty(){ maintain(); return Q.empty(); } int size(){ maintain(); return Q.size()-del.size(); }}Min; int n,K;int a[27];int Ans=1<<30;int tot;int main(){ freopen("t.in","r",stdin); freopen("t.out","w",stdout); read(n); read(K); for (int i=1;i<=K;i++) read(a[i]); for (int i=1;i<=K;i++) Max.push(a[i]),Min.push(a[i]),tot+=a[i]; while (1){ int x=Min.top(); Min.pop(); Max.erase(x); tot-=x; for (int i=1;i<=K;i++) Min.push(x+a[i]),Max.push(x+a[i]),tot+=x+a[i]; while (Min.size()>n){ int x=Max.top(); Max.pop(); Min.erase(x); tot-=x; } if (Min.size()==n){ if (Ans>tot)Ans=tot; elsebreak; } } printf("%d\n",Ans);}
0 0
- [哈夫曼树 堆] BZOJ 2923 [Poi1998]The lightest language
- 【贪心】【bzoj 2923】: [Poi1998]The lightest language
- [博弈 图论] BZOJ 2922 [Poi1998]Chase
- [贪心 归并排序] BZOJ 2920 [Poi1998]How to pack containers
- [并查集] BZOJ 2919 [Poi1998]Word equations
- [模型转化 最长下降子序列] BZOJ 2924 [Poi1998]Flat broken lines
- [数形结合 数位DP] BZOJ 2917 [Poi1998]Painter’s Studio
- The Language ⇔ Language Matrix
- The Language ⇔ Language Matrix
- BZOJ 4152 The Captain (Dijkstra 堆优化)
- The Curl Programming Language
- The Curl Programming Language
- The Jess language 1.
- The Jess language 3 .
- The Programming Language Tips
- The Jess language 4.
- The Programming Language Idioms
- The Curl Programming Language
- Codeforces 743D-Chloe and pleasant prizes
- 悬赏预告功能需求分析
- 时间工具类
- java.lang.NoClassDefFoundError: 异常处理记录
- 整数对查找
- [哈夫曼树 堆] BZOJ 2923 [Poi1998]The lightest language
- unity5.5 安卓烘焙问题
- mysql查询语句练习题跟答案
- ViewPager的setOnPageChangeListener方法详解
- pwnable 笔记 Rookiss - fsb - 20 pt
- Retrofit2.0使用详解
- Uva11021 Tribles 概率论
- 理解AngularJS的作用域Scope
- Android向网络提交数据并获取返回结果