POJ 2010 Moo University - Financial Aid
2016-04-04 15:57
483 查看
枚举中位数+优先级队列预处理前x个数选n个最小和
#include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<vector> #include<iostream> #include<algorithm> using namespace std; typedef long long LL; const int maxn=100000+10; int n,c; LL f; struct X { LL s; LL v; } t[maxn]; priority_queue<LL>q; LL dp1[maxn],dp2[maxn]; bool cmp(const X&a,const X&b) { return a.s<b.s; } int main() { scanf("%d%d%lld",&n,&c,&f); for(int i=1; i<=c; i++) scanf("%lld%lld",&t[i].s,&t[i].v); sort(t+1,t+1+c,cmp); LL ans=-1; if(c>=n) { if(n==1) { for(int i=1; i<=c; i++) if(t[i].v<=f) ans=t[i].s; } else { while(!q.empty()) q.pop(); int num=(n-1)/2; for(int i=1; i<=num; i++) dp1[num]=dp1[num]+t[i].v,q.push(t[i].v); for(int i=num+1; i<=c; i++) { if(t[i].v>=q.top()) dp1[i]=dp1[i-1]; else { dp1[i]=dp1[i-1]-(q.top()-t[i].v); q.pop(); q.push(t[i].v); } } while(!q.empty()) q.pop(); for(int i=c; i>=c-num+1; i--) dp2[c-num+1]=dp2[c-num+1]+t[i].v,q.push(t[i].v); for(int i=c-num; i>=1; i--) { if(t[i].v>=q.top()) dp2[i]=dp2[i+1]; else { dp2[i]=dp2[i+1]-(q.top()-t[i].v); q.pop(); q.push(t[i].v); } } for(int i=1; i<=c; i++) { if(i-1<num||c-i<num) continue; if(dp1[i-1]+dp2[i+1]+t[i].v<=f) ans=t[i].s; } } } printf("%lld\n",ans); return 0; }
相关文章推荐
- 【BZOJ-2434】阿狸的打字机 AC自动机 + Fail树 + DFS序 + 树状数组
- linker command failed with exit code 1)错误总结
- 70. Climbing Stairs
- 本地socket unix domain socket
- 杭电1022Train Problem I
- A. Dreamoon and Stairs(Codeforces Round #272)
- QPainter 使用注意事项
- 杭电1021Fibonacci Again
- 解决sublimeText3无法安装插件有关问题 - There are no packages available for installation
- LeetCode Palindrome Pairs
- 服务器TIME_WAIT和CLOSE_WAIT详解和解决办法
- Tensorflow 官方版教程中文版
- 解决time_wait过多的问题
- 2016微软开发者大会 - 简洁收集
- 70. Climbing Stairs
- RAID各级别特性
- 学习笔记之RAID各级别特性
- wait()、notify()、notifyAll()
- 10013---Trail ~ Catalogs
- POJ 1273 USACO 93 Drainage Ditches 最大流