码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2010 Moo University - Financial Aid( 优先队列+二分查找)

时间:2016-04-03 14:26:11      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

POJ 2010 Moo University - Financial Aid

    题目大意,从C头申请读书的牛中选出N头,这N头牛的需要的额外学费之和不能超过F,并且要使得这N头牛的中位数最大.若不存在,则输出-1(一开始因为没看见这个,wa了几次).

    这个题的第一种做法就是用两个优先队列+贪心.

    

/*
* Created:     2016年03月27日 14时41分47秒 星期日
* Author:      Akrusher
*
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define inll(n) scanf("%I64d",&(n))
#define inll2(x1,x2) scanf("%I64d%I64d",&(x1),&(x2))
#define inlld(n) scanf("%lld",&(n))
#define inlld2(x1,x2) scanf("%lld%lld",&(x1),&(x2))
#define inf(n) scanf("%f",&(n))
#define inf2(x1,x2) scanf("%f%f",&(x1),&(x2))
#define inlf(n) scanf("%lf",&(n))
#define inlf2(x1,x2) scanf("%lf%lf",&(x1),&(x2))
#define inc(str) scanf("%c",&(str))
#define ins(str) scanf("%s",(str))
#define out(x) printf("%d\n",(x))
#define out2(x1,x2) printf("%d %d\n",(x1),(x2))
#define outf(x) printf("%f\n",(x))
#define outlf(x) printf("%lf\n",(x))
#define outlf2(x1,x2) printf("%lf %lf\n",(x1),(x2));
#define outlld(x) printf("%lld\n",(x))
#define outc(str) printf("%c\n",(str))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define mem(X,Y) memset(X,Y,sizeof(X));
typedef vector<int> vec;
typedef long long ll;
typedef pair<int,int> P;
const int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
const int INF=0x3f3f3f3f;
const ll mod=1e9+7;
ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
const bool AC=true;

struct point{
ll a;
ll b;
};
point p[100010];
ll l[100005];
ll r[100005];
bool cmp(point x,point y){
 return x.a>y.a;
}
int main()
{
    
    ll c,n,ans,sum,sum1,sum2,f;
    while(scanf("%lld %lld %lld",&c,&n,&f)==3){
    rep(i,0,n)
    inlld2(p[i].a,p[i].b);
    sort(p,p+n,cmp);
    sum1=sum2=0;sum=0;
    priority_queue <int> que1;
    priority_queue <int> que2;
    mem(l,0);
    mem(r,0);
    rep(i,0,n){ //遍历数组,找出left[i],right[i];
    if(i<c/2){  //i左边的数少于c/2时,全部加入队列
    sum1+=p[i].b;
    que1.push(p[i].b);
    }
    else{
    l[i]=sum1;
    if(p[i].b<que1.top()){
    sum1-=que1.top(); //更新最小值
    sum1+=p[i].b;
    que1.pop();
    que1.push(p[i].b);
    }
    }
    }
    per(i,0,n){
    if(i>=n-c/2){
    sum2+=p[i].b;
    que2.push(p[i].b);
    }
    else{
    r[i]=sum2;
    if(p[i].b<que2.top()){
    sum2-=que2.top();
    sum2+=p[i].b;
    que2.pop();
    que2.push(p[i].b);
    }
    }
    }
    ans=-1;
    rep(i,c/2,n-c/2){
    sum=p[i].b+l[i]+r[i];
    if(sum<=f){
    ans=p[i].a;
    break;
    }
    }
    outlld(ans);
    }
    return 0;
}

这一题的第二种做法是二分

 

POJ 2010 Moo University - Financial Aid( 优先队列+二分查找)

标签:

原文地址:http://www.cnblogs.com/akrusher/p/5349665.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!