[堆]51 Nod 1461——稳定桌
2017-10-25 18:02
197 查看
题目描述
有一张桌子,有n个腿。第i根腿的长度是li。现在要拿掉一些腿,使得桌子稳定,拿掉第i根腿需要di的能量。
稳定的条件是,假如拿掉若干条腿之后,桌子还有k个腿,那么长度最长的腿的数目要超过一半。比如桌子有5根腿,那么至少要有三根腿是最长的。另外,只有一根腿的桌子是稳定的,两个腿的桌子想要稳定,必需长度是一样的。
你的任务是拿掉若干腿,使得桌子稳定,并且所消耗的能量要最少。
解题思路
考虑枚举长度,每个长度的个数我们可以知道。那么我们显然要保留个数-1个小于该长度的能量最大的腿。
这个用两个堆维护就可以了。
虽然保持堆中个数稳定是稳的,但是交换堆顶的复杂度不可估,但应该非常难卡。
#include<cstdio> #include<queue> #include<algorithm> #define LL long long using namespace std; char nc(){ static char buf[100000],*l=buf,*r=buf; if (l==r) r=(l=buf)+fread(buf,1,100000,stdin); if (l==r) return EOF;return *l++; } inline int _read(){ int num=0;char ch=nc(); while(ch<'0'||ch>'9') ch=nc(); while(ch>='0'&&ch<='9') num=num*10+ch-48,ch=nc(); return num; } const int maxn=100005; struct jz{ int x,w; bool operator<(const jz &b)const{return x<b.x;} }a[maxn]; priority_queue<int> Q1; priority_queue<int,vector<int>,greater<int> >Q2; int n; LL sum,ans,now; int main(){ freopen("exam.in","r",stdin); freopen("exam.out","w",stdout); n=_read(); for (int i=1;i<=n;i++) a[i].x=_read(); for (int i=1;i<=n;i++) a[i].w=_read(),sum+=a[i].w; sort(a+1,a+1+n); int i=1; while(i<=n){ int j=i;LL tot=0; while(j<=n&&a[i].x==a[j].x) j++; while(Q2.size()<j-i-1&&!Q1.empty()) now+=Q1.top(),Q2.push(Q1.top()),Q1.pop(); while(Q2.size()>j-i-1&&!Q2.empty()) now-=Q2.top(),Q1.push(Q2.top()),Q2.pop(); while(!Q1.empty()&&!Q2.empty()&&Q2.top()<Q1.top()) now+=Q1.top()-Q2.top(),Q2.push(Q1.top()),Q1.pop(),Q1.push(Q2.top()),Q2.pop(); while(i<j) tot+=a[i].w,Q1.push(a[i].w),i++; if (now+tot>ans) ans=now+tot; } printf("%lld\n",sum-ans); return 0; }C++
相关文章推荐
- 51nod1081---子段求和(51nod基础:前缀和)
- 51 nod 1076 2条不相交的路径(强联通)
- D - 最大子矩阵和 51Nod - 1051
- 51 nod 1097 拼成最小的数 思路:字符串排序
- 51 nod 最小1的数量 数位DP
- 数塔取数问题 --51Nod 1级算法
- 1006 最长公共子序列Lcs(输出路径)(51NOD基础题)
- 51 nod 1107 斜率小于0的连线数量
- [数学杂题]51 Nod 1765——谷歌的恐龙
- [贪心+单调队列+ST算法]51 nod 1288 ——汽油补给
- 51 nod 1385 凑数字(贪心+构造)
- [树状数组]51 Nod 1711——平均数
- [单调队列]51 Nod 1952——栈
- Java中如何让bigdecimal不用科学计数法 显示(51Nod 1873 初中的算术)
- 聪明的木匠(51nod 1117)
- 51 nod 1179 最大的最大公约数
- [51Nod](1284) 2 3 5 7的倍数 ---- 容斥原理
- 51-nod 1003 . 阶乘后面0的数量
- 51 Nod 1215 数组的宽度(单调栈)
- 数位DP (51nod)