BZOJ 3728 PA2014Final Zarowki 堆+贪心
2015-03-03 16:58
218 查看
题目大意:给定n个灯泡和n个房间,每个灯泡有一个功率,每个房间有一个照亮的最小功率,可以换k个灯泡,求照亮所有房间的最小功率
将灯泡的功率和房间的最小功率排序,从大到小扫描每个房间
对于一个房间,首先将能照亮这个房间的灯泡都加入堆
如果堆为空则花掉一次换灯泡的机会换一个功率为这个房间的最小功率的灯泡
否则取走功率最小的灯泡照亮这个房间,并将灯泡功率与房间最小功率的差值加入另一个堆
结束时如果还有换灯泡的机会,就把第二个堆中最大的k个差值换掉
将灯泡的功率和房间的最小功率排序,从大到小扫描每个房间
对于一个房间,首先将能照亮这个房间的灯泡都加入堆
如果堆为空则花掉一次换灯泡的机会换一个功率为这个房间的最小功率的灯泡
否则取走功率最小的灯泡照亮这个房间,并将灯泡功率与房间最小功率的差值加入另一个堆
结束时如果还有换灯泡的机会,就把第二个堆中最大的k个差值换掉
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define M 500500 using namespace std; struct Heap{ int heap[M],top; void Insert(int x) { heap[++top]=x; int t=top; while(t>1) { if(heap[t]>heap[t>>1]) swap(heap[t],heap[t>>1]),t>>=1; else break; } } void Pop() { heap[1]=heap[top--]; int t=2; while(t<=top) { if( t<top && heap[t+1]>heap[t] ) ++t; if(heap[t]>heap[t>>1]) swap(heap[t],heap[t>>1]),t<<=1; else break; } } }heap1,heap2; int n,k,a[M],b[M]; long long ans; int main() { int i,j; cin>>n>>k; for(i=1;i<=n;i++) scanf("%d",&a[i]); for(i=1;i<=n;i++) scanf("%d",&b[i]); sort(a+1,a+n+1); sort(b+1,b+n+1); for(i=n,j=n;i;i--) { for(;j&&a[j]>=b[i];j--) heap1.Insert(-a[j]); if(!heap1.top) { k--;ans+=b[i]; if(k==-1) return puts("NIE"),0; } else { int x=-heap1.heap[1];heap1.Pop(); ans+=x; heap2.Insert(x-b[i]); } } while(k--) ans-=heap2.heap[1],heap2.Pop(); cout<<ans<<endl; return 0; }
相关文章推荐
- BZOJ[3728]PA2014 Final Zarowki 堆+贪心
- BZOJ 3728: PA2014Final Zarowki|贪心
- bzoj 1555: KD之死 (贪心+STL)
- BZOJ 2525 [Poi2011]Dynamite 二分+树形贪心
- 贪心-BZOJ-1628-[Usaco2007 Demo]City skyline
- BZOJ_3174_[Tjoi2013]拯救小矮人_贪心+DP
- BZOJ 1034: [ZJOI2008]泡泡堂BNB 贪心 田忌赛马
- BZOJ 3668 NOI2014 起床困难综合症 贪心
- 【bzoj4619】[Wf2016]Swap Space 贪心
- [ 可并堆 贪心 ] [ Baltic2004 ] BZOJ1367
- BZOJ 1124 POI2008 枪战Maf 贪心
- [BZOJ1029] [JSOI2007]建筑抢修(贪心 + 优先队列)
- 【BZOJ】1691: [Usaco2007 Dec]挑剔的美食家(set+贪心)
- bzoj 3728 zarovik 买灯泡
- bzoj2006: [NOI2010]超级钢琴 贪心+堆
- 【bzoj1572】[Usaco2009 Open]工作安排Job 贪心+堆
- BZOJ3709 [PA2014]Bohater 【贪心】
- Bzoj 2241: [SDOI2011]打地鼠 暴力,枚举,贪心
- bzoj 1634: [Usaco2007 Jan]Protecting the Flowers 护花(贪心排序)
- BZOJ_3790_神奇项链_manacher+贪心