AYYZOJ p1588
•分析:
二分工资最大值
利用贪心(从左往右能取就取)判断是否可行
要注意判断是否恰好取M次。若取得M次则增加下界,否则减小上界
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 var 2 l,r,mid,n,m,i:longint; 3 a,b:array[1..100000] of longint; 4 function check(p:longint):boolean; 5 var 6 i,pre,tot:longint; 7 begin 8 tot:=m; 9 pre:=0; 10 i:=1; 11 while i<=n do 12 begin 13 if pre+a[i]<=p then 14 begin 15 pre:=pre+a[i]; 16 inc(i); 17 end 18 else 19 begin 20 pre:=0; 21 dec(tot); 22 if tot=0 then exit(false); 23 end; 24 end; 25 exit(true); 26 end; 27 begin 28 readln(n,m); 29 r:=0; 30 for i:=1 to n do 31 begin 32 readln(a[i]); 33 inc(r,a[i]); 34 end; 35 l:=0; 36 while l<r do 37 begin 38 mid:=(l+r)>>1; 39 if check(mid) then r:=mid else l:=mid+1; 40 end; 41 writeln(l); 42 end.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 var 2 n,k,i,p,l,r,m,s:longint; 3 a:array[1..100005] of longint; 4 begin 5 readln(n,k); 6 for i:=1 to n do 7 begin 8 read(a[i]); 9 inc(r,a[i]); 10 end; 11 while r-l>1 do 12 begin 13 m:=(l+r) shr 1; 14 s:=0; p:=0; 15 for i:=1 to n do 16 if s+a[i]<=m then s:=s+a[i] 17 else begin inc(p); s:=a[i]; end; 18 if p+1>k then l:=m else r:=m; 19 end; 20 writeln(r); 21 end.
不懂另一个为什么是65分。。