题目链接:点击打开链接
解题思路:
明显要消除连续的m才能使收益最大,我们直接暴力的枚举好了,每个区间的a[i + m ] - a[ i - 1] - 1的最大值即所求。这里在左右边界分别添上0和100
完整代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <climits> using namespace std; int n , m; const int maxn = 100001; const int INF = 1000000000; int a[maxn]; void solve() { int maxx = -INF; for(int i = 1 ; i <= n ; i ++) { if(i + m > n) break; int sum = a[i + m] - a[i - 1] - 1; if(sum > maxx) maxx = sum; } cout << maxx << endl; } int main() { #ifdef DoubleQ freopen("in.txt" , "r" , stdin); #endif // DoubleQ int T; cin >> T; while(T--) { cin >> n >> m; for(int i = 1 ; i <= n ; i ++) cin >> a[i]; if(m >= n) { cout << "100" << endl; continue; } a[0] = 0; a[n+1] = 100; n ++; solve(); } }
时间: 2024-10-31 06:43:42