Moo University - Financial Aid

Descriptions

奶牛大学:奶大招生,从C头奶牛中招收N(N为奇数)头。它们分别得分score_i,需要资助学费aid_i。希望新生所需资助不超过F,同时得分中位数最高。求此中位数。

Input

*第1行:三个以空格分隔的整数N,C和F

*第2..C + 1行:每行两个以空格分隔的整数。首先是小牛的CSAT分数; 第二个整数是小牛所需的经济援助金额


Output

*第1行:一个整数,即Bessie可以达到的最大中位数分数。如果没有足够的钱来接纳N小牛,输出-1。 


Sample Input

  1. 3 5 70
  2. 30 25
  3. 50 21
  4. 20 20
  5. 5 18
  6. 35 30

Sample Output

  1. 35

Hint

样本输出如果Bessie接受CSAT分数为5,35和50的小牛,则中位数为35.所需的总经济援助为18 + 30 + 21 = 69 <= 70。 
 
题目链接
 
先将奶牛按分数排序,考虑每个奶牛作为中位数时,比它分数低(前面的)的那群牛的学费总和lower_i,后面的总和upper_i。然后从分数高往分数低扫描,满足aid_i + lower_i + upper_i <= F的第一个解就是最优解。
 
AC代码
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <fstream>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <deque>
  7. #include <vector>
  8. #include <queue>
  9. #include <string>
  10. #include <cstring>
  11. #include <map>
  12. #include <stack>
  13. #include <set>
  14. #include <sstream>
  15. #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
  16. #define Mod 1000000007
  17. #define eps 1e-6
  18. #define ll long long
  19. #define INF 0x3f3f3f3f
  20. #define MEM(x,y) memset(x,y,sizeof(x))
  21. #define Maxn 100005
  22. #define P pair<int,int>
  23. using namespace std;
  24. P a[Maxn];
  25. int N,C,F;
  26. // 牛i作为中位数时,lower[i]表示分数低于它的牛的学费总和
  27. int lower[Maxn],upper[Maxn];
  28. int main()
  29. {
  30. cin>>N>>C>>F;
  31. int half=N/;
  32. for(int i=; i<C; i++)
  33. cin>>a[i].first>>a[i].second; //分数 学费
  34. sort(a,a+C);
  35. {
  36. //求出lower[i]
  37. int total=;
  38. priority_queue<int>q;
  39. for(int i=; i<C; i++)
  40. {
  41. lower[i]=q.size()==half?total:INF;
  42. q.push(a[i].second);
  43. total+=a[i].second;
  44. if(q.size()>half)
  45. {
  46. //去掉一个学费最高的
  47. total-=q.top();
  48. q.pop();
  49. }
  50. }
  51. }
  52. {
  53. //求出upper[i]
  54. int total=;
  55. priority_queue<int>q;
  56. for(int i=C-; i>=; i--)
  57. {
  58. upper[i]=q.size()==half?total:INF;
  59. q.push(a[i].second);
  60. total+=a[i].second;
  61. if(q.size()>half)
  62. {
  63. //去掉一个学费最高的
  64. total-=q.top();
  65. q.pop();
  66. }
  67. }
  68. }
  69. int ans=-1;
  70. for(int i=C-; i>=; i--)
  71. if(a[i].second+lower[i]+upper[i]<=F)
  72. {
  73. ans=a[i].first;
  74. break;
  75. }
  76. cout<<ans<<endl;
  77. return ;
  78. }

