7.26 D - Max Sum

                                          D - Max Sum


Given a sequence a 1 1,a 2 2,a 3 3......a n 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
Sample Output
Case 1:
14 1 4

Case 2:
7 1 6


#include<stdio.h>
#include<string.h>
int a[120000];
int main()
{
	int T,t,k,s,i,j,m,n,max,f,sum;
	while(scanf("%d",&T)!=EOF)
	{
		f=1;
		
		while(T--)
		{

			scanf("%d",&n);
			for(i=1;i<=n;i++)
			{
				scanf("%d",&a[i]);
			}
			max=-1e9;
			s=t=k=1;
			sum=0;
			for(i=1;i<=n;i++)
			{
				sum=sum+a[i];
				
				if(max<sum)
				{
					max=sum;
					t=s;
					k=i;
				}
				if(sum<0)
				{
					sum=0;
					s=i+1;
				}
			}
		
			printf("Case %d:\n%d %d %d\n",f++,max,t,k);
			if(T>0)
				printf("\n");
			
		}
	
	}
	return 0;
} 


 

以下是求解多元多项式回归的 MATLAB 代码: ```matlab % 输入数据 x1 = [36.4 37.2 37.2 36.2 31.5 28.9 27.2 26.7 26.1 26.1 25.4 25.3 25.4]'; x2 = [50.0 52.0 49.0 51.0 68.0 74.0 83.0 82.0 88.0 88.0 90.0 88.0 87.0]'; x3 = [982.9 982.2 981.8 981.6 982.3 982.6 983.4 983.5 984.0 983.6 984.4 984.5 984.4]'; y = [-7.30 -7.36 -7.35 -7.33 -7.31 -7.30 -7.26 -7.22 -7.21 -7.23 -7.18 -7.17 -7.14]'; % 构建设计矩阵X X = [ones(size(x1)) x1 x2 x3 x1.^2 x1.*x2 x1.*x3 x2.^2 x2.*x3 x3.^2]; % 求解回归系数 beta = X \ y; % 构建预测模型 model = @(x1,x2,x3) beta(1) + beta(2)*x1 + beta(3)*x2 + beta(4)*x3 ... + beta(5)*x1.^2 + beta(6)*x1.*x2 + beta(7)*x1.*x3 ... + beta(8)*x2.^2 + beta(9)*x2.*x3 + beta(10)*x3.^2; % 预测并绘制拟合图 x1fit = min(x1):0.1:max(x1); x2fit = min(x2):0.1:max(x2); x3fit = min(x3):0.1:max(x3); [X1FIT,X2FIT,X3FIT] = meshgrid(x1fit,x2fit,x3fit); YFIT = model(X1FIT,X2FIT,X3FIT); figure; plot3(x1,x2,x3,'o',x1fit,x2fit,x3fit,'*'); hold on; mesh(X1FIT,X2FIT,X3FIT,YFIT); xlabel('x1'); ylabel('x2'); zlabel('x3'); title('拟合图'); % 绘制残差图 YFIT = model(x1,x2,x3); figure; plot(YFIT - y,'o'); xlabel('样本编号'); ylabel('残差'); title('残差图'); ``` 运行上述代码后,会先绘制拟合图,然后绘制残差图。拟合图中,蓝色的点表示原始数据,红色的点表示拟合值,可以看到拟合值与原始数据比较接近;残差图中,横轴表示样本编号,纵轴表示残差,残差的分布应该比较均匀,没有明显的规律。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值