$47 A Senior'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 <iostream>
 7 #include <stdio.h>
 8 #include <stdlib.h>
 9 #include <math.h>
10 #include <string.h>
11 #include <algorithm>
12 /*修改数组中的某一个数 使得数组的区间和最大*/
13 typedef long long LL;
14 const int maxn=1005;
15 const LL min_=-1e16-1;
16 LL a[maxn];
17 /*dp[i+1]前i+1个的最大区间和*/
18 int main()
19 {
20     LL t,n,p,ans;
21     //printf("%I64d
",min_);
22     scanf("%I64d",&t);
23     while(t--)
24     {
25         ans=min_;
26         scanf("%I64d%I64d",&n,&p);
27         for(int i=0; i<n; i++)
28             scanf("%I64d",&a[i]);
29         for(int i=0; i<n; i++)
30         {
31             LL temp=a[i],sum=min_;
32             a[i]=p;
33             for(int j=0;j<n;j++)
34             {
35                 if(sum<0) sum=a[j];
36                 else sum+=a[j];
37                 if(sum>ans) ans=sum;
38             }
39             a[i]=temp;
40         }
41         printf("%I64d
",ans);
42     }
43     return 0;
44 }
View Code
原文地址:https://www.cnblogs.com/linxhsy/p/4641972.html