首页 > 其他分享 >Max Sum

Max Sum

时间:2024-02-20 11:33:46浏览次数:31  
标签:sequence int Max Sum memset line include dp

Vjudge

  • Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.

  • Input
    The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).

  • Output
    For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.

  • Sample

  • Input

    2								
    5 6 -1 5 4 -7					
    7 0 6 -1 1 -6 7 -5 				
    
  • Output

    Case 1:
    14 1 4
    
    Case 2:
    7 1 6
    

    原思路:直接DP,用一维数组,否则会爆,但还是会超时
    超时代码如下

    点击查看代码
    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <math.h>
    #include <string.h>
    using namespace std;
    int t,n,a[100005],dp[100005],k;
    void solve()
    {
    	cin>>n;
    	memset(a,0,sizeof(a));
    	memset(dp,0,sizeof(dp));
    //	memset(vis,0,sizeof(vis));
    	for(int i=1;i<=n;i++)
    	{
    		cin>>a[i];
    	}
    	int ans=0,st=0,en=0;
    	for(int i=1;i<=n;i++)
    	{
    		for(int j=i;j<=n;j++)
    		{
    			if(dp[j]<dp[j-1]+a[j])
    			{
    				dp[j]=dp[j-1]+a[j];
    			}
    			//dp[j]=max(dp[j],dp[j-1]+a[j]);
    			if(ans<dp[j])
    			{
    				ans=dp[j];
    				st=i,en=j;
    			}
    
    //			cout<<i<<" "<<j<<" "<<dp[i][j]<<endl;
    		}
    	}
    	cout<<"Case "<<k<<":"<<endl;
    	cout<<ans<<" "<<st<<" "<<en<<endl;
    	if(t)cout<<endl;
    }
    int main()
    {
    	ios_base::sync_with_stdio(false);
    	cin.tie(0);cout.tie(0);
    	cin>>t;
    	while(t--)
    	{
    		k++;
    		solve();
    	}
    	return 0;
    

}
```

O(n^2)的复杂度显然会超时,所以我们可以用前缀和维护,代替数组

标签:sequence,int,Max,Sum,memset,line,include,dp
From: https://www.cnblogs.com/wlesq/p/18022705

相关文章

  • 用python脚本自动发送钉钉消息出现服务器异常的报错: HTTPSConnectionPool(host='oapi.
    一、问题描述执行python脚本发送钉钉消息,出现报错:HTTPSConnectionPool(host='oapi.dingtalk.com',port=443):Maxretriesexceededwithurl:/robot/send?access_token=43df999582e899dc6815c9d6346c9d253060259625c92e4f166e25ea58e5bdb5&timestamp=1708242748918&sign......
  • 149. Max Points on a Line
    149.MaxPointsonaLineGivenanarrayof points where points[i]=[xi,yi] representsapointonthe X-Y plane,return themaximumnumberofpointsthatlieonthesamestraightline.Example1:Input:points=[[1,1],[2,2],[3,3]]Output:3E......
  • CF1196B Odd Sum Segments题解
    【问题分析】本题考了奇数。由此想到以下定律:奇数+偶数=奇数;奇数+奇数=偶数;偶数+偶数=偶数;所以偶数可以忽略不计,只有奇数可以对结果产生影响,所以我们只要注意奇数即可。经过思考可得奇数的个数至少为$k$个且比$k$多的个数为偶数,此时多出的奇数可组成偶数,对结果不产生......
  • CF1879D Sum of XOR Functions
    记前缀异或和数组\(s\),于是题目中的\(f(l,r)\)可以被表示成\(s_r\opluss_{l-1}\),转化为求\(\sum\limits_{l=1}^n\sum\limits_{r=l}^n(s_r\opluss_{l-1})\times(r-l+1)\)。再记录\(4\)个值,\(cnt_0,cnt_1,sum_0,sum_1\)分别表示当前已经出现过的\(0/1\)的个数,出......
  • bug-missing GOSUMDB
     问题描述:D:\gopj>gomodtidygo:findingmoduleforpackagego.uber.org/zapgo:findingmoduleforpackagegithub.com/valyala/fasthttpgo:downloadinggo.uber.org/zapv1.26.0go:downloadinggithub.com/valyala/fasthttpv1.52.0go:githun.com/bigwh......
  • 迈从AX5 PRO MAX拆解以及驱动说明
    迈从黑武士AX5PROMAX拆解图看到拆解可以了解以下:编码器是防尘的、鼠标微动是TTC的光微动,是免焊接的,后续更换方便。鼠标正面涂了三防漆,背面无。按键柱的两个贴片目测至少0.7MM以上 鼠标微动手感:脆闷AX5PROMAX型号: 602535500MA 电池数据:长36MM、宽24.5MM、厚度5.8MM电......
  • Codeforces Round 169 (Div. 2)C. Little Girl and Maximum Sum(差分、贪心)
    目录题面链接题意题解代码总结题面链接C.LittleGirlandMaximumSum题意给q个[l,r]将所有这些区间里面的数相加和最大。可以进行的操作是任意排列数组题解对出现的每个区间内的位置加上1,代表权值操作完之后求一遍前缀和,得到每个位置的权值然后贪心的考虑,权值越大,应......
  • [转帖]linux参数之max_map_count
    https://www.cnblogs.com/duanxz/p/3567068.html “Thisfilecontainsthemaximumnumberofmemorymapareasaprocessmayhave.Memorymapareasareusedasaside-effectofcallingmalloc,directlybymmapandmprotect,andalsowhenloadingsharedlibr......
  • Atcoder Grand Contest 056 B - Range Argmax
    因为一组\(x\)可能对应多组\(p\),考虑怎么让决策唯一化。我们从大到小依次钦定每个值的位置,即倒着遍历\(i=n,n-1,\cdots,1\),找到最左端的位置\(v\)满足,对于现在还活着的所有区间\(j\)满足\(l_j\lev\ler_j\),都有\(x_j=v\),令\(p_j=i\),然后删去所有包含\(i\)的区间。......
  • Educational Codeforces Round 145 (Rated for Div. 2)C. Sum on Subarrays(构造)
    很意思的一道构造题题意:给一个\(n、k\),让构造长度为n的数组满足,子数组为整数的个数为k个,负数的为\(k-(n+1)*n/2\),每个数的范围为\([-1000,1000]\)这种构造题可以考虑就是前一段可以一直用一样的、最小的。我们观察可以发现\(k+k-(n+1)*n/2=(n+1)*n/2\)也就是所有子数组......