首页 > 其他 > 详细

B. The Number of Products(Codeforces Round #585 (Div. 2))

时间:2019-09-23 20:01:55      阅读:126      评论:0      收藏:0      [点我收藏+]

本题地址:  https://codeforces.com/contest/1215/problem/B

本场比赛A题题解:https://www.cnblogs.com/liyexin/p/11535519.html

B. The Number of Products
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
 
standard output

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.

 

Examples
input
Copy
5
5 -3 3 -1 1
output
Copy
8 7
input
Copy
10
4 2 -4 3 1 2 -4 3 2 3
output
Copy
28 27
input
Copy 
5
-1 -2 -3 -4 -5
output
Copy
9 6

  题意:给你n长度的数组。分别求出任意长度连续区间内乘积为负数和正数的个数。   
      暴力会超时,int 会炸。需要O(n)才能过。
      这需要一个继承的思想。
      看这么一组: 1  2  3    令a=0此为正数个数,sum为区间乘积为正数个数。读入1,a++,sum+=a;a=1,sum=1;
                                              读入2,a++,sum+=a;a=2,sum=3;
                                             读入3,a++,sum+=a;a=3,sum=6;
           这个sum累加的过程符合求区间个数的过程。 
     
ll n,a=0,b=0,alla=0,allb=0;    //a统计当前正数区间数,b统计当前负数区间数。alla为所有区间乘积为正数个数,allb为所有区间乘积为负数个数.

      正数的加入不会改变正负号。

      比如对于1,2,3。相当于一个继承过程。我们可以把初始看成1,2,?。(只有2个数)“?”处填入的数,并不影响原来只有1,2时的区间数,a++只是多了一个{3}而已。“?”处只是一个可有可无的东西,加入不加入3,。1,2就那么多区间,1,2,3也就那么多区间,两者唯一区别就是多了一个{3},所以a++即可,当然这是对全正数的处理。

      加入一个负数的话,继承之前的正数区间数swap(a,b),因为负数的加入使正数区间变为负数区间(上段思想),然后有一个{-1}所以再b++即可。同理正数区间继承了负数区间个数,但不需要a++,因为加入了{-1}再乘就不是正数了。

      摘自https://blog.csdn.net/Luoriliming/article/details/100883824的一个解题思想:简单来说就是按序遍历,记录他可以组成的正数个数和负数个数,并且在s1,s2中累加,遇到正数时只需将前一个状态继承并++正数个数即可,如果遇到负数,则需调换正数和负数个数,并++负数个数

      

      上代码把:

#include<iostream>
using namespace std;
typedef long long ll;
ll n,a=0,b=0,alla=0,allb=0;
int main()
{
    cin>>n;
    for(int i=0;i<n;i++)
    {
        ll x;
        cin>>x;
        if(x>0)
        {
            a++;
        }
        else
        {
            swap(a,b);
            b++;
        }
        alla+=a;
        allb+=b;
    }
    cout<<allb<<" "<<alla<<endl;
}

 


B. The Number of Products(Codeforces Round #585 (Div. 2))

原文:https://www.cnblogs.com/liyexin/p/11574163.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!