一条河里有一串石头,给出石头间的间距,让你去掉m个石头,使最短间距最大。
二分答案,对于每一种mid,判断要不要删除这块石头。然后逼近答案。
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; int dist[50010],save[50010],L,N,M; int main() { while(~scanf("%d%d%d",&L,&N,&M)) { int low = L,high = L; for(int i=0;i<N;i++) { scanf("%d",&dist[i]); low = min(low,dist[i]-dist[i-1]); } dist[N] = 0; dist[N+1] = L; N+=2; sort(dist,dist+N); int mid = (low+high)>>1; while(low <= high) { int rmv = 0,sum=0; for(int i=1;i<N;i++) { if(sum+dist[i]-dist[i-1] <= mid) { rmv++; sum+=dist[i]-dist[i-1]; } else { sum=0; } } if(rmv > M) high = mid-1; else low = mid+1; mid = (low+high)>>1; } printf("%d\n",low); } }
时间: 2024-10-22 20:21:21