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 }

hdu 5280 Senior's Array

时间: 2024-12-26 13:18:08

hdu 5280 Senior's Array的相关文章

hdu 5280 Senior&#39;s Array(最大子段和)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5280 题意:将一个长度为n的数组,将里面某一个数改为p,使改变后最大子段和最大. 题解:dp[i]=max(dp[i-1)+a[i],a[i]),表示以第 i 个数结束的最大子段和,时间复杂度为O(n). 1)由于n<=1000,可以暴力解决,将每一个数都依次改为p,求出最大的子段和,再去这些最大子段和中最大的,时间复杂度为O(n*n); #include <iostream> #inclu

HDU 5280 Senior&#39;s Array (暴力,水)

题意:给一个数列,再给一个数字p,要求p一定要替换掉数列中的一个元素,然后求最大连续子序列之和. 思路:1000*1000的复杂度,O(n*n) .就是每个都试,然后求和. 1 #include <bits/stdc++.h> 2 #define LL long long 3 #define pii pair<int,int> 4 #define INF 0x7f7f7f7f 5 using namespace std; 6 const int N=2000; 7 int a[N]

[最大子序列和]Hdu 5280 Senior&#39;s Array

题意:一个序列,在其中一个数必须替换成给定数字p的条件下,求最大连续子序列之和. 依次把每一个数替换成p,求每次的最大连续和,找出最大值.O(n^2). #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> typedef long long ll; using namespace std; const int MAXN=1000+5; const int

HDU 5280 Senior&#39;s Array 最大区间和

题意:给定n个数,要求必须将其中某个数改为P,求改动后最大的区间和可以为多少. 水题.枚举每个区间,如果该区间不修改(即修改该区间以外的数),则就为该区间和,若该区间要修改,因为必须修改,所以肯定是把最小的数修改为P能保证该区间最后和最大,所以比较两种方案的较大者.对于每个区间取出的较大者,再取总共的最大者即可.注意一个trick,枚举到整个区间的时候,是必须要修改一个数的,所以这个最大的这个区间只有一种方案.先预处理1~i的区间和,维护每个区间的最小值和区间和. #include <iostr

HDU 5280 Senior&amp;#39;s Array 最大区间和

题意:给定n个数.要求必须将当中某个数改为P,求修改后最大的区间和能够为多少. 水题.枚举每一个区间.假设该区间不改动(即改动该区间以外的数),则就为该区间和,若该区间要改动,由于必须改动,所以肯定是把最小的数改动为P能保证该区间最后和最大,所以比較两种方案的较大者.对于每一个区间取出的较大者,再取总共的最大者就可以.注意一个trick,枚举到整个区间的时候,是必需要改动一个数的.所以这个最大的这个区间仅仅有一种方案. 先预处理1~i的区间和,维护每一个区间的最小值和区间和. #include

HDU 5280 BestCoder Round #47 1001:Senior&#39;s Array

Senior's Array Accepts: 199 Submissions: 944 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 某天学姐姐得到了一个数组A,在这个数组的所有非空区间中,她找出了一个区间和最大的,并把这个区间和定义为这个数组的美丽值. 但是她觉得这个数组不够美,于是决定修理一下这个数组. 学姐姐将会进行一次操作,把原数组中的某个数修改为P(必须修改)

hdu 5281 Senior&#39;s Gun

题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5281 Senior's Gun Description Xuejiejie is a beautiful and charming sharpshooter. She often carries $n$ guns, and every gun has an attack power $a[i]$. One day, Xuejiejie goes outside and comes across $m

HDU #5283 Senior&#39;s Fish

题目描述: 平面上有一些鱼,初始时鱼会在一些位置,某些时刻编号在一段区间内的鱼会同时向x轴正方向,或y轴正方向平移一定距离,某些时刻会询问一个矩形内鱼的数量. 解题思路: 显然地,求一个矩形内的鱼可以用矩形四个顶点为右上角的整个左下矩形加加减减.那么问题就转化为一个顶点左下角矩形内鱼的数量.注意到鱼只会向右和向上的话,那就很好做.维护两颗线段树,分别维护编号在l~r的鱼的x坐标最大值.y坐标最大值.每次修改对应区间加.每次如果区间最大值大于限制,那就找出最大值的位置,赋为-inf,同时在树状数组

$47 A Senior&#39;s Array

题目要求:给出n个数的数组,要在里面把某一个数替换成P.寻求区间和最大. O(n)的算法不会,日后专研更新 1 /* ********************************************** 2 Auther: linhan 3 Created Time: 2015-06-27 21:55:10 4 File Name : 新建文本文档.txt 5 *********************************************** */ 6 #include <i