hdu 5280 Senior's Array

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=5280

Senior's Array

Description

One day, Xuejiejie gets an array $A$. Among all non-empty intervals of $A$, she wants to find the most beautiful one. She defines the beauty as the sum of the interval. The beauty of the interval---$[L,R]$ is calculated by this formula : beauty$(L,R) = A[L]+A[L+1]+……+A[R]$. The most beautiful interval is the one with maximum beauty.

But as is known to all, Xuejiejie is used to pursuing perfection. She wants to get a more beautiful interval. So she asks Mini-Sun for help. Mini-Sun is a magician, but he is busy reviewing calculus. So he tells Xuejiejie that he can just help her change one value of the element of $A$ to $P$ . Xuejiejie plans to come to see him in tomorrow morning.

Unluckily, Xuejiejie oversleeps. Now up to you to help her make the decision which one should be changed(You must change one element).

Input

In the first line there is an integer $T$, indicates the number of test cases.

In each case, the first line contains two integers $n$ and $P$. $n$ means the number of elements of the array. $P$ means the value Mini-Sun can change to.

The next line contains the original array.

$1\leq n\leq 1000$, $-10^9\leq A[i], P\leq 10^9$。

Output

For each test case, output one integer which means the most beautiful interval's beauty after your change.

2
3 5
1 -1 2
3 -2
1 -1 2

Sample Output

8
2

数据才1000直接暴力。。

 #include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<map>
using std::max;
using std::cin;
using std::cout;
using std::endl;
using std::find;
using std::sort;
using std::map;
using std::pair;
using std::vector;
using std::multimap;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) decltype((c).begin())
#define cls(arr,val) memset(arr,val,sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for (int i = 1; i <= (int)(n); i++)
#define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = ;
const int INF = 0x7fffffff;
typedef long long ll;
ll p, ans, arr[N], sum[N];
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int t, n;
scanf("%d", &t);
while (t--) {
ans = -INF;
scanf("%d %lld", &n, &p);
rep(i, n) scanf("%lld", &arr[i]);
rep(i, n) {
ll tmp = arr[i]; arr[i] = p;
rep(j, n) {
sum[j] = sum[j - ] > ? sum[j - ] + arr[j] : arr[j];
ans = max(ans, sum[j]);
}
arr[i] = tmp;
}
printf("%lld\n", ans);
}
return ;
}
上一篇:编写高质量代码改善C#程序的157个建议[协变和逆变]


下一篇:一步步带你做vue后台管理框架