bzoj 4590 自动刷题机

二分答案.

  • 不难发现 $n$ 增大,切题数不会增多, $n$ 减小,切题数不会减少.
  • 于是分别二分 $n$ 的最小值与最大值,检验直接模拟操作就好了.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
inline int read()
{
int out=0,fh=1;
char jp=getchar();
while ((jp>'9'||jp<'0')&&jp!='-')
jp=getchar();
if (jp=='-')
fh=-1,jp=getchar();
while (jp>='0'&&jp<='9')
out=out*10+jp-'0',jp=getchar();
return out*fh;
}
const int MAXN=1e5+10;
int n,k,x[MAXN];
bool checkmin(ll mid)
{
int tot=0;
ll len=0;
for(int i=1;i<=n;++i)
{
len+=x[i];
len=max(len,0LL);
if(len>=mid)
len=0,++tot;
}
if(len>=mid)
len=0,++tot;
return tot<=k;
}
ll solvemin()
{
ll L=1,R=1e18;
ll ans=-1;
while(L<=R)
{
ll mid=(L+R)>>1;
if(checkmin(mid))
ans=mid,R=mid-1;
else
L=mid+1;
}
return ans;
}
bool checkmax(ll mid)
{
int tot=0;
ll len=0;
for(int i=1;i<=n;++i)
{
len+=x[i];
len=max(len,0LL);
if(len>=mid)
len=0,++tot;
}
if(len>=mid)
len=0,++tot;
return tot>=k;
}
ll solvemax()
{
ll L=1,R=1e18;
ll ans=-1;
while(L<=R)
{
ll mid=(L+R)>>1;
if(checkmax(mid))
ans=mid,L=mid+1;
else
R=mid-1;
}
return ans;
}
int main()
{
n=read(),k=read();
for(int i=1;i<=n;++i)
x[i]=read();
ll a=solvemin(),b=solvemax();
if(a>b || a<0 || b<0)
puts("-1");
else
cout<<a<<' '<<b<<endl;
return 0;
}