CF 1215 B The Number of Products(思维题)

链接:https://codeforces.com/contest/1215/problem/B

You are given a sequence a1,a2,,ana1,a2,…,an consisting of nn non-zero integers (i.e. ai0ai≠0).

You have to calculate two following values:

  1. the number of pairs of indices (l,r)(l,r) (lr)(l≤r) such that alal+1ar1aral⋅al+1…ar−1⋅ar is negative;
  2. the number of pairs of indices (l,r)(l,r) (lr)(l≤r) such that alal+1ar1aral⋅al+1…ar−1⋅ar is positive;
Input

The first line contains one integer n(1n2105)(1≤n≤2⋅105) — the number of elements in the sequence.

The second line contains nn integers a1,a2,,ana1,a2,…,an (109ai109;ai0)(−109≤ai≤109;ai≠0) — the elements of the sequence.

Output

Print two integers — the number of subsegments with negative product and the number of subsegments with positive product, respectively.

 

题意:求乘积为负数和正数的子段个数(没有0)

题解:正数0,负数1,统计前缀和(就是求前缀和奇偶),再从头扫一遍,维护2个桶前缀和为奇数的个数和前缀和为偶数的个数。

#include <bits/stdc++.h>
using namespace std;
 
const int maxn=2e5+5;
int n;
int a[maxn], sum[maxn], cnt[2];
long long pos, neg;
 
int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin>>n;
    for(int i=1; i<=n; i++)
    {
        cin>>a[i];
        if(a[i]>0) a[i]=0;
        else a[i]=1;
    }
    for(int i=1; i<=n; i++)
        sum[i]=sum[i-1]+a[i];
    for(int i=1; i<=n; i++)
        sum[i]=sum[i]%2;
    cnt[0]++;                 //注意cnt0初始化为1,表示区间长度为1的那个
    for(int i=1; i<=n; i++)
    {
        if(sum[i]){
            pos+=cnt[1];
            neg+=cnt[0];
        }
        else{
            pos+=cnt[0];
            neg+=cnt[1];
        }
        cnt[sum[i]]++;
    }
    cout<<neg<<" "<<pos;
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/Yokel062/p/11613839.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值