本题地址:  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. 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

Copy
  1. 5
  2. 5 -3 3 -1 1
output

Copy
  1. 8 7
input

Copy
  1. 10
  2. 4 2 -4 3 1 2 -4 3 2 3
output

Copy
  1. 28 27
input

Copy 
  1. 5
  2. -1 -2 -3 -4 -5
output

Copy
  1. 9 6
  2.  
  3.   题意:给你n长度的数组。分别求出任意长度连续区间内乘积为负数和正数的个数。   
          暴力会超时,int 会炸。需要O(n)才能过。
          这需要一个继承的思想。
          看这么一组: 1  2  3    令a=0此为正数个数,sum为区间乘积为正数个数。读入1,a++,sum+=aa=1,sum=1;
                                                  读入2,a++,sum+=aa=2,sum=3;
                                                 读入3a++,sum+=aa=3,sum=6;
               这个sum累加的过程符合求区间个数的过程。 
         
  1. ll n,a=,b=,alla=,allb=;    //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中累加,遇到正数时只需将前一个状态继承并++正数个数即可,如果遇到负数,则需调换正数和负数个数,并++负数个数

      

      上代码把:

  1. #include<iostream>
  2. using namespace std;
  3. typedef long long ll;
  4. ll n,a=,b=,alla=,allb=;
  5. int main()
  6. {
  7. cin>>n;
  8. for(int i=;i<n;i++)
  9. {
  10. ll x;
  11. cin>>x;
  12. if(x>)
  13. {
  14. a++;
  15. }
  16. else
  17. {
  18. swap(a,b);
  19. b++;
  20. }
  21. alla+=a;
  22. allb+=b;
  23. }
  24. cout<<allb<<" "<<alla<<endl;
  25. }

  C题题解:

                         C. Everyone is a Winner!

  

On the well-known testing system MathForces, a draw of nn rating units is arranged. The rating will be distributed according to the following algorithm: if kk participants take part in this event, then the nn rating is evenly distributed between them and rounded to the nearest lower integer, At the end of the drawing, an unused rating may remain — it is not given to any of the participants.

For example, if n=5n=5 and k=3k=3, then each participant will recieve an 11 rating unit, and also 22 rating units will remain unused. If n=5n=5, and k=6k=6, then none of the participants will increase their rating.

Vasya participates in this rating draw but does not have information on the total number of participants in this event. Therefore, he wants to know what different values of the rating increment are possible to get as a result of this draw and asks you for help.

For example, if n=5n=5, then the answer is equal to the sequence 0,1,2,50,1,2,5. Each of the sequence values (and only them) can be obtained as ⌊n/k⌋⌊n/k⌋ for some positive integer kk (where ⌊x⌋⌊x⌋ is the value of xx rounded down): 0=⌊5/7⌋0=⌊5/7⌋, 1=⌊5/5⌋1=⌊5/5⌋, 2=⌊5/2⌋2=⌊5/2⌋, 5=⌊5/1⌋5=⌊5/1⌋.

Write a program that, for a given nn, finds a sequence of all possible rating increments.

Input

The first line contains integer number tt (1≤t≤101≤t≤10) — the number of test cases in the input. Then tt test cases follow.

Each line contains an integer nn (1≤n≤1091≤n≤109) — the total number of the rating units being drawn.

Output

Output the answers for each of tt test cases. Each answer should be contained in two lines.

In the first line print a single integer mm — the number of different rating increment values that Vasya can get.

In the following line print mm integers in ascending order — the values of possible rating increments.

Example
input

Copy
  1. 4
  2. 5
  3. 11
  4. 1
  5. 3
output

Copy
  1. 4
  2. 0 1 2 5
  3. 6
  4. 0 1 2 3 5 11
  5. 2
  6. 0 1
  7. 3
  8. 0 1 3
        
  1.     题意:就是给出n,从 k =1 一直分到k=n+1 ,即b/k
        比如样例1 n =5
              k=   1  2  3  4  5  6
                   5 2 1 1  1  0
            所以输出0  1  2  5
  1. #include<iostream>
  2. using namespace std;
  3. typedef long long ll;
  4. ll n,a=,b=,alla=,allb=;
  5. int main()
  6. {
  7. cin>>n;
  8. for(int i=;i<n;i++)
  9. {
  10. ll x;
  11. cin>>x;
  12. if(x>)
  13. {
  14. a++;
  15. }
  16. else
  17. {
  18. swap(a,b);
  19. b++;
  20. }
  21. alla+=a;
  22. allb+=b;
  23. }
  24. cout<<allb<<" "<<alla<<endl;
  25. }
  1.  
  1.  

B. The Number of Products(Codeforces Round #585 (Div. 2))的更多相关文章

  1. 【CodeForces】841D. Leha and another game about graph(Codeforces Round #429 (Div. 2))

    [题意]给定n个点和m条无向边(有重边无自环),每个点有权值di=-1,0,1,要求仅保留一些边使得所有点i满足:di=-1或degree%2=di,输出任意方案. [算法]数学+搜索 [题解] 最关 ...

  2. B. Ohana Cleans Up(Codeforces Round #309 (Div. 2))

    B. Ohana Cleans Up   Ohana Matsumae is trying to clean a room, which is divided up into an n by n gr ...

  3. Vus the Cossack and Strings(Codeforces Round #571 (Div. 2))(大佬的位运算实在是太强了!)

    C. Vus the Cossack and Strings Vus the Cossack has two binary strings, that is, strings that consist ...

  4. A. Kyoya and Photobooks(Codeforces Round #309 (Div. 2))

    A. Kyoya and Photobooks   Kyoya Ootori is selling photobooks of the Ouran High School Host Club. He ...

  5. 【CodeForces】841C. Leha and Function(Codeforces Round #429 (Div. 2))

    [题意]定义函数F(n,k)为1~n的集合中选择k个数字,其中最小数字的期望. 给定两个数字集A,B,A中任意数字>=B中任意数字,要求重组A使得对于i=1~n,sigma(F(Ai,Bi))最 ...

  6. A. Ilya and Diplomas( Codeforces Round #311 (Div. 2) )

    A. Ilya and Diplomas time limit per test 1 second memory limit per test 256 megabytes input standard ...

  7. D. Zero Quantity Maximization ( Codeforces Round #544 (Div. 3) )

    题目链接 参考题解 题意: 给你 整形数组a 和 整形数组b ,要你c[i] = d * a[i] + b[i], 求  在c[i]=0的时候  相同的d的数量 最多能有几个. 思路: 1. 首先打开 ...

  8. CodeForces 360E Levko and Game(Codeforces Round #210 (Div. 1))

    题意:有一些无向边m条权值是给定的k条权值在[l,r]区间可以由你来定,一个点s1 出发一个从s2出发  问s1 出发的能不能先打到f 思路:最短路. 首先检测能不能赢 在更新的时候  如果对于一条边 ...

  9. 贪心+构造( Codeforces Round #344 (Div. 2))

    题目:Report 题意:有两种操作: 1)t = 1,前r个数字按升序排列:   2)t = 2,前r个数字按降序排列: 求执行m次操作后的排列顺序. #include <iostream&g ...

随机推荐

  1. 「BJWC2012」冻结

    传送门 Luogu 解题思路 分层图最短路,层与层之间的边的边权减半,然后就是板子了. 细节注意事项 咕咕咕. 参考代码 #include <algorithm> #include < ...

  2. php 实现店铺装修4

    /** * @title 发布装修的店铺 * @example FlagShipShopDecorate.fabu? 调试参数:{"username":"17721355 ...

  3. 修改Xshell字体大小和颜色

    博客专区 > XManager的博客 > 博客详情 修改Xshell字体大小和颜色 XManager 发表于7个月前 分享到: 一键分享 QQ空间 微信 腾讯微博 新浪微博 QQ好友 有道 ...

  4. jenkins构建python项目时,提示python不是内部或外部命令的解决办法

    1.回到 Jenkins 首页,点击 “构建执行状态”或“Build Executor Status” ,右则会列出本机信息. 完美解决!!!

  5. js求两个整数的百分比

    function GetPercent(num, total) {                          num = parseFloat(num);                   ...

  6. java 解析json格式数据(转)

    2012-07-30 16:43:54|  分类: java |  标签:java  json  |举报|字号 订阅     有时候我们可能会用到json格式的数据进行数据的传输,那么我们怎么把接收到 ...

  7. 闲谈“如何优化SSH框架的项目”

    使用struts框架的好处之一就是所有action类继承一个基类,将访问控制在基类中处理.2.所有的action类都继承自baseaction,一个资源对应一个action类.1.实现一个继承自str ...

  8. VScode禁用alt+key触发菜单栏快捷键

    因为用惯了Mac,突然改回Windows,但是已经习惯了按Command键.所以在Windows下把vscode的快捷键全改成alt+key了.但是Windows的alt+key快捷键就比较烦人了.所 ...

  9. iOS Burp suite CA证书 HTTPS

    设置好burp suite代理后,在浏览器地址输入http://burp/,下载CA证书: 在iOS上下载CA证书,可通过邮件或百度云等一切iOS可以访问证书文件的方法: 点击证书文件iOS提示安装, ...

  10. 【转】WdatePicker日历控件使用方法

    转                自:   https://www.cnblogs.com/yuhanzhong/archive/2011/08/10/2133276.html WdatePicke官 ...