HDU 5280 Senior's Array(DP思想 暴力)

来源:互联网 发布:买个淘宝店 编辑:程序博客网 时间:2024/05/23 19:20
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 Pn means the number of elements of the array. P means the value Mini-Sun can change to. 

The next line contains the original array.

1n1000109A[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

题意:将数组中的一个数替换成p,使得替换后的数组的区间和最大,输出最大的区间和。
没什么难度,理解题意就会发现是求替换后的子序列的最大和,数据范围不大,可以直接借用DP的思想,直接暴力,过程中维护最大值。

AC代码:
#include<stdio.h>  #include<string.h>  #include<algorithm>  using namespace std;  #define N 1005  long long a[N];  long long dp[N];  int main()  {      long long x;      scanf("%lld",&x);      while(x--)      {          long long i,j,m,n;          long long  M=-0x3f3f3f3f;          scanf("%lld%lld",&m,&n);          for(i=1;i<=m;i++)          {              scanf("%lld",&a[i]);          }          memset(dp,0,sizeof(dp));          for(i=1;i<=m;i++)          {              int t=a[i];              a[i]=n;              for(j=1;j<=m;j++)              {                  dp[j]=max(a[j],dp[j-1]+a[j]);                  M=max(M,dp[j]);              }              a[i]=t;          }          printf("%lld\n",M);      }      return 0;  }  






0 0
原创粉丝点击