P1824 进击的奶牛 二分做法O(log x[n]) WA#1
查看原帖
P1824 进击的奶牛 二分做法O(log x[n]) WA#1
550974
LucaZSC楼主2022/11/6 11:08

RT\mathrm{RT}

二分

O(log2x[n])\mathrm{O}(\log_{2}{x[n]})

         &\space\space\space\space\space\space\space\space\space\&

     W ⁣A\space\space\space\space\space\mathrm{W\!A} #11

代码:

#include <unordered_map>//本人火车头
#include <unordered_set>
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
#define int long long
using namespace std;
int l=0,r,mid,n,c,x[100005],ans;
bool check(int mid){//check mid是否合理
	int t=1,last_cow=x[1];
	for(int i=2;i<=n;i++){
		if(x[i]-last_cow>=mid){
			last_cow=x[i];
			t++;
		}
	}
	return t>=c;
}
int read(){//快读
	int num=0,f=1;
	char c=getchar();	 	
	while((c<'0'||c>'9')&&c!='-')c=getchar();
	if(c=='-')f=-1;
	else num=c-'0';
	c=getchar();
	while(c>='0'&&c<='9'){
		num=num*10+(c-'0');
		c=getchar();
	}
	return f*num;
}
signed main(){
	n=read();
	c=read();
	for(int i=1;i<=n;i++){
		x[i]=read();
	}
	r=x[n];
	while(l<=r){//二分每头牛之间的距离
		mid=l+((r-l)>>1);
		if(check(mid)){
			l=mid+1;
			ans=mid;
		}
		else{
			r=mid-1;
		}
	}
	printf("%lld",ans); 
	return 0;
}

疑惑:

样例:

5 3
1
2
8
4
9

结果:

1

除给的样例外都过了······

为什么输出 11

怎么改 ?

2022/11/6 11:08
加载中...