Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:可以二分中位数,不用堆,只不过需要修改一小点地方

Posted by lydliyudong at 2011-10-15 20:58:31 on Problem 2010 and last updated at 2011-10-15 20:59:51
In Reply To:这题可以二分的么??? Posted by:xxiioo3 at 2011-08-16 17:48:57
虽然直接二分中位数是不满足单调性的。
但是可以假设分数比当前中位数大的牛可以代替分数比中位数小的牛,也就是说从分数比中位数低的牛里取不超过n>>1个,从分数比中位数高或者相等的牛里取>=n div 2个(也就是可以随便选,没有个数上限)。
虽然说当前这样选是不符合题意的,但是只需要把中位数扩大一些,就能达到题目要求。
这样一来二分就具有单调性了,因为随着中位数的减小,“可以任意选”的集合在增大。


type
 arr=array[0..100000]of longint;
var
 a,b,c,d:arr;
 n,m,t,i,l,r,score,now,mid,x,y,k:longint;
procedure qsort(var a,b:arr);
 procedure sort(l,r:longint);
  var
   i,j,x,y:longint;
  begin
   i:=l; j:=r;
   x:=a[(l+r)>>1];
   repeat
    while a[i]<x do inc(i);
    while x<a[j] do dec(j);
    if not(i>j) then
     begin
      y:=a[i]; a[i]:=a[j]; a[j]:=y;
      y:=b[i]; b[i]:=b[j]; b[j]:=y;
      inc(i); dec(j);
     end;
   until i>j;
   if l<j then sort(l,j);
   if i<r then sort(i,r);
  end;
 begin
  sort(1,m);
 end;
begin
 read(n,m,t);
 for i:=1 to m do read(a[i],b[i]);
 c:=a; d:=b;
 qsort(b,a);
 qsort(c,d);
 for i:=1 to n do inc(now,b[i]);
 if now>t then
  begin writeln(-1); halt; end;
 l:=1; r:=m;
 while l<r do
  begin
   mid:=(l+r+1)>>1;
   score:=c[mid];
   now:=d[mid];
   x:=0; y:=0; i:=0; k:=0;
   while (x+y<n-1)and(i<m) do
    begin
     inc(i);
     if (k=0)and(a[i]=score)and(b[i]=d[mid]) then
      begin inc(k); continue; end;
     if (x<n>>1)and(a[i]<score) then
      begin inc(x); inc(now,b[i]); end;
     if a[i]>=score then
      begin inc(y); inc(now,b[i]); end;
    end;
   if (now>t)or(x+y<n-1) then r:=mid-1 else l:=mid;
  end;
 writeln(c[l]);
end.

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator