2012北邮腾讯创新俱乐部热身赛 A
来源:互联网 发布:淘宝不可以货到付款吗 编辑:程序博客网 时间:2024/06/06 03:24
这场比赛打得还是挺开心的,ak了!哈哈哈~~A题是一道dp,复杂度O(n),挺经典的~
Description
Given a set of n integers: A={a1, a2,..., an}, we define a function d(A) as below:
d(A)=sum{a[s1]~a[t1]}+sum{a[s2]~a[t2]}
The rule is 1<=s1<=t1<s2<=t2<=n.
Your task is to calculate d(A).
Input
The input consists of T(<=30) test cases. The number of test cases (T) is given in the first line of the input.
Each test case contains two lines. The first line is an integer n(2<=n<=50000). The second line contains n integers: a1, a2, ..., an. (|ai| <= 10000).There is an empty line after each case.
Output
Print exactly one line for each test case. The line should contain the integer d(A).
Sample Input
1
10
1 -1 2 2 3 -3 4 -4 5 -5
Sample Output
13
Hint
In the sample, we choose {2,2,3,-3,4} and {5}, then we can get the answer.
Huge input,scanf is recommended.
#include<iostream>#include<vector>#include<algorithm>#include<cstdio>#include<queue>#include<stack>#include<string>#include<map>#include<set>#include<cmath>#include<cassert>#include<cstring>#include<iomanip>using namespace std;#ifdef _WIN32#define i64 __int64#define out64 "%I64d\n"#define in64 "%I64d"#else#define i64 long long#define out64 "%lld\n"#define in64 "%lld"#endif#define FOR(i,a,b) for( int i = (a) ; i <= (b) ; i ++)#define FF(i,a) for( int i = 0 ; i < (a) ; i ++)#define FFD(i,a) for( int i = (a)-1 ; i >= 0 ; i --)#define S64(a) scanf(in64,&a)#define SS(a) scanf("%d",&a)#define LL(a) ((a)<<1)#define RR(a) (((a)<<1)+1)#define SZ(a) ((int)a.size())#define PP(n,m,a)puts("---");FF(i,n){FF(j,m)cout << a[i][j] << ' ';puts("");}#define pb push_back#define CL(Q) while(!Q.empty())Q.pop()#define MM(name,what) memset(name,what,sizeof(name))#define read freopen("in.txt","r",stdin)#define write freopen("out.txt","w",stdout)const int inf = 0x3f3f3f3f;const i64 inf64 = 0x3f3f3f3f3f3f3f3fLL;const double oo = 10e9;const double eps = 10e-10;const double pi = acos(-1.0);const int maxn = 51111;int a[maxn];int dp[maxn];int sum[maxn];int b[maxn];int s2[maxn];int n,T;void dpstart(){ memset(dp,0,sizeof(dp)); memset(sum,0,sizeof(sum)); memset(b,0,sizeof(b)); memset(s2,0,sizeof(s2)); dp[1] = a[1]; sum[1] = a[1]; for(int i=2;i<=n;i++) { sum[i] = max(sum[i-1]+a[i],a[i]); dp[i] = max(dp[i-1],sum[i]); } b[n] = a[n]; s2[n]=a[n]; for(int i=n-1;i>=1;i--) { s2[i]=max(s2[i+1]+a[i],a[i]); b[i]=max(b[i+1],s2[i]); } int ans = -inf; for(int i=1;i<=n-1;i++) { ans = max(dp[i]+b[i+1],ans); } cout<<ans<<endl; return ;}int main(){ cin>>T; while(T--) { cin>>n; memset(a,0,sizeof(a)); for(int i=1;i<=n;i++) { SS(a[i]); } dpstart(); } return 0;}
- 2012北邮腾讯创新俱乐部热身赛 A
- 2012北邮腾讯创新俱乐部热身赛 B
- 西北工业大学腾讯创新俱乐部的情愫
- 2012 夏季高校微软俱乐部活动 - 开门创新
- 2012 夏季高校微软俱乐部活动 - 开门创新
- 【2012四川省热身赛】Problem A. A - B Problem
- CTO俱乐部走进腾讯
- SYSU暑假热身赛A
- 哈尔滨赛区热身赛 A题
- 辽宁赛区热身赛A-Game
- 腾讯TIC创新大赛
- 2012天津热身赛
- 热身赛
- 热身赛
- 【2012年腾讯俱乐部ACM赛新手组1001】简单大数取余-99 division
- 【2012年腾讯俱乐部ACM赛新手组1003】Fengsensei的DOTA
- HDU 4226 迎接2012新赛季——HDOJ系列热身赛(5)(A题)
- 第二场热身赛A(简单水题)
- php写一个简单的通讯录
- 为自己的人生加油
- CFileDialog文件操作详解
- Java 发送邮件
- java文件打包
- 2012北邮腾讯创新俱乐部热身赛 A
- 计算机无法通过域验证问题的几种情况
- MyEclipse编辑问题
- 数据库优化措施
- java开发相关资料
- JavaScript escape() 函数编码
- MFC学习总结 (67个技巧)
- Oracle SQL*PLUS基础及sqlplus命令详解-2
- Ortp的使用(待续)