POJ 2010

题目地址:http://poj.org/problem?id=2010

题目大意

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

思路

这题以前用优先队列做过(【优先队列】POJ 2010),现在刷到二分搜索系列,可以用二分搜索解决。

刚开始用C(x)来表示x是否能作为中位数。但是在判定过程中还是用到最大堆(:qゝ∠)

效率低下,看了一下题解,发现要分四种情况。

于是可以这样二分:

  1. 将奶牛成绩从小到大排序存放
  2. 将奶牛补助从小到大排序存放
  3. 二分搜索mid左右两个区间,统计符合要求的左右区间数量。

具体实现看代码吧。

AC代码

/*************************************************************************
    > File Name: 2010_2.cpp
      > Author: Netcan
      > Blog: http://www.netcan666.com
      > Mail: 1469709759@qq.com
      > Created Time: 2015-12-27 日 19:48:05 CST
 ************************************************************************/

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
int N, C, F;
int Median;
struct Cows {
    int score, aid, rank_by_score;
} cows_rank_by_score[100005], cows_rank_by_aid[100005];

bool by_score(const Cows &a, const Cows &b) {
    return a.score < b.score;
}

bool by_aid(const Cows &a, const Cows &b) {
    return a.aid < b.aid;
}

void solve() {
    Median = N >> 1;
    sort(cows_rank_by_score, cows_rank_by_score+C, by_score);
    for(int i=0; i<C; ++i) cows_rank_by_score[i].rank_by_score = i;
    memcpy(cows_rank_by_aid, cows_rank_by_score, sizeof(Cows)*C);
    sort(cows_rank_by_aid, cows_rank_by_aid+C, by_aid);
    // for (int i = 0; i < C; ++i)
        // printf("score: %d aid: %d id:%d\n", cows_rank_by_score[i].score, cows_rank_by_score[i].aid, cows_rank_by_score[i].rank_by_score);
    // printf("=====\n");
    // for (int i = 0; i < C; ++i)
        // printf("score: %d aid: %d\n", cows_rank_by_aid[i].score, cows_rank_by_aid[i].aid);
    int ans;
    int lb = 0, ub = C; // [lb, ub)
    // printf("Median: %d\n", Median);
    while(ub - lb > 1) {
        int left = 0, right = 0;
        int mid = (lb + ub) >> 1;
        int fee = cows_rank_by_score[mid].aid;
        // printf("lb:%d ub:%d\n", printf(i;0= 0,ntf(tuonst Cod]. pr/c      // pr0+ ub   /t举", p>&f(() {
 r (inan: %d\mp;a, p;a,  ( 1;
+by_aid[i].score, cows_ra r (i= F\mp;a, p;a,  y_aid[i].score, cowsank_by_score[ir (ins_rst Cod]边", p>;;;;++) {
;", p>;;;; 1;
+=by_aid[i].score, cows_ra;", p>}", p>else;&f((  int r (inan: %d\mp;a, p;a,  ( 1;
+by_aid[i].score, cows_ra r (i= F\mp;a, p;a,  y_aid[i].score, cowsank_by_score[irg(ins_rst Cod]边", p>;;;;++  int;", p>;;;; 1;
+=by_aid[i].score, cows_ra;", p>}", p}_score[mid].aid;
        / {
 pri  intf("lb:%d  {
    int\n& ub) >&f() {
 r (inan: %dmp;a, p;a,    int r (inan: %d\m{", p>].a
  -1;", p>binck;", p}_score[mielse;&f() {
 r (inan: %d\mmmmmnt anmra;", pelse;&f(  int r (inan: %d\mut anmra;", pelse;{", p>].a
        int fee = cows_ran a.scorquot;, p>nt anmra;", p}_scor}_scoraid;
        ("lb:%d].a)a.aidht = aind;
}
#ifdef Ojre+C, rernal       > Fiiuot;lb:%dot;lb:rot;lb:%d;
#id\n&#endifsizeof: h        (&(&(&ot;lb:%do;a, N%do;a, C%do;a, Fcore+C, byot;);
    // for (int i = 0; i < f: h        (&(&ot;lb:%do;a,  id:%d\n", cows_rank_by_o;a,  id:%d\n", cows_r_rank_by_a b.aid;;t Cows &0a.ai始用<POJ 2010

题目地址:http://poj366210roblem?id=2010

题Farm noJohn想 pr电话公司修低电缆连接/a>)农场助已知$N$电线杆编号示$1, 2, \cdots N助1号已经连接电话公司N号示农场有$P$对电线杆<连接分四种ol> 出$P$对电线杆距离$A_i, B_I, L_i$e>C(x)$A_i$和$B_i$<连接得分$s长度示$L_i$tcan缆分四种o话公司赞中长度分四种出二r/c 求此中位数。

dfs始用<加-2010"

TLE始用<了刁看code类问跸得系列了分四种设

刚开始用C(x)连接1号到N号c 缆rg(ix长度有多少紁用cod判断免r/助当第低䪷免r/助示$「绁分四种这里怎么出rg(ix有多少 脑洞有点迦右cc"思用则示0跑出tcac"怩示数助和用AC代码

/*************************************************************************
    3662 File Name: 2010_2.cpp
      > Author: Netcan
      > Blog: http://www.netcan666.com
      > Mail: 1469709759@qq.com
      > Created8  10:31:46-27 日 19:48:05 CST
 ************************************************************************/

#include <iostream>
#include <cstdio&gvectorclude <cstdio>
#include <queue>
#include <cstring>
using namespP, KC, F;
int edget Cod]顶点性d);
    ito%d  ngth;t Cowedge(   ito%dt; 1) ngth):ito(to)%d  ngth() ngth) {}_scorlt; brnaratorce&g;
}

boedgetp;a, con
}

bo{", ps &) ngth759@qqb.  ngth;t Cow}
}n& vectorce&gedge59@qqGcowsank_t; 1dcowsank_lt; bdijk F;a_C(   is%dt; 1xst Cod].出长度rg(ixtca少数rg(ix边权示1助>则示0徭位c")助断免r/助列-数出第低䪷免r/tcax助/co$「tca小tca大x_scoraid#incy_t;
#ine&gedge59@qqt;
ank_by_scset(d, 0x3fws_rank_bd);;t Cowd[s] left = 0,t;
.push(edge(s%d0)\n", Med!t;
.empty()\m{", pedgetp let;
.topd;;,t;
.popd;; ub) >>v lep.to; ub) >&_bd[v]or (inp.) ngth) div inuen", printunsign   f(i;0= 0,ntfG[v]._rand;;,uonst ", p>edget;
  G[v]ws_orquot;, p>t; 1) n
  e.) ngth759@qqxorquot;, p>t_bd[
.to]759@qqd[v]o+1) nst ", p> Cowd[
.to]7=qd[v]o+1) n;", p>;;;;t;
.push(edge(
.to,wd[
.to]));", p>}", p}_scor}t Cows &d[N] r (i= Ka.aidd < b.aid;
}

voidijk F;a_C(1, 1nk_by_ai_bd[N] == 0x3f3f3f3fst ", paid;
        -1intf("==== Cows &_scor}t Cow   int an-1;
    iows_r00+5t lb ( 0, ub = C; ;, Median);
    while(ub - lb > 1 0;
        int mid = (lb + ub) >&_bdijk F;a_C(1, s_rs\mut anmra; lb ( 0, ub]", pelse;nt anmra;"}_scoraid;
        ("lb:%d%d\n&aidht = aind;
}
#ifdef Ojre+C, rernal       3662iiuot;lb:%dot;lb:rot;lb:%d;
#id\n&#endifsizeof: h        (&(&(&ot;lb:%do;a, N%do;a, P%do;a, Knk_by_aid[i], 0, lore+C, byot;);
    // for (inP;,uonst ", pf: h        (&(&(&ot;lb:%do;a, a%do;a, b%do;a, lot;==== CowG[a].push_back(edge(0, l)ot;==== CowG[b].push_back(edge(a, l)ot;====}_by_a b.aid;;t Cows &0a.ai始用<li"> 订; bcontaintoc href_containtoc-item toc-level-2div id="nav-toc-l" clad="POJ-2010"> |  Hi  |  Hitsa; bcontaintoc-c, Md href_containtoc-item toc-level-3div id="nav-toc-l" clad="POJ-derlink" title=span> |  Hi  |  Hitsa区ref_containtoc-item toc-level-3div id="nav-toc-l" clad="POJ-derlink" span> |  Hi  |  Hitsa区ref_containtoc-item toc-level-3div id="nav-toc-l" clad="POJ-derlink" tispan> |  Hi  |  Hitsa区r数区ref_containtoc-item toc-level-2div id="nav-toc-l" clad="POJ-20103662 |  Hi  |  Hitsa; bcontaintoc-c, Md href_containtoc-item toc-level-3div id="nav-toc-l" clad="POJ-derlink" tit-1id=span> |  Hi  |  Hitsa区ref_containtoc-item toc-level-3div id="nav-toc-l" clad="POJ-derlin-1id=span> |  Hi  |  Hitsa区ref_containtoc-item toc-level-3div id="nav-toc-l" clad="POJ-derlink"-1id=span> |  Hi  |  Hitsa区r数区r数订阅
本文标:ss="post-title">【二分搜索】最小匌ref>本文字数1,137匌ref>本文作者 >匌ref>发布时d左; Cr年12月27ime:- 21时36st-匌ref>最后更新; C6年09月24ime:- 22时18st-匌ref>原始链接p>题目地址:t; Blog: http://w的值/" href="/2015/12/27/【二分搜索】最小" target=t; Blog: http://w的值/" href="/2015/12/27/【二分搜索】最小tsa区ref>版权声明p>ef="/atom.xml">ccom .0/ga clo langfa-r题目地址:ccom .0/a clo llank"licens/pacy-nc-sa/3fontn/lass="hea中国陆 (CC BY-NC-SA 3fo CN)