【POJ - 2010】Moo University - Financial Aid(优先队列)的更多相关文章

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

    POJ 2010 Moo University - Financial Aid 题目大意,从C头申请读书的牛中选出N头,这N头牛的需要的额外学费之和不能超过F,并且要使得这N头牛的中位数最大.若不存在 ...

  2. poj -2010 Moo University - Financial Aid (优先队列)

    http://poj.org/problem?id=2010 "Moo U"大学有一种非常严格的入学考试(CSAT) ,每头小牛都会有一个得分.然而,"Moo U&quo ...

  3. POJ 2010 Moo University - Financial Aid 优先队列

    题意:给你c头牛,并给出每头牛的分数和花费,要求你找出其中n(n为奇数)头牛,并使这n头牛的分数的中位数尽可能大,同时这n头牛的总花费不能超过f,否则输出-1. 思路:首先对n头牛按分数进行排序,然后 ...

  4. poj 2010 Moo University - Financial Aid

                                                                                                Moo Univ ...

  5. poj 2010 Moo University - Financial Aid 最大化中位数 二分搜索 以后需要慢慢体会

    Moo University - Financial Aid Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6599   A ...

  6. poj 2010 Moo University - Financial Aid(优先队列(最小堆)+ 贪心 + 枚举)

    Description Bessie noted that although humans have many universities they can attend, cows have none ...

  7. POJ 2010 - Moo University - Financial Aid 初探数据结构 二叉堆

    考虑到数据结构短板严重,从计算几何换换口味= = 二叉堆 简介 堆总保持每个节点小于(大于)父亲节点.这样的堆被称作大根堆(小根堆). 顾名思义,大根堆的数根是堆内的最大元素. 堆的意义在于能快速O( ...

  8. POJ 2010 Moo University - Financial Aid (优先队列)

    题意:从C头奶牛中招收N(奇数)头.它们分别得分score_i,需要资助学费aid_i.希望新生所需资助不超过F,同时得分中位数最高.求此中位数. 思路: 先将奶牛排序,考虑每个奶牛作为中位数时,比它 ...

  9. poj 2010 Moo University - Financial Aid (贪心+线段树)

    转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 骗一下访问量.... 题意大概是:从c个中选出n个 ...

  10. POJ 2010 Moo University - Financial Aid treap

    按第一关键字排序后枚举中位数,就变成了判断“左边前K小的和 + 这个中位数 + 右边前K小的和 <= F",其中维护前K小和可以用treap做到. #include <cstdi ...

随机推荐

  1. 报警提示 System.NullReferenceException:“未将对象引用设置到对象的实例。

    System.NullReferenceException:“未将对象引用设置到对象的实例.是就因为Session在记录到服务器时,没有添加  IRequiresSessionState 所以运行时回 ...

  2. learning scala stripMargin

    (1)Scala中创建多行字符串使用Scala的Multiline String. 在Scala中,利用三个双引号包围多行字符串就可以实现. 代码实例如: val foo = "" ...

  3. 运行级别 runlevel

    linux默认有7个等级,从0到6 0 关机 1 单用户模式,系统出现问题是可使用该模式进入系统.例如完了root密码,就可以使用1进入系统修改root密码 2 多用户模式,没有网络连接 3 完全多用 ...

  4. 关于 js 函数参数的this

    先看一道面试题: var number = 10; function fn() { console.log(this.number); } var obj = { number: 2, show: f ...

  5. 区间连续长度的线段树——洛谷P2894 [USACO08FEB]酒店Hotel

    https://www.luogu.org/problem/P2894 #include<cstdio> #include<iostream> using namespace ...

  6. 1069 The Black Hole of Numbers(20 分)

    For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in ...

  7. lightgbm用于排序

    一. LTR(learning to rank)经常用于搜索排序中,开源工具中比较有名的是微软的ranklib,但是这个好像是单机版的,也有好长时间没有更新了.所以打算想利用lightgbm进行排序, ...

  8. hadoop3.1.1:找不到或无法加载主类 org.apache.hadoop.mapreduce.v2.app.MRAppMaster

    yarn执行MapReduce任务时,找不到主类导致的 解决: 1.在命令行输入:hadoop classpath [hadoop@localhost ~]$ hadoop classpath /da ...

  9. gitlab怎么用

    0101在个人资料里面去设置去找密钥.... 0102 点击生成密钥 0103 在文件夹的命令行输入 ssh-keygen -t rsa -C "your.email@example.com ...

  10. 【2018.07.27】(字符串/找相同)学习KMP算法小记

    虽然说原理很好理解,但是代码理解了花费我一个下午的时间,脑阔痛 该注释的地方都标记了,希望以后看到这些代码我还能好好理解吧 学习的链接地址:https://www.cnblogs.com/teble/ ...