• POJ 2010


    Moo University - Financial Aid

    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 14558   Accepted: 4320

    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. 

    ac代码:

    // #include<bits/stdc++.h>
    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <queue> //priority_queue
    #include <map>
    #include <set> //multiset set<int,greater<int>>大到小
    #include <vector> // vector<int>().swap(v);清空释放内存
    #include <stack>
    #include <cmath> // auto &Name : STLName  Name.
    #include <utility>
    #include <sstream>
    #include <string> //__builtin_popcount(ans);//获取某个数二进制位1的个数
    using namespace std;
    #define rep(i,a,n) 	for(int i=a;i<=n;i++)
    #define per(i,a,n) 	for(int i=n;i>=a;i--)
    #define read_a_int(x) 	scanf("%d",&x)
    #define Read(x,y) 		scanf("%d%d",&x,&y)
    typedef long long ll;
    
    const int INF = 0x3f3f3f3f;
    const int mod1e9 = 1000000007;
    const int mod998 = 998244353;
    const int mod = mod1e9;
    const int MAX_N = 100000 + 10;
    
    // int read()
    // {
    //     int x=0,f=1;char ch=getchar();
    //     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    //     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    //     return x*f;
    // }
    
    struct Data
    {
    	int s,f;
    }cow[MAX_N];
    
    bool cmp(Data a,Data b)
    {
    	if(a.s!=b.s)
    		return a.s>b.s;
    	else
    		return a.f<b.f;
    }
    
    priority_queue<int> Q;
    int dpl[MAX_N];
    int dpr[MAX_N];
    
    int main(void)
    {	
    	// ios::sync_with_stdio(false);
    	int n,c,f;
    	while(scanf("%d%d%d",&n,&c,&f)!=EOF)
    	{
    		rep(i,1,c)
    			scanf("%d%d",&cow[i].s,&cow[i].f);
    		sort(cow+1,cow+c+1,cmp);
    
    		while(!Q.empty())
    			Q.pop();
    		
    		int nu=(n-1)/2;
    		int sum=0;
    
    		rep(i,1,nu){
    			Q.push(cow[i].f);
    			sum+=cow[i].f;
    		}
    		dpl[nu]=sum;
    		rep(i,nu+1,c){
    			if(cow[i].f>=Q.top())
    				dpl[i]=sum;
    			else{
    				sum=sum-Q.top()+cow[i].f;
    				Q.pop();
    				Q.push(cow[i].f);
    				dpl[i]=sum;
    			}
    		}
    
    		sum=0;
    		while(!Q.empty())
    			Q.pop();
    		per(i,c,c-nu+1){
    			Q.push(cow[i].f);
    			sum+=cow[i].f;
    		}
    		dpr[c-nu+1]=sum;
    		per(i,c-nu,1){
    			if(cow[i].f>=Q.top())
    				dpr[i]=sum;
    			else{
    				sum=sum-Q.top()+cow[i].f;
                    Q.pop();
                    Q.push(cow[i].f);
                    dpr[i]=sum;
    			}
    		}
    
    		bool flag=false;
    		rep(i,nu+1,c-nu)
    		{
    			if(cow[i].f+dpr[i+1]+dpl[i-1]<=f)
    			{
    				flag=true;
    				printf("%d
    ",cow[i].s);
    				break;
    			}
    		}
    		if(!flag)
    			printf("-1
    ");
    
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    ASP.NET Web API 2 之文件下载
    Windows 查看某个端口号是否被占用
    C# 数据类型之 String(字符串)
    数据表对应关系(一对一、一对多、多对多)
    嫁给程序员的好处,你get到了吗?
    ASP.NET Web API 2 之 HttpRequestMessage 对象
    C# 使用 log4net 记录日志
    Chrome 浏览器快捷键
    C# 获取程序运行时路径
    SpringBoot整合Pagehelper分页插件
  • 原文地址:https://www.cnblogs.com/jaszzz/p/12831148.html
Copyright © 2020-2023  润新知