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直接暴力。。

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<vector>
 7 #include<map>
 8 using std::max;
 9 using std::cin;
10 using std::cout;
11 using std::endl;
12 using std::find;
13 using std::sort;
14 using std::map;
15 using std::pair;
16 using std::vector;
17 using std::multimap;
18 #define pb(e) push_back(e)
19 #define sz(c) (int)(c).size()
20 #define mp(a, b) make_pair(a, b)
21 #define all(c) (c).begin(), (c).end()
22 #define iter(c) decltype((c).begin())
23 #define cls(arr,val) memset(arr,val,sizeof(arr))
24 #define cpresent(c, e) (find(all(c), (e)) != (c).end())
25 #define rep(i, n) for (int i = 1; i <= (int)(n); i++)
26 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
27 const int N = 1010;
28 const int INF = 0x7fffffff;
29 typedef long long ll;
30 ll p, ans, arr[N], sum[N];
31 int main() {
32 #ifdef LOCAL
33     freopen("in.txt", "r", stdin);
34     freopen("out.txt", "w+", stdout);
35 #endif
36     int t, n;
37     scanf("%d", &t);
38     while (t--) {
39         ans =  -INF;
40         scanf("%d %lld", &n, &p);
41         rep(i, n) scanf("%lld", &arr[i]);
42         rep(i, n) {
43             ll tmp = arr[i]; arr[i] = p;
44             rep(j, n) {
45                 sum[j] = sum[j - 1] > 0 ? sum[j - 1] + arr[j] : arr[j];
46                 ans = max(ans, sum[j]);
47             }
48             arr[i] = tmp;
49         }
50         printf("%lld\n", ans);
51     }
52     return 0;
53 }
View Code

转载于:https://www.cnblogs.com/GadyPu/p/4641675.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值