HDU-5280

Senior‘s Array

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 852    Accepted Submission(s): 311

Problem 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≤n≤1000, −109≤A[i],P≤109。

Output

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

Sample Input

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

Sample Output

8

2

Source

BestCoder Round #47 ($)

/**
    题意:给出一个数组,如果用p代替数组中的一个数求最大的区间和
    做法:dp(比赛的时候想得太多)
**/
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#define maxn 1100
#define INF 0x7fffffff
using namespace std;
long long  mmap[maxn];
long long  dp[2][maxn];
long long n;
long long DP()
{
    dp[0][0] = max((long long)0,mmap[0]);
    for(int i=1;i<n;i++)
    {
        dp[0][i] = max((long long)0,dp[0][i-1] + mmap[i]);
    }
    long long tmp = mmap[0];
    for(int i=1;i<n;i++)
    {
        dp[1][i] = dp[0][i-1] + mmap[i];
        tmp = max(tmp,dp[1][i]);
    }
    return tmp;
}
int main()
{
//#ifndef ONLINE_JUDGE
//    freopen("in.txt","r",stdin);
//#endif // ONLINE_JUDGE
    int T;
    scanf("%d",&T);
    while(T--)
    {
        long long p;
        scanf("%lld %lld",&n,&p);
        for(int i=0;i<n;i++)
        {
            scanf("%lld",&mmap[i]);
        }
        memset(dp,0,sizeof(dp));
        long long res = -INF;
        for(int i=0;i<n;i++)
        {
            long long tt = mmap[i];
            mmap[i] = p;
            res = max(res,DP());
            mmap[i] = tt;
        }
        printf("%lld\n",res);
    }
    return 0;
}
时间: 2024-08-22 02:22:54

HDU-5280的相关文章

hdu 5280 Senior&#39;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. Th

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 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 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 贪心 O(n)算法

题意给你一个序列A[1...N],你必须修改一个A[i]为P,使得修改后的序列A的连续最大和最大 其中N<=1000 分析,N非常小,N^2暴力随便做,不细讲 说一个O(N)的算法 我们知道O(N)的求连续最大和的算法 那么定义L[i], R[i]分别为L[i]以i为结尾的最大连续和,R[i]一i为开头的连续最大和 由于必须要修改一个A[i]为P,这个修改后的A[i]可能不包含在连续最大和中,也可能包含在连续最大和中 如果包含,那么就等价于:max(L[i - 1], 0) + max(R[i

【Aguin】第九周 7.12-7.18

7.12 HDU 5280 Senior's Array 补一个O(n)的dp方法. dp1[i]为i左端最大连续和.dp2[i]为i右端最大连续和. 枚举改p的位置. 若p在最大区间和中.则ans为p左右最大连续和加上p. 若p不在最大区间和中.则ans为所有最大连续和中最大的. 注意区间取整个数组时.p是一定包含在内的. 即第二种情况不能取整个数组.  Aguin 7.13 结果上讲的确什么都没干. 7.14 补一个上次的BC HDU 5282 Senior's String 本来是想昨天补

Bestcoder Round 47 &amp;&amp; 48

1.Senior's Array(hdu 5280) 题目大意:给出大小为N的数组和P,求将数组中的某个元素替换为P后的最大连续子段和.N<=1000 题解: 1.送分题,比赛的时候只想到枚举替换的元素然后贪心找最大连续子段和.时间复杂度O(N2) 2.实际上有更好的做法.类似线段树的合并,枚举替换元素的时候 可以把左右两边的答案合并.F[i]表示以i结尾,G[i]表示以i开头的最优解.合并的时候只要考虑把中间的P用起来,即 A[x]+max(0,F[x-1])+max(0,G[x+1]) .