HDU 4561 连续最大积

题意:中文题不解释。。。


思路:把0作为划分,分段统计下区间负数的个数即可。如果是偶数个,那么直接返回区间长度。如果是奇数个,那么去掉最左边的负数及其左边所有数,算出剩下的个数,右边同理。


代码:


#include <algorithm>
#include <iostream>
#include <string>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <list>
#include <set>
#include <map>
using namespace std;

/*--in common define-----*/
#define N  10010
#define E  100010
#define ll long long
const int INF   =0x3fffffff;
const int PRIME =999983;
const int MOD   =1000000007;
const int MULTI =1000000007;
const double EPS=1e-8;
/*--end in common define-*/

/*--in common use--------*/
#define CUBE(x) ((x)*(x)*(x))
#define SQ(x)     ((x)*(x))
#define ALL(x)     x.begin(),x.end()
#define CLR(x,a) memset(x,a,sizeof(x))
inline bool isodd(int x){return x&1;}
inline bool isodd(ll x) {return x&1;}
/*--end in common use----*/


int a[N];

inline int gao(int l,int r)
{
	int cnt=0,ans=0;
	for(int i=l;i<=r;i++)
		if(a[i]<0) cnt++;
	if(cnt%2==0) return r-l+1;
	for(int i=l;i<=r;i++)
		if(a[i]<0){
			ans=max(ans,r-(i+1)+1);
			break;
		}
	for(int i=r;i>=l;i--)
		if(a[i]<0){
			ans=max(ans,i-l);
			break;
		}
	return ans;
}

int main()
{
	int re,n,ans,Case=1;
	scanf("%d",&re);
	while(re--){
		scanf("%d",&n);
		for(int i=0;i<n;i++) scanf("%d",&a[i]);
		ans=0;
		for(int i=0,j;i<n;i=j){
			while(i<n && a[i]==0) i++; j=i;
			while(j<n && a[j]!=0) j++;
			if(i<n) ans=max(ans,gao(i,j-1));
		}
		printf("Case #%d: %d\n",Case++,ans);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值