题解:P15823 [JOI Open 2013] 观影 / Watching
二分,dp。
题意
在一个数轴上,在
思路
首先二分一个
所以设 bool 动态转移数组为
对于一个状态
判断
::::success[代码]
#include<iostream>
#include<algorithm>
#define int long long
using namespace std;
const int N=2010,INF=1e9;
int n,p,q,a[N],f[N][N];
bool check(int w)
{
for(int i=0; i<=p; ++i)for(int k=1; k<=n; ++k)f[i][k]=INF;
for(int k=1,s,t; k<=n; ++k)
{
s=upper_bound(a+1,a+n+1,a[k]-w)-a-1;
t=upper_bound(a+1,a+n+1,a[k]-2*w)-a-1;
f[0][k]=f[0][t]+1;
for(int i=1; i<=p; ++i)f[i][k]=min(f[i-1][s],f[i][t]+1);
}
return f[p][n]<=q;
}
int binary_search()
{
if(p+q>=n)return 1;
int l=0,r=INF+1,mid;
while(l+1<r)
{
mid=l+(r-l)/2;
if(check(mid))r=mid;
else l=mid;
}
return r;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin>>n>>p>>q;
for(int i=1; i<=n; ++i)cin>>a[i];
sort(a+1,a+n+1);
cout<<binary_search()<<'\n';
return 0;
}
::::