普吉岛那有问题, 求高人指点
原题链接:49.跳石头2026-08-28 20:22:52
发布于:陕西
#include<bits/stdc++.h>
using namespace std;
const int maxn = 114514;
int a[maxn], b[maxn], n, len, m;
int check(int mid){
int cnt = 0;
int i = 0, now = 0;
while(i < n+1){
i++;
if(a[i] - a[now] < mid){
cnt++;
}else{
now = i;
}
}
if(cnt <= m) return 1;
return 0;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> len >> n >> m;
for(int i = 1; i <= n; i++){
cin >> a[i];
a[0] = 0, a[n+1] = len;
if(n == 0){
cout << len;
return 0;
}
}
long long l = 1, r = len;
while(l < r){
int mid = (l+r+1)>>1;
if(check(mid)) l = mid;
else r = mid - 1;
}
cout << l;
return 0;
}
全部评论 2
#include<bits/stdc++.h> using namespace std; const int maxn = 114514; int a[maxn], n, len, m; bool check(int mid) { int cnt = 0; int now = 0; for(int i = 1; i <= n; ++i) { if(a[i] - a[now] < mid) { cnt++; } else { now = i; } } return cnt <= m; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> len >> n >> m; if(n == 0) { cout << len << endl; return 0; } for(int i = 1; i <= n; i++) { cin >> a[i]; } sort(a + 1, a + 1 + n); a[0] = 0; a[n+1] = len; int l = 1, r = len; while(l < r) { int mid = (l + r + 1) >> 1; if(check(mid)) l = mid; else r = mid - 1; } cout << l << endl; return 0; }我看着像跳石头(A49),就看了一下提交记录,发现是这道题,帮你改了,能A
2026-08-29 来自 广东
1太棒了,谢谢您!
2026-08-29 来自 陕西
0是何思路呢

2026-08-29 来自 陕西
0
先说一下是哪一题
2026-08-29 来自 广东
1


























有帮助,赞一个