Test instructions: Given n number, it is required to change one of the number to p, the maximum interval and the number of changes can be.
Water problem. Enumerate each interval, if the interval is not modified (that is, modify the number outside the interval), then for that interval and, if the interval to be modified, because it must be modified, so it is necessary to change the minimum number to P can guarantee the last and largest interval, so compare the larger of the two schemes. For each interval removed the larger, then take the total maximum. Note that a trick, when enumerated to the entire interval, is necessary to modify a number, so this largest interval has only one scheme. The interval of the 1~i is preprocessed and the minimum and interval of each interval are maintained.
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <string > #include <algorithm> #include <stack> #include <queue> #include <vector> #include <map > #include <set>using namespace std;const int MAX = 1005;const int INF = 1e9;int N;__int64 P, A[max];__int64 sum[m AX], smallest[max][max];void input () {scanf ("%d%i64d", &n, &p); for (int i = 1; I <= n; i++) scanf ("%i64d", &a[i]);} void Solve () {smallest[0][0] = INF; Sum[0] = 0; __int64 ans = P; for (int i = 1; I <= n; i++) sum[i] = Sum[i-1] + a[i]; for (int i = 1; I <= n; i++) {smallest[i][i] = A[i]; ans = max (ans, a[i]); for (int j = i + 1; j <= N; j + +) {Smallest[i][j] = min (smallest[i][j-1], a[j]); ans = max (ans, sum[j]-sum[i-1]-smallest[i][j] + P); if (i! = 1 | | J! = n) ans = max (ans, sum[j]-sum[i-1]); }} printf ("%i64d\n", ans);} int main () {int T; scanf ("%d", &t); while (t--) {input (); Solve (); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 5280 Senior ' s Array maximum range and