懵了!求大佬帮助

P1676 [USACO05FEB] Aggressive cows G

@[ltzx2022_kanxinyi_5](/user/892571) 这道题和进击的奶牛一样,二分就可过,代码你看看 #include<iostream> #include<iomanip> #include<math.h> #include<algorithm> #include<string> #include<stdio.h> using namespace std; int read()//快读 { int ans=0,f=1; char x; x=getchar(); while(x<'0'||x>'9') { if(x=='-') { f=-1; } x=getchar(); } while(x>='0'&&x<='9') { ans=ans*10+x-'0'; x=getchar(); } return ans*f; } int cow[100005]; int n,m,l,r,mid; bool cha(int x)//查 { long long tot=1,now=1; for(int i=1;i<=n;i++) { int dis=cow[i]-cow[now]; if(dis>=x) { now=i; tot++; } } if(tot<m) { return true; } return false; } int main() { n=read(); m=read(); int ans=0; for(int i=1;i<=n;i++) { cow[i]=read(); } sort(cow+1,cow+n+1); l=1; r=cow[n]; mid=(l+r)/2; while(l<=r) { if(cha(mid)) { r=mid-1; } else { l=mid+1; ans=mid; } mid=(l+r)/2; } cout<<ans; } ```
by wbhqm @ 2023-08-03 17:37:09


@[wbhqm](/user/728909) 希望更丰富的展现?使用 [Markdown](https://help.luogu.com.cn/rules/academic/handbook/markdown)、[KaTeX](https://help.luogu.com.cn/rules/academic/handbook/latex)。
by 44i11 @ 2023-10-11 20:07:11


|