题意给你一个序列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 + 1], 0) + P
如果不包含,那么就计算1...n-1的最大L[i], 2...n的最大的R[i]
为什么是 计算1...n-1的最大L[i], 2...n的最大的R[i] 而不是全部跑一遍
都是少一个?
因为必须修改A[1...n]中的某一个元素为P
比如说
n = 3 P = -100
A[1...3] = {10, 10, 10}
那么答案就是20,并非30
总是少一个的原因就是假设修改的那一个元素并没有影响到当前答案
如果修改后的A[i]=P并没有在最大的连续子段和中,那么一定在这连续子段和之外,设这个最大连续子段和的子段为A[l...r],一定保证l>1或者r<n,不然最大连续子段和的子段就变成A[1...n],与前面矛盾,那么我们就可以把A[1]变成P或者把A[n]变成P
若i=n,那么我们就只需要枚举L[1...n - 1]
若i=1,那么我们就只需要枚举R[2..n]
 #include <set>
#include <map>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <iostream>
#include <algorithm> #define dprint(expr) fprintf(stderr, #expr " = %d\n", expr) using namespace std; typedef long long LL;
typedef pair <int, int> PII; const int N = 1e5 + ;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + ;
const double EPS = 1e-;
const double PI = acos(-1.0); int a[N];
LL l[N], r[N]; int main(void){
int T;
scanf("%d", &T);
while (T--) {
int n, p;
scanf("%d%d", &n, &p);
for (int i = ; i <= n; ++i)
scanf("%d", &a[i]);
l[] = r[] = l[n + ] = r[n + ] = ;
for (int i = ; i <= n; ++i)
l[i] = max(l[i - ] + a[i], (LL)a[i]);
for (int i = n; i; --i)
r[i] = max(r[i + ] + a[i], (LL)a[i]);
LL ans = -INF;
for (int i = ; i <= n; ++i) {
ans = max(ans, max(l[i - ], 0LL) + max(r[i + ], 0LL) + p);
}
for (int i = ; i < n; ++i) {
ans = max(ans, l[i]);
}
for (int i = n; i > ; --i) {
ans = max(ans, r[i]);
}
printf("%I64d\n", ans);
}
return ;
}

最新文章

  1. winform中textbox属性Multiline=true时全选
  2. CSS命名规范
  3. JDBC性能分析与优化
  4. 20141109--SQL 练习题-1
  5. Angular 2.0 从0到1 (六)
  6. 使用FileResult导出txtl数据文件
  7. .net项目IIS、VS 附加进程调试
  8. Dev的GridControl控件选择框的使用
  9. 总结NAND FLASH控制器的操作
  10. jvm 常用内存分析命令
  11. Flash与 Javascript 交互
  12. WebGL&amp;Three.js工作原理
  13. uabntu18.04 安装mysql5.7
  14. golang web实战之一(beego,mvc postgresql)
  15. 服务器-华为RH5885 V3-安装Windows Server 2008R2后设备管理器中存在大量的感叹号,并且无法识别网络适配器,没有网卡
  16. golang编译源代码和交叉编译方法
  17. PC/FORTH 数字类型
  18. Windows 7上安装配置TensorFlow-GPU运算环境
  19. 使用tar命令解压的时候报错not in gzip format
  20. django源码笔记-【1】(转)

热门文章

  1. 【AMAD】django-silk -- 为Django提供如丝般顺滑的性能测量
  2. TCP和SSL TCP应用
  3. eclipse 导出jar 没有主清单属性的解决方法
  4. spring boot 框架根據 sql 創建語句自動生成 MVC層類代碼
  5. (一)使用twisted Deferred
  6. [BZOJ2594] [WC2006]水管局长(Kruskal+LCT)
  7. python-1:正则表达式(基础知识点)
  8. Codeforces 1221E. Game With String
  9. [Next] 五.next自定义内容
  10. tomcat启动报ClassNotFound