NC24017 [USACO 2016 Jan S]Angry Cows
题目链接
关键点:
1、要求爆炸半径的最小值,二分答案,每次判断
2、如何判断:先将距离排序(从小到大),然后设最左边(l)为第一个数组(grass[1]),接下来让l+=r*2(加上两个半径),这是最大利用爆炸面积的做法,然后去找在这个l之后的第一个grass,再次爆炸,判断在有限的奶牛数量中,爆炸的最大距离是否会大于最后一个干草堆的坐标
# include <cstdio>
# include <iostream>
# include <algorithm>
using namespace std;
typedef long long ll;
const int N = 50000+10;
int n, k;
int grass[N];
int judge(ll x)
{
ll l = grass[1];
// cout<<l<<endl;
for (int i=1; i<=k; i++)
{
l += x*2;
if (l>=grass[n])
{
return 1;
}
int ans = *upper_bound(grass+1, grass+1+n, l);
l = ans;
// cout<<l<<endl;
}
return 0;
}
int main()
{
cin>>n>>k;
for (int i=1; i<=n; i++)
cin>>grass[i];
sort(grass+1, grass+1+n);
ll l = 0, r = 1000000010;
while (l<=r)
{
ll mid = (l+r)/2;
// cout<<mid<<endl;
if (judge(mid)) r = mid-1;
else l = mid+1;
}
cout<<r+1<<endl;
return 0;
}
NC16462 [NOIP2015]跳石头
题目链接
关键点
1、求最短跳跃距离的最大值,一样的二分答案再验证
2、如何验证,求在该最短跳跃距离下,是否还会出现更短的跳跃距离,如果出现,则将石头搬走,如果搬走石头的次数超出所给出的次数,那么当前的跳跃距离就不行
完整代码
# include <iostream>
# include <cstdio>
using namespace std;
typedef long long ll;
int len, m, n;
int rock[500000+10];
int judge(int x)
{
int start = 0;
int now = 0;
for (int i=1; i<=n; i++)
{
int dis = rock[i]-start;
if (dis<x)
{
now++;
}
else
start = rock[i];
if (now>m)
{
return 0;
}
}
int dis = len-rock[n];
if (dis<x)
now++;
if (now>m)
return 0;
return 1;
}
int main()
{
cin>>len>>n>>m;
for (int i=1; i<=n; i++)
cin>>rock[i];
ll l = 0, r = 1000000000+10;
while (l<=r)
{
ll mid = (l+r)/2;
if (judge(mid)) l = mid+1;
else r = mid-1;
}
cout<<l-1<<endl;
return 0;
}
NC235254 晾衣服
题目链接
关键点
1、一样的二分最短晾衣服的时间
2、判断,如果能在当前二分的时间里使衣服晾干,那么该衣服就不要用烘干机,如果不能晾干,那么就求要占用的机子的时间(注意用ceil向上取整),然后求总共要用吹风机的时间,注意因为在最开始求是否存在自然干的衣服时,已经将所有的衣服都在该分钟内的水量减去了1,所以这是再算吹风机的效果时,再减去k-1即可,如果超出了所给的时间,那么就不行,
3、要注意判断如果吹风机的烘干数(k)为1时,直接输出最长晾干的衣服的水量即可
完整代码
# include <iostream>
# include <cstdio>
# include <cmath>
# include <algorithm>
using namespace std;
typedef long long ll;
ll n, k;
int maxx;
int a[100000+10];
int judge(ll x)
{
ll total = 0;
for (int i=1; i<=n; i++)
{
if (a[i]>x)
total+=(ceil((a[i]-x)*1.0/(k-1)));
}
if (total<=x)
return 1;
else
return 0;
}
int main()
{
scanf("%d", &n);
for (int i=1; i<=n; i++)
{
scanf("%d", &a[i]);
maxx = max(maxx, a[i]);
}
scanf("%d", &k);
if (k==1)
{
cout<<maxx;
return 0;
}
ll l = 0, r = maxx;
while (l<=r)
{
ll mid = (r+l)/2;
if (judge(mid)) r = mid-1;
else l = mid+1;
}
cout<<r+1<<endl;
return 0;
}