B. The Number of Products

给定一个非零整数序列,计算序列中乘积为负数的子段数量和乘积为正数的子段数量。题目要求O(n)时间复杂度,答案需使用long long类型存储。算法思路是按顺序遍历,维护正数和负数子段的数量,遇到正数时更新正数计数,遇到负数时交换正负数计数并增加负数计数。
摘要由CSDN通过智能技术生成

链接: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. ai≠0ai≠0).

You have to calculate two following values:

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

Input

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

The second line contains nn integers a1,a2,…,ana1,a2,…,an (−109≤ai≤109;ai≠0)(−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<

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.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值