HDU 5280 BestCoder Round#47 1001 ---枚举+dp

来源:互联网 发布:mac自带的php环境 编辑:程序博客网 时间:2024/05/29 16:44

                                                         Senior's Array

                                           Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

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.

1n1000,109A[i],P109
 

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

Sample Input
23 51 -1 23 -21 -1 2
 

Sample Output
82
 

Source
BestCoder Round #47 ($)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5280
对于数组每一个元素换成p;在计算数列和最大值即可。
#include <iostream>using namespace std;long long a[1100],n;long long dp[1100];long long unit(){    long long sum=a[0];    dp[0]=a[0];    for(int i=1; i<n; i++)    {        if(dp[i-1]+a[i]>a[i])        {            dp[i]=a[i]+dp[i-1];        }        else        {            dp[i]=a[i];        }        if(sum<dp[i])            sum=dp[i];    }    return sum;}int main(){    long long t,p;    cin>>t;    while(t--)    {        cin>>n>>p;        for(int i=0; i<n; i++)        {            cin>>a[i];        }        long long ans=-1000000000001;        for(int i=0;i<n;i++)        {          long long tmp=a[i];          a[i]=p;          long long x;           x=unit();           ans=max(ans,x);           a[i]=tmp;        }        cout<<ans<<endl;    }    return 0;}


0 0
原创粉丝点击