3 solutions

  • 1
    @ 2022-12-18 21:25:19

    这个题第二个数据是个坑,但是给我提了个醒

    我们定义,l,r的时候可以比右边界多一,因为l<r,r是取不到的,而第二个数据的答案就是L,我一开始定义的r=L,结果答案总是差1,所以我把r=L+1,一开就过了.

    #include<iostream>
    using namespace std;
    const int N1 = 1e5 + 5;
    int L, N, M;
    int a[N1];
    bool check(int x) {
    	int cnt = 0, now = 0;//now表示当前位置
    	for (int i = 1; i <= N; ++i) {
    		if (a[i] - now < x)cnt++;
    		else now = a[i];
    	}
    	return cnt <= M;
    }
    int main() {
    	scanf("%d%d%d", &L, &N, &M);
    	for (int i = 1; i <= N; ++i)scanf("%d", &a[i]);
    	int l = 0, r = L + 1, mid;//这里的r不能开成L,只能是L+1
    	while (l < r) {
    		mid = (l + r) >> 1;
    		if (check(mid)) l = mid + 1;
    		else r = mid;
    	}
    	printf("%d\n", r - 1);
    	return 0;
    }
    

    Information

    ID
    252
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    4
    Tags
    # Submissions
    227
    Accepted
    45
    Uploaded By