欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

二分答案

程序员文章站 2024-03-17 15:38:40
...

很久之前做的很水的二分答案。

 

Aggressive cows  (usaco 2006December sliver)

var
    n,c,ans,i,min,max:longint;
    x,d:array[0..100001] of longint;

procedure qsort(head,tail:longint);
var
    temp,i,j,mid:longint;
begin
    if head>=tail then exit;

    i:=head-1;
    j:=tail+1;
    mid:=x[(head+tail) div 2];
    while i<j do
    begin
        repeat inc(i) until x[i]>=mid;
        repeat dec(j) until x[j]<=mid;
        if i<j then
        begin
            temp:=x[i];
            x[i]:=x[j];
            x[j]:=temp;
        end;
    end;
    qsort(head,j);
    qsort(j+1,tail);
end;


function check(mid:longint):boolean;
var
    a:array[0..100000] of longint;
    i,j:longint;
begin
   j:=1;
   for i:=1 to 100000 do a[i]:=0;
   // fillchar(a,sizeof(a),0);

    for i:=1 to n-1 do
        if  a[j]<mid then
            begin
            a[j]:=a[j]+d[i];
            if a[j]>=mid then inc(j);
            end
            else
            begin
                inc(j);
                a[j]:=a[j]+d[i];
            end;
    if j>c-1 then
        exit(true);
    exit(false);
end;




procedure work(head,tail:longint);
var
    mid:longint;
begin
    if head=tail then
    begin
        if check(head) then
                if mid>ans then ans:=mid;
        exit;
    end;
    mid:=(head+tail) div 2;
    if check(mid) then
    begin
        if mid>ans then ans:=mid;
        if mid+1<=tail then
            work(mid+1,tail);
    end
        else
                work(head,mid);
end;



begin
    read(n,c);
    for i:=1 to n do
        read(x[i]);
    qsort(1,n);


    min:=maxlongint;
    for i:=1 to n-1 do
    begin
        d[i]:=x[i+1]-x[i];
        if d[i]<min then
            min:=d[i];
    end;

    max:=(x[n]-x[1]) div (c-1);

    work(min,max);

    writeln(ans);
end.

 

这是 我做的第一道二分答案的题   二分过程(work) 写的不是很好 ... 

首先是 找出max来   然后程序里min 不太对 其实只要找到一个可能的解赋值给min,然后work(min,max) 就OK了 最后注意每次调用函数前 把J 赋初值。(我就w在这了...)。

 

 

Monthly Expense(usaco2006December sliver)

var
ans,m,n,i,max,min:longint;
a:array[1..100000] of longint;
function check(x:longint):boolean;
var
i,s,q:longint;
begin
    s:=1;
    q:=0;
    for i:=1 to n do
        if q+a[i]<=x then
            q:=q+a[i]
            else
            begin
                q:=a[i];
                inc(s);
            end;
    if s<=m then
        exit(true);
    exit(false);
end;



procedure work(head,tail:longint);
var
mid:longint;
begin
    if head>tail then
        exit;
    mid:=(head+tail) div 2;
    if check(mid) then
    begin
        ans:=mid;
        work(head,mid-1);
    end
    else
        work(mid+1,tail);
end;

begin
    read(n,m);
    for i:=1 to n do
    begin
        read(a[i]);
        if min<a[i] then
            min:=a[i];
    end;

    for i:=1 to n-m+1 do
        max:=max+a[i];
    for i:=3 to n do
        if max<a[i] then
            max:=a[i];
       work(min,max);
     writeln(ans);
end.

 

       比上一个  还简单 不解释..

 

River Hopscotch(usaco 2006 december sliver)

var
a,d:array[1..60000] of longint;
n,m,l,i,max,min,ans:longint;
procedure qsort(head,tail:longint);
var
    mid,temp,i,j:longint;
begin
    if head>=tail then
        exit;
    i:=head-1;
    j:=tail+1;
    mid:=a[(head+tail) div 2];

    while i<j do
    begin
        repeat inc(i) until a[i]>=mid;
        repeat dec(j) until a[j]<=mid;
        if i<j then
            begin
                temp:=a[i];
                a[i]:=a[j];
                a[j]:=temp;
            end;
    end;
    qsort(head,j);
    qsort(j+1,tail);
end;
function check(x:longint):boolean;
var
    q,i,s:longint;

begin
    q:=0;
    s:=0;
    for i:=1 to n+1 do
        begin
        if q<x then
            q:=q+d[i]
            else
            begin
                q:=d[i];
                inc(s);
            end;
         end;

    if q>=x then
        inc(s);


    if n+1-s>m then
        exit(false);
    exit(true);
end;


procedure work(head,tail:longint);
var
    mid:longint;
begin
    if head>tail then
        exit;
    mid:=(head+tail) div 2;
    if check(mid) then
    begin
        ans:=mid;
        work(mid+1,tail);
    end
        else
            work(head,mid-1);
end;



begin
    read(l,n,m);
    a[1]:=0;
    a[n+2]:=l;
    for i:=2 to n+1 do
        read(a[i]);
    qsort(2,n+1);

    max:=l-1;
    min:=maxlongint;
    for i:=1 to n+1 do
    begin
        d[i]:=a[i+1]-a[i];
        if min>d[i] then
            min:=d[i];
    end;

    work(min,max);
     writeln(ans);
end.

 同第一个.

 

 

转载于:https://www.cnblogs.com/xujian9502/archive/2011/10/31/2230159.html