B.The Number of Products

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

You have to calculate two following values:

the number of pairs of indices (l,r) (l≤r) such that al⋅al+1…ar−1⋅ar is negative;
the number of pairs of indices (l,r) (l≤r) such that al⋅al+1…ar−1⋅ar is positive;
Input
The first line contains one integer n (1≤n≤2⋅105) — the number of elements in the sequence.

The second line contains n integers a1,a2,…,an (−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.

给你一个序列a1,a2,…,an一个由n个非零整数组成的序列(即a i≠0)。
必须计算以下两个值:
指数对(L,R)(L≤R)的数目,使得al⋅al+1…ar−1⋅ar为负;
指数对(L,R)(L≤R)的数目,使得al⋅al+1…ar−1⋅ar为正;
输入:
第一行包含一个整数n (1≤n≤2 *10 5)序列中的元素数。
第二行包含n个整数a1,a2,…,an(-10 9≤ai≤10 9;ai不等于0)序列的元素。
输出:
分别打印两个整数-负积的子段数和正积的子段数。
Examples
input

5
5 -3 3 -1 1

output

8 7

input

10
4 2 -4 3 1 2 -4 3 2 3

output

28 27

input

5
-1 -2 -3 -4 -5

output

9 6

分析:
如果全为正数,则子段数全为正积,个数: 1+2+3+…+(n-1)+n;
所以可以将数组从头遍历,然后累加。
如果在遍历正数时遇到了负数,则子段数的积为负,个数为 i 。将负数个数与正数个数互换,然后累加负数个数。
如果再遇到负数,则字段数的积为正,个数为 i 。将正数个数与负数个数互换,然后再累加正数个数。
负数中间子段数的积也为正,所以还是需要累加正数个数。

code:

#include <iostream>

using namespace std;

int main()
{
    int n;
    while(cin>>n)
    {
        long long x,ne=0,po=0,ren=0,rep=0;
        while(n--)
        {
            cin>>x;
            if(x>0)
                po++;
            else
            {
                x=po;
                po=ne;
                ne=x;
                ne++;
            }
            rep+=po;
            ren+=ne;
        }
        cout<<ren<<" "<<rep<<endl;
    }
    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Sure! Here is an example implementation of the Producer class that meets your requirements: ```java import java.util.Random; public class Producer implements Runnable { private int sizeOfJobs; private int numberOfJobs; private int delayBetweenJobs; private String producerName; private PrinterManager printerManager; public Producer(int sizeOfJobs, int numberOfJobs, int delayBetweenJobs, String producerName, PrinterManager printerManager) { this.sizeOfJobs = sizeOfJobs; this.numberOfJobs = numberOfJobs; this.delayBetweenJobs = delayBetweenJobs; this.producerName = producerName; this.printerManager = printerManager; } @Override public void run() { Random rand = new Random(); for (int i = 1; i <= numberOfJobs; i++) { PrintJob job = new PrintJob(producerName + " Job #" + i, rand.nextInt(sizeOfJobs) + 1); try { printerManager.addJob(job); System.out.println(producerName + " is adding a job to the print queue."); } catch (FullQueueException e) { System.out.println(producerName + " could not add a job to the print queue: " + e.getMessage()); } try { Thread.sleep(delayBetweenJobs); } catch (InterruptedException e) { System.out.println(producerName + " was interrupted while waiting to add a job to the print queue."); } } } } ``` This class takes in four parameters in its constructor: `sizeOfJobs`, `numberOfJobs`, `delayBetweenJobs`, and `producerName`. These correspond to the instance variables you mentioned in your question. It also takes in a `PrinterManager` object, which is used to add jobs to the print queue. The `run` method generates `numberOfJobs` print jobs, with names based on the `producerName` and the job number. The number of pages in each job is randomly generated between 1 and `sizeOfJobs`. The job is added to the printer manager via the `addJob` method, which might throw a `FullQueueException`. If this occurs, the method catches the exception and prints out a message indicating that the job could not be added to the print queue. Between jobs, the producer sleeps for `delayBetweenJobs` milliseconds. I hope this helps! Let me know if you have any further questions.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

为君倾此杯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值