习题:布朗尼切片(二分)
洛谷2017
题目大意:
一个N*M的的棋盘,每个格子里有一定量分值,将棋盘分成A*B份,先横向切割后,对每一段纵向切割,求分数最小的那一份的最大值。
分析:
显然二分。
代码:
program Slicing; var w:array[0..500,0..500]of longint; dx:array[0..500]of longint; n,i,m,a,b,j,l,r,mid,numa,numb,sum,s,ans:longint; begin assign(input,'Slicing.in'); reset(input); assign(output,'Slicing.out'); rewrite(output); readln(n,m,a,b); for i:=1 to n do begin for j:=1 to m do begin read(w[i,j]); s:=s+w[i,j]; end; readln; end; l:=0; r:=s; while l<=r do begin mid:=(l+r) div 2; for i:=1 to m do dx[i]:=0; numa:=0; for i:=1 to n do begin for j:=1 to m do inc(dx[j],w[i,j]); sum:=0; numb:=0; for j:=1 to m do begin sum:=sum+dx[j]; if sum>=mid then begin sum:=0; inc(numb); end; end; if numb>=b then begin for j:=1 to m do dx[j]:=0;inc(numa); end; end; if numa>=a then begin ans:=mid; l:=mid+1; end else r:=mid-1; end; writeln(ans); close(input); close(output); end.