SDUT 3916 上色的纱雾
Time Limit: 1000MS Memory Limit: 65536KB
Problem Description
纱雾画画功力首屈一指,现在她准备上色了,这时候她在想,怎样上色的时间最短呢?
在这里我们把问题简化,平面图抽象成坐标轴,需要上色的地方抽象成 n 个点,纱雾有 m 支画笔。
开始的时候纱雾可以选择让画笔落在坐标轴任意一点上,之后每次移动画笔一个单位的距离都会花费 1 秒(画笔可以左右移动,纱雾强大的能力可以同时移动 m 枝画笔,涂色的时间忽略不计)。现在纱雾想要知道这 n 个点全部上完色最少要多少时间呢?
相信你一定能让可爱的纱雾露出这样的表情的:
Input
多组输入。
对于每组数据:
首先输入两个以空格分隔的整数 n, m (1 <= n, m <= 10^5),分别表示需要上色的点以及纱雾拥有的画笔的数量
接下来一行是 n 个以空格分隔的整数(绝对值保证不超过 10^9)
Output
对于每组数据,输出一个整数,表示纱雾需要的最少时间(秒)。
Example Input
3 2
1 4 8
3 1
1 2 9
4 2
1 2 3 6
Example Output
3
8
2
Hint
两支画笔,第一支画笔初始地点选择 1,第二支画笔初始地点选择地点 8。
那么 3s 后第一支画笔可以到达地点 4,这样 3 个点就可以全部涂上了。
Author
「“师创杯”山东理工大学第九届ACM程序设计竞赛 热身赛」UMR
Submit
#include <bits/stdc++.h>
using namespace std;
int N, M;
int n[100005];
int ans;
void binsearch(int low, int high)
{
if(low > high)
return;
int mid = low + ((high-low)/2);
int sum = 0, count = 1;
for(int i = 1; i < N; i++)
{
if(sum + n[i] - n[i-1] <= mid)
{
sum += n[i] - n[i-1];
}
else
{
count++;
sum = 0;
}
}
if(count > M)
binsearch(mid+1, high);
else
{
ans = mid;
binsearch(low, mid-1);
}
}
int main()
{
while(~scanf("%d%d", &N, &M))
{
for(int i = 0; i < N; i++)
scanf("%d", &n[i]);
sort(n, n+N);
int low, high;
high = n[N-1] - n[0];
low = 0;
binsearch(low, high);
printf("%d\n", ans);
}
return 0;
}
二分思路,参考SDUT 2778 小明的花费预算http://blog.csdn.net/yxc9806/article/details/72854030