20考研率辉计算机复试(分治法)

航天OJ1003

#include<iostream>
using namespace std;
void getCrossMax(int arr[],int &L,int mid,int &R,int &max)
{
	int maxCross =0, maxL=-1001, maxR=-1001,l=mid,r=mid+1;
	for(int i=mid;i>=L;--i)
	{
		maxCross+=arr[i];
		if(maxCross>=maxL)
		{
			l=i;
			maxL=maxCross;
		}
	}
	for(int i=mid+1,maxCross=0;i<=R;++i)
	{
		maxCross+=arr[i];
		if(maxCross>maxR)
		{
			r=i;
			maxR=maxCross;
		}
	}
	max=maxL+maxR;
	L=l;
	R=r;
}
void getMax(int arr[],int &L,int &R,int &max)
{
	if(L<R)
	{
		int l=L,r=R,maxL,maxR,maxCross;
		int m=(L+R)/2;
		int l1=l,l2=m+1,r1=m,r2=r;
		getMax(arr,l1,r1,maxL);
		getMax(arr,l2,r2,maxR);
		getCrossMax(arr,l,m,r,maxCross);
		int tempMax=maxL,tempL=l1,tempR=r1;
		if(tempMax<maxR)
		{
			tempMax=maxR;
			tempL=l2;
			tempR=r2;
		}
		if(tempMax<maxCross)
		{
			tempMax=maxCross;
			tempL=l;
			tempR=r;
			max=tempMax;
			L=tempL;
			R=tempR;
		}
	}
		else
		{
			max=arr[L];
		}
}
int main()
{
	int i,j = 1,n,m,L,R,max;
	int a[100001];
	cin>>n;
	while(n--) 
	{
		cin>>m;
		for(int i=1;i<=m;++i)
		cin>>a[i];
		L=1;
		R=m;
		getMax(a,L,R,max);
		cout<<"Case"<<j++<<":\n"<<max<<" "<<L<<" "<<R<<endl;
		if(n!=0)
		cout<<endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值