苹果消消乐(尺取法)

有L个苹果和香蕉排成一条直线,其中有N个香蕉,你可以使用至多M次魔法道具将香蕉变成苹果,最后“最长的连续苹果数量”即为你本次苹果消消乐的得分。
给定苹果和香蕉的排列,求你能获得的最大得分。

输入
第一行是3个整数N、M和L,表示香蕉的数量,魔法道具使用次数,以及苹果和香蕉的总数。
第二行包含N个整数a1, a2, … aN(1 <= a1 < a2 < … < aN<= L),表示第a1, a2, … aN个位置上摆放的是香蕉,其余位置摆放的都是苹果。

输出
输出通过使用魔法道具后你能获得的最大得分。
样例输入

5 2 100
10 30 55 56 90

样例输出
59

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 1e6 + 10;
int n,m,l,ans;
int mp[maxn];
signed main(){
    ios::sync_with_stdio(0);
    cin >> n >> m >> l;
    for(int i = 1; i <= n; i++)
        cin >> mp[i];

    mp[0] = 0;
    mp[n + 1] = l + 1;
    int l = 1,r = m;
    while(r <= n){
        ans = max(ans,mp[r + 1] - mp[l - 1] + 1 - 2);
        l++;
        r++;
    }
    cout << ans << endl;
    return 0;
}
原文地址:https://www.cnblogs.com/xcfxcf/p/12301594.html