愤怒的牛

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 using namespace std;
 5 const int maxn=1e5+7;
 6 int n,m;
 7 int l,r,mid;
 8 int xx[maxn];
 9 bool check(int u){
10     int cnt=1;int lft=xx[1]+u;
11     for(int i=2;i<=n;i++){
12         if(lft<=xx[i]) {cnt++;lft=xx[i]+u;}
13     }
14     return cnt>=m;
15 }
16 int main(){
17     cin>>n>>m;
18     for(int i=1;i<=n;i++) cin>>xx[i];
19     sort(xx+1,xx+n+1);
20     l=0;r=xx[n]-xx[1];
21     while(l<=r){
22         mid=(l+r)/2;
23         if(check(mid)) l=mid+1;
24         else r=mid-1;
25     }
26     cout<<r<<endl;
27     return 0;
28 }

如果不是l=mid+1,r=mid-1的话,因为这里都是整数,很可能出现l=mid的情况,这是就会无限循环下去

数列分段II

 1 #include<iostream>
 2 #include<cstdio>
 3 using namespace std;
 4 typedef long long ll;
 5 const ll maxn=1e6+7;
 6 ll n,m,l,r,mid;
 7 ll a[maxn];
 8 bool check(ll x){
 9     ll cnt=1;ll lft=a[1];
10     for(ll i=2;i<=n;i++){
11         if(lft+a[i]>x){cnt++;lft=a[i];}
12         else{lft+=a[i];}
13     }
14     return cnt<=m;
15 }
16 int main(){
17     cin>>n>>m;
18     for(ll i=1;i<=n;i++) {cin>>a[i];r+=a[i];l=max(l,a[i]);}
19     while(l<=r){
20         mid=(l+r)/2;
21         if(check(mid)) r=mid-1;
22         else l=mid+1;
23     }
24     cout<<l<<endl;
25     return 0;
26 }

l不能设成0

原文地址:https://www.cnblogs.com/lcan/p/9612425.html

时间: 2024-10-08 14:53:26

愤怒的牛的相关文章

BZOJ 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛( 二分答案 )

最小最大...又是经典的二分答案做法.. -------------------------------------------------------------------------- #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #define rep( i , n ) for( int i = 0 ; i < n ; ++i ) #defin

bzoj 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛

1734: [Usaco2005 feb]Aggressive cows 愤怒的牛 Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,...,xN (0 <= xi <= 1,000,000,000). His C (2 <= C &l

1734: [Usaco2005 feb]Aggressive cows 愤怒的牛

1734: [Usaco2005 feb]Aggressive cows 愤怒的牛 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 217  Solved: 175[Submit][Status][Discuss] Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a st

1433:【例题1】愤怒的牛

1433:[例题1]愤怒的牛 题解 This is an er'fen ti. 由题意可得  它是求最小值最大的问题 我们假设: 每两头牛之间的最小距离为 d ,也就是每两头牛之间的距离 >= d ,问题也就是求这个 d 最大是多少 理一理思路: 1.对所有的牛舍从小到大排序 2.假设我们把第 i 头牛放在 ai 号牛舍里,那么第 i+1 头牛就要放在 ai+d<=ak 的ak 牛舍中,由于可能有很多牛舍满足条件,我们选取距离 ai 最近的一个(这是很显然的),然后依次类推,放牛,  放牛,放

bzoj1734 愤怒的牛

Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,...,xN (0 <= xi <= 1,000,000,000). His C (2 <= C <= N) cows don't like this barn layout an

BZOJ1734: [Usaco2005 feb]Aggressive cows 愤怒的牛 二分查找

Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,...,xN (0 <= xi <= 1,000,000,000). His C (2 <= C <= N) cows don't like this barn layout an

Aggressive cows 愤怒的牛

Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,...,xN (0 <= xi <= 1,000,000,000). His C (2 <= C <= N) cows don't like this barn layout and become agg

一本通网站 1433:【例题1】愤怒的牛

原题 传送门 [题目描述] 农夫 John 建造了一座很长的畜栏,它包括N (2 ≤ N ≤ 100,000)个隔间,这些小隔间依次编号为x1,...,xN (0 ≤ xi ≤ 1,000,000,000). 但是,John的C (2 ≤ C ≤ N)头牛们并不喜欢这种布局,而且几头牛放在一个隔间里,他们就要发生争斗.为了不让牛互相伤害.John决定自己给牛分配隔间,使任意两头牛之间的最小距离尽可能的大,那么,这个最大的最小距离是什么呢 [输入] 第一行:空格分隔的两个整数N和C: 第二行---

BZOJ1734: [Usaco2005 feb]Aggressive cows 愤怒的牛

n<=100000个点在坐标系上,选m个点使点间最小距离最大. 二分模板?? 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #include<algorithm> 5 #include<math.h> 6 //#include<iostream> 7 using namespace std; 8 9 int n,m; 10 #define maxn