The Number of Products
The Number of Products
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:
- 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;
- 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
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
题解及分析
AC代码:
#include<stdio.h>
#include<string.h>
//R[i]表示以i为结尾的正子列的个数,L[i]表示以i为结尾的负子列的个数
long long int n,a[200005],R[200005],L[200005],ansR=0,ansL=0;
int main()
{scanf("%lld",&n);for(long long int i=1;i<=n;i++){scanf("%lld",&a[i]);//将a[i]按照是否为负数分类,便于后面进行遍历if(a[i]<0) a[i]=1;else a[i]=0;}R[0]=0;L[0]=0;for(int i=1;i<=n;i++){if(a[i])//递推关系{L[i]=R[i-1]+1;R[i]=L[i-1];}else {R[i]=R[i-1]+1;L[i]=L[i-1];}ansR+=R[i];//ansR表示前i个数组成的数列一共有ansR个正子列ansL+=L[i];//ansL表示前i个数组成的数列一共有ansL个负子列}printf("%lld %lld",ansL,ansR);return 0;
}
The Number of Products相关推荐
- 1215B. The Number of Products
B. The Number of Products:题目 #include <bits/stdc++.h> using namespace std; #define int long lo ...
- K - The Number of Products
K - The Number of Products 题目入口:K - The Number of Products Codeforces:B. The Number of Products 参考网站 ...
- Magento 产品推荐 extension Featured products 2.0 – revamped!
Platform: Community Platform: Community Platform: Enterprise Compatible with: 1.6, 1.6.1, 1.6.2.0, 1 ...
- magento创建自定义页面 (Bestseller实例) Bestseller products in Magento
Bestseller or best selling product is one of the features people tend to ask for when it comes to Ma ...
- Prevent Crow's Feet
来源:http://www.howbeauty.org/anti-aging/223424.htm Wear a sunscreen with an SPF of at least fifteen t ...
- 独家 | 探索性文本数据分析的新手教程(Amazon案例研究)
作者:Abhishek Sharma 翻译:李嘉骐 校对:方星轩 本文长度为5500字,建议阅读10+分钟 本文利用Python对Amazon产品的反馈对数据文本进行探索性研究与分析,并给出结论. 标 ...
- 剑指offer:剪绳子
题目描述 给你一根长度为n的绳子,请把绳子剪成m段(m.n都是整数,n>1并且m>1),每段绳子的长度记为k[0],k[1],-,k[m].请问k[0]xk[1]x-xk[m]可能的最大乘 ...
- Master Data makes SAP Retail special
Master Data makes SAP Retail special It's a self-serving notion with the added benefit of being true ...
- 使用Bash编写Linux Shell脚本-7.复合命令
转载自:http://blog.csdn.net/fox_lht/archive/2010/09/20/5897336.aspx 7. 复合命令 除了最简单的脚本,你很少想要执行每一个命令.执行一 ...
最新文章
- ubuntu18.04下双机驱动调试
- safari快捷图标不见了_Win7桌面图标不见了怎么办?附解决办法
- GTD (Getting Things Done)时间管理 提高效率 简介
- AI和大数据下,前端技术将如何发展?
- cocos2d-x游戏实例(3)-获得地图索引
- 后端:Java中如何更优雅的处理空值,看完你就懂了!
- 跟angular2学一键开启项目--关于上个react-redux项目的一键调试
- 小知识:vue中的name的作用
- 微软有“病”,推出bing,看上去算象个站内搜索,心寒!
- java 男女 相邻交换 队形_(Java实现) 洛谷 P1091合唱队形
- sentaurus学习日记(二)--器件特性提取
- “飞扬小字典” -- 辅助记忆的小工具
- Jenkins插件Gerrit Trigger配置,实现change-merged时自动触发Jenkins工程build
- 使用Stellarium Web跟踪浏览器中的星星
- 日程安排(多重继承+重载)
- Wannafly挑战赛26 御坂网络
- STM32F411RET6的定时器和STM32F103ZET6定时器比较
- java:如何解决汉字在记事本中编译翻译后出现乱码
- 狭义的Infra到广义的Infra_看看AWS的发展
- persevere的用法_persevere是什么意思_persevere的翻译_音标_读音_用法_例句_爱词霸在线词典...
热门文章
- PS:oracle恢复删除的数据
- .net快速创建PDF文档 by c#
- 测试培训大纲第一课时,软件测试基础(培训待续中....)
- ios 调用webservice 辅助类
- Python read()/pack()/try
- 定期定量采购_采购的四种方法
- swing怎么监听其他类的按钮_舍得酒因资金违规占用被ST,“其他类风险”爆雷到底怎么防?...
- dfs时间复杂度_两道有意思的时间复杂度计算
- 任秀计算机音乐,重磅丨15家主流媒体聚焦全球音乐院校校长交流季
- python绘制四边螺旋线代_Python绘制3d螺旋曲线图实例代码