1044: [haoi2008] Time Limit: 10 sec memory limit: 162 MB
Submit: 1580 solved: 567
[Submit] [Status] Description
There are n wooden sticks. The length of the I wooden sticks is Li, and N wooden sticks are connected in turn. There are n-1 connections in total. now you can cut down a maximum of M connections. After the cut, N wooden sticks are divided into many segments, which requires the minimum length of the maximum segment, how many cutting methods are output to minimize the length of the maximum segment. and the result mod 10007...
Input
The first line of the input file has 2 numbers n, m. The next n rows have a positive integer Li, indicating the length of the I stick.
Output
There are two output numbers. The first number is the minimum length of the segment with the largest total length, and the second number is the number of cutting methods to meet the conditions.
Sample input3 2
1
1
10
Sample output10 2
Two methods: (1) (1) (10) and (1) (10)
Data range
N <= 50000, 0 <= m <= min (n-1, 1000 ).
1 <= LI <= 1000.
Hint Source
Question:
Question 2 + greed can be obtained
In the second simple statement, the worst case is O (Mn ^ 2), but considering that each contact has pre [I], indicates that the maximum J value of S [I]-s [J] <= ANS is met.
Having f [I, j] = sigma (F [K, J-1]) (pre [I] <= k <= i-1), considering that pre [I] is a single addition, so we can use monotonous queues for optimization to O (Mn), but we can also skip queues,
Only one temporary variable is used to record the weights and
Let's talk about the following:
1. It is hard to write this question.
2. When writing a program, it should be easy to understand and error-prone, but should be short and beautiful. Don't think about how to reduce the code as soon as you come up, or you will get dizzy with yourself.
Code:
1 const maxn=50000+100;p=10007; 2 var i,j,n,m,x,ans,cnt,l,r,mid,t,sum:longint; 3 q,pre,len,s:array[0..maxn] of longint; 4 f:array[0..maxn,0..1] of longint; 5 procedure init; 6 begin 7 readln(n,m); 8 for i:=1 to n do begin readln(len[i]);s[i]:=s[i-1]+len[i];end; 9 s[n+1]:=maxlongint;10 end;11 function test(x:longint):boolean;12 var i,j,sum:longint;13 begin14 sum:=0;j:=0;15 for i:=1 to n do16 if sum+len[i]<=x then inc(sum,len[i])17 else begin inc(j);sum:=len[i];end;18 exit(j<=m);19 end;20 procedure main;21 begin22 l:=0;r:=1000000000;23 while l<r do24 begin25 mid:=(l+r)>>1;26 if test(mid) then r:=mid else l:=mid+1;27 end;28 ans:=l;29 j:=0;30 for i:=1 to n do31 begin32 while s[i]-s[j]>ans do inc(j);33 pre[i]:=j;34 end;35 fillchar(f,sizeof(f),0);36 i:=1;while s[i]<=ans do begin f[i,0]:=1;inc(i);end;37 t:=0;cnt:=0;38 for i:=1 to m do39 begin40 t:=1-t;41 l:=0;r:=0;sum:=0;42 for j:=1 to n do43 begin44 while (l<r) and (q[l]<pre[j]) do begin sum:=(sum-f[q[l],1-t]+p) mod p;inc(l);end;45 f[j,t]:=sum;46 inc(r);q[r]:=j;sum:=(sum+f[q[r],1-t]) mod p;47 end;48 cnt:=(cnt+f[n,t]) mod p;49 end;50 writeln(ans,‘ ‘,cnt);51 end;52 begin53 assign(input,‘input.txt‘);assign(output,‘output.txt‘);54 reset(input);rewrite(output);55 init;56 main;57 close(input);close(output);58 end.
View code