• POJ 2010


    Moo University - Financial Aid
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 4235   Accepted: 1293

    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

     
    两种方法
    第一是二分答案,首先把生成两个分别按score排序和按aid排序的数组,定义left为中位数左边可加的数的个数,right为右边可加的数的个数
    若left < n / 2则中位数只能调高,若right < n / 2 中位数只能调低,否则满足条件则应把中位数调高,按此规则二分即可
     
      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <algorithm>
      5 
      6 using namespace std;
      7 
      8 #define maxn 100005
      9 
     10 struct node {
     11         int id,aid,sco;
     12 };
     13 
     14 int n,c,f;
     15 node calve[maxn],s[maxn];
     16 int sma[maxn],big[maxn];
     17 
     18 bool cmp1(node a,node b) {
     19         return a.aid < b.aid;
     20 }
     21 
     22 bool cmp2(node a,node b) {
     23         return a.sco < b.sco;
     24 }
     25 
     26 int check(int x) {
     27     int sum = s[x].aid,left = 0,right = 0;
     28 
     29     for(int i = 1; i <= c; ++i) {
     30             if(calve[i].id < x && sum + calve[i].aid <= f && left < n / 2) {
     31                     sum += calve[i].aid;
     32                     ++left;
     33             } else if(calve[i].id > x && sum + calve[i].aid <= f && right < n / 2) {
     34                     sum += calve[i].aid;
     35                     ++right;
     36             }
     37     }
     38 
     39     if(left < n /2 ) return 2;
     40     if(right < n / 2) return 1;
     41     return 0;
     42 
     43 
     44 
     45 
     46 
     47 }
     48 
     49 void solve() {
     50         int l = 1,r = c;
     51 
     52         //for(int i = 1; i <= c; ++i) printf("%d ",s[i].sco);
     53         while(l < r) {
     54                 int mid = (l + r + 1) >> 1;
     55                 if(check(mid) == 1) {
     56                         r = mid - 1;
     57                 } else if(check(mid) == 2) {
     58                         l = mid + 1;
     59                 } else {
     60                         l = mid;
     61                 }
     62         }
     63 
     64 
     65 
     66         printf("%d
    ",s[l].sco);
     67 
     68 }
     69 
     70 int main()
     71 {
     72 
     73    // freopen("sw.in","r",stdin);
     74 
     75     scanf("%d%d%d",&n,&c,&f);
     76 
     77     for(int i = 1; i <= c; ++i) {
     78             scanf("%d%d",&s[i].sco,&s[i].aid);
     79     }
     80 
     81     sort(s + 1,s + c + 1,cmp2);
     82 
     83     for(int i = 1; i <= c; ++i) {
     84             calve[i].id = s[i].id = i;
     85             calve[i].sco = s[i].sco;
     86             calve[i].aid = s[i].aid;
     87     }
     88 
     89 
     90 
     91     sort(calve + 1,calve + 1 + c,cmp1);
     92 
     93     int sum = 0;
     94     for(int i = 1; i <= n; ++i) {
     95             sum += calve[i].aid;
     96     }
     97 
     98     if(sum > f) {
     99             printf("-1
    ");
    100     } else {
    101             solve();
    102     }
    103 
    104     return 0;
    105 }
    View Code

    第二种方法是用大根堆,首先按照score从小到大排序。用大根堆预处理数组dpl[i]代表从1  到 i 能得到的数量为 n / 2的最小的aid ,dpr[i]代表 从i 到 c能得到的数量为 n / 2的最小的aid,具体方法是,若堆的大小小于 n / 2,则不断入列,否则进列,并删除堆顶。

    则从大到小找出能满足条件的最大的中位数。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <queue>
     6 
     7 using namespace std;
     8 
     9 #define maxn 100005
    10 
    11 struct node {
    12         int sco,aid;
    13 };
    14 
    15 int n,c,f;
    16 node s[maxn];
    17 int dpl[maxn],dpr[maxn];
    18 
    19 bool cmp(node a,node b) {
    20         return a.sco < b.sco;
    21 }
    22 
    23 bool cmp2(node a,node b) {
    24         return a.aid < b.aid;
    25 }
    26 
    27 void solve() {
    28         priority_queue<int> q;
    29         int sum = 0;
    30         for(int i = 1; i <= c; ++i) {
    31                 if(q.size() < (n / 2)) {
    32                         sum += s[i].aid;
    33                         q.push(s[i].aid);
    34                 } else {
    35                         q.push(s[i].aid);
    36                         sum += s[i].aid;
    37                         sum -= q.top();
    38                         q.pop();
    39 
    40                 }
    41                 dpl[i] = sum;
    42         }
    43 
    44         while(!q.empty()) q.pop();
    45         sum = 0;
    46         for(int i = c; i >= 1; --i) {
    47                 if(q.size() < (n / 2)) {
    48                         sum += s[i].aid;
    49                         q.push(s[i].aid);
    50                 } else {
    51                         q.push(s[i].aid);
    52                         sum += s[i].aid;
    53                         sum -= q.top();
    54                         q.pop();
    55 
    56                 }
    57                 dpr[i] = sum;
    58         }
    59 
    60 
    61         int ans;
    62         for(int i = c; i >= 1; --i) {
    63                 if(i - 1 < n / 2 || c - i < n / 2) continue;
    64                 if(dpl[i - 1] + dpr[i + 1] + s[i].aid <= f) {
    65                         ans = s[i].sco;
    66                         break;
    67                 }
    68         }
    69 
    70         printf("%d
    ",ans);
    71 
    72 }
    73 
    74 
    75 int main() {
    76         //freopen("sw.in","r",stdin);
    77 
    78         scanf("%d%d%d",&n,&c,&f);
    79 
    80         for(int i = 1; i <= c; ++i) {
    81                 scanf("%d%d",&s[i].sco,&s[i].aid);
    82         }
    83 
    84         sort(s + 1,s + c + 1,cmp2);
    85         int sum = 0;
    86         for(int i = 1; i <= n; ++i) sum += s[i].aid;
    87         if(sum > f) {
    88                 printf("-1
    ");
    89                 return 0;
    90         }
    91 
    92         sort(s + 1,s + c + 1,cmp);
    93 
    94         solve();
    95 
    96         return 0;
    97 
    98 
    99 }
    View Code
  • 相关阅读:
    2018年强化学习领域十篇重要论文(附源码)[转]
    【转载】 再励学习面试真题 (强化学习面试真题)
    枸杞常泡水喝好吗?
    亲历亚马逊、华为机器学习面试,原来考官想听到这些回答[转]
    阿里面试 深度学习[转]
    强化学习(四)用蒙特卡罗法(MC)求解
    初识kaggle,以及记录 kaggle的使用
    强化学习(三)用动态规划(DP)求解
    CORTEX-M3中断的现场保护问题
    地球是圆的怎么还分东西方
  • 原文地址:https://www.cnblogs.com/hyxsolitude/p/3617276.html
Copyright © 2020-2023  润新知