POJ
来源:互联网 发布:csol一键瞬狙数据 编辑:程序博客网 时间:2024/06/08 07:11
传送门:POJ2010
题意:给出c个点对(a, b),从中选出n个来,使得sum(b)不大于f,并且由a组成的序列的中位数尽量的大。
正解:先将所有的点对按a排序,假设k是中位数对应的点对在原序列的位置,由中位数定义可知,[1,k-1]中必定选了(n-1)/2个点对,[k+1,c]中也必定选了(n-1)/2个,而且无论剩下的点对怎么选,最后影响结果的都只是k中的a。又因为我们要满足sum(b)<=f,因此可以预处理dp1[i]代表[1,i]中选出(n-1)/2个的最小sum(b),dp2[i]代表[i,c]中选出(n-1)/2个的最小sum(b),显然问题转化成了动态维护一段序列的前i小问题,果断优先队列。
代码:
#include<stdio.h>#include<iostream>#include<queue>#include<algorithm>#define ll long long#define pb push_back#define fi first#define se second#define pi acos(-1)#define inf 0x3f3f3f3f#define lson l,mid,rt<<1#define rson mid+1,r,rt<<1|1#define rep(i,x,n) for(int i=x;i<n;i++)#define per(i,n,x) for(int i=n;i>=x;i--)using namespace std;typedef pair<int,int>P;const int MAXN=100010;int gcd(int a,int b){return b?gcd(b,a%b):a;}int dp1[MAXN];int dp2[MAXN];priority_queue<int, vector<int>, less<int> >q;P p[MAXN];int main(){int n, c, f, sum = 0;cin >> n >> c >> f;for(int i = 0; i < c; i++)scanf("%d %d", &p[i].fi, &p[i].se);sort(p, p + c);n >>= 1;for(int i = 0; i < n; i++)q.push(p[i].se), sum += p[i].se;dp1[n - 1] = sum;for(int i = n; i < c; i++){sum += p[i].se;q.push(p[i].se);sum -= q.top();q.pop();dp1[i] = sum;}sum = 0;while(!q.empty()) q.pop();for(int i = c - 1; i > c - 1 - n; i--)q.push(p[i].se), sum += p[i].se;dp2[c - 1 - n + 1] = sum;for(int i = c - 1 - n; i >= 0; i--){sum += p[i].se;q.push(p[i].se);sum -= q.top();q.pop();dp2[i] = sum;}int ans = -1;for(int i = n; i <= c - n - 1; i++)if(dp1[i - 1] + dp2[i + 1] + p[i].se <= f)ans = max(ans, p[i].fi);cout << ans << endl; return 0;}
阅读全文
0 0
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- HDU 4995 Revenge of kNN
- IDEA本地执行 or 调试Spark Application的方法
- 跨函数使用malloc函数
- 详解如何将TensorFlow训练的模型移植到Android手机
- 密匙交换与加密
- POJ
- HDU2156 分数矩阵【数学计算+水题】
- python 进行深入学习
- ZOJ 2112 & BZOJ 1901 Dynamic Rankings(主席树 单点更新 区间第K大)
- 51Nod-1485-字母排序
- list2
- AlertDialog的基本使用
- Ubuntu搭建lamp环境
- OpenCV中的数值计算功能(一)矩阵求逆(伪逆)