poj2010

Moo University - Financial Aid

Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 3115 Accepted: 945

Description

Bessie noted that although humans have many universities they can attend, cows have none. To remedy this problem, she and her fellow cows formed a new university called The University of Wisconsin-Farmside,"Moo U" for short.

Not wishing to admit dumber-than-average cows, the founders created an incredibly precise admission exam called the Cow Scholastic Aptitude Test (CSAT) that yields scores in the range 1..2,000,000,000.

Moo U is very expensive to attend; not all calves can afford it.In fact, most calves need some sort of financial aid (0 <= aid <=100,000). The government does not provide scholarships to calves,so all the money must come from the university's limited fund (whose total money is F, 0 <= F <= 2,000,000,000).

Worse still, Moo U only has classrooms for an odd number N (1 <= N <= 19,999) of the C (N <= C <= 100,000) calves who have applied.Bessie wants to admit exactly N calves in order to maximize educational opportunity. She still wants the median CSAT score of the admitted calves to be as high as possible.

Recall that the median of a set of integers whose size is odd is the middle value when they are sorted. For example, the median of the set {3, 8, 9, 7, 5} is 7, as there are exactly two values above 7 and exactly two values below it.

Given the score and required financial aid for each calf that applies, the total number of calves to accept, and the total amount of money Bessie has for financial aid, determine the maximum median score Bessie can obtain by carefully admitting an optimal set of calves.

Input

* Line 1: Three space-separated integers N, C, and F

* Lines 2..C+1: Two space-separated integers per line. The first is the calf's CSAT score; the second integer is the required amount of financial aid the calf needs

Output

* Line 1: A single integer, the maximum median score that Bessie can achieve. If there is insufficient money to admit N calves,output -1.

Sample Input

3 5 70
30 25
50 21
20 20
5 18
35 30

Sample Output

35

Hint

Sample output:If Bessie accepts the calves with CSAT scores of 5, 35, and 50, the median is 35. The total financial aid required is 18 + 30 + 21 = 69 <= 70.

Source

USACO 2004 March Green

好题
题目意思是
告诉你要选出n个人(n为奇数)和总人数 和能提供的最大的帮助f
再告诉你每个人的成绩和所需的aid,
然后我们要从其中找出n个人来,保证aid的和<=f的条件下,使得他们的中位数最大
这题乍一看摸不着头脑,我们可以来分析一下
n为什么是奇数而不是偶数呢,显然,奇数的话,中位数必然是一个固定的数,而不是两个数的average
这样想,我们有一点思路了
我们可以枚举这个中位数,然后去验证有没有情况满足
但是怎么验证呢
首先,我们发现,有一部分必然不是中位数,这是前n/2小的和后n/2大的
所以我们先排一下序,只去枚举中间的一段当中位数,假设当前枚举第i个
那么我们必然要从左侧选n/2个数,设其和为f1[i-1],从右侧选n/2个数,设其和为f2[i+1]
使得f1+f2+need[i]<=f,
我们用f1[i-1]表示左侧中选出n/2个最小的,f2[i+1] 表示……
为什么和最小的呢,自己想去吧

然后就是怎么选呢,
这就用到最大堆的数据结构
先考虑从左侧选出n/2个使得和最小
我们维护一个元素个数为n/2的最大堆,
然后从n/2+1开始往堆中添加新元素,如果新元素小于堆顶,则添加并调整,
这样,我们总是能保证选出的元素和的值最小

同样右侧选n/2个也是如此

这样,这道题就解决了

最大堆维护最小和(dp)+枚举


苦逼的看题啊,最后没有结果输出-1,我输出0,wa了6次
哭……

code
#include <cstdio>
#include 
<cstdlib>
#include 
<cstring>
#include 
<cmath>
#include 
<ctime>
#include 
<cassert>
#include 
<iostream>
#include 
<sstream>
#include 
<fstream>
#include 
<map>
#include 
<set>
#include 
<vector>
#include 
<queue>
#include 
<algorithm>
#include 
<iomanip>
#define maxn 200005
using namespace std;
struct node 
{
    
int score,need;
}
a[maxn],tmp1;
long long sum,tmp;
int nn;
int n,c,f;
int dp1[maxn],dp2[maxn];
struct heapnode
{
    node x[maxn];
    
int num;
    
void nii(int n)
    
{
        
for(int i=1;i<=n/2;i++
            swap(x[i],x[n
-i+1]);
    }

    
long long getsum()
    
{
        
long long sum=0;
        
for(int i=1;i<=num;i++)
        
{
            sum
+=x[i].need;
        }

        
return sum;
    }

    
void down(int i,int m)
    
{
        
int t=2*i;
        
while(t<=m)
        
{
            
if(t<m&&x[t].need<x[t+1].need) t++;
            
if(x[i].need<x[t].need)
            
{
                swap(x[i],x[t]);
                i
=t;
                t
=i*2;
            }

            
else break;
        }

    }

    
void change(node tmpx)
    
{
        x[
1]=tmpx;
        down(
1,num);
    }

    
void build()
    
{
        
for(int i=num/2;i>=1;i--) down(i,num);
    }

}
heap1,heap2;
bool cmp(node t1,node t2)
{
    
if(t1.score>t2.score) 
        
return 0;
    
else if(t1.score<t2.score)
        
return 1;
    
else return t1.need<t2.need;
}

/*void print(heapnode t1)
{
    printf("\n");
    for(int i=1;i<=c;i++)
        printf("%d %d\n",t1.x[i].score,t1.x[i].need);
    printf("\n");
}
*/

int main()
{
    scanf(
"%d%d%d",&n,&c,&f);
    
for(int i=1;i<=c;i++) scanf("%d%d",&a[i].score,&a[i].need);
    sort(a
+1,a+c+1,cmp);
    memcpy(heap1.x,a,
sizeof(heap1.x));
    memcpy(heap2.x,a,
sizeof(heap2.x));
    heap2.nii(c);
    nn
=n/2;
    heap1.num
=nn;
    heap2.num
=nn;
    heap1.build();
    heap2.build();
    memset(dp1,
0,sizeof(dp1));
    memset(dp2,
0,sizeof(dp2));
    dp1[nn]
=heap1.getsum();
    dp2[nn]
=heap2.getsum();
    
for(int i=nn+1;i<=c-nn;i++)
    
{
        
if(heap1.x[i].need<heap1.x[1].need)
        
{
            dp1[i]
=dp1[i-1]-heap1.x[1].need+heap1.x[i].need;
            heap1.change(heap1.x[i]);
        }

        
else dp1[i]=dp1[i-1];
    }

    
for(int i=nn+1;i<=c-nn;i++)
    
{
        
if(heap2.x[i].need<heap2.x[1].need)
        
{
            dp2[i]
=dp2[i-1]-heap2.x[1].need+heap2.x[i].need;
            heap2.change(heap2.x[i]);
        }

        
else dp2[i]=dp2[i-1];
    }

    
bool flag;
    flag
=0;
    
for(int i=c-nn;i>=nn+1;i--)
    
{
        
if(a[i].need+dp1[i-1]+dp2[c-i]<=f)
        
{
            printf(
"%d\n",a[i].score);
            flag
=1;
            
break;
        }

    }

    
if(flag==0)
        printf(
"-1\n");
    
return 0;
}



posted on 2012-07-25 09:09 jh818012 阅读(207) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2024年7月>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

导航

统计

常用链接

留言簿

文章档案(85)

搜索

最新评论