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

题目地址:http://codeforces.com/contest/1215/problem/B

题意:给你一个值都不为零的数组,分别找出有多少个连续的子串乘积小于零,大于零。

我的思路:先找出为正数的基础子串x个,即单个正数或两个负数连起来的子串算一个,可算得。。。算了一直wrong在样例7.还没想出来那错了,就不写了。

别人家的思路:从首开始找负数字串,即有一个负数后,在下一个负数前这里面的都是负数子串。而其他的就为正数子串,正数子串初始化应为1,所以得的正数子串最后应加上1。两个负数作为边上的元素的子串也可看为一个正数子串,负数子串和正数子串的乘积即为子串乘积为负数的数量。这里面看似有重复(我也想了挺久的),其实并没有,例如 ”-1,2,3,-4“这个数组,它的负数子串为3,正数子串为2,3×2=3,这个2,一个是-1,一个是-4。多成了一个自己等于算上后面的负数。负数个数出来了,用总数n×(n+1)/2减去负数个数便是正数的个数。

AC代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 typedef long long ll;
 6 int main(){
 7     ll n;
 8     cin>>n;
 9     ll a,num1=0,num2=1,t=1;
10         //num1记录负子串个数,num2记录正子串个数,t表判断
11     for(ll i=0;i<n;i++){
12         cin>>a;
13         if(a<0) t*=-1;
14         if(t>0) num2++;
15         else num1++;
16     }
17     cout<<num1*num2<<" "<<n*(n+1)/2-num1*num2<<endl;
18     return 0;
19 }

原文地址:https://www.cnblogs.com/xunzf0402/p/11529938.html

时间: 2024-08-30 06:27:03

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

Codeforces Round #585 (Div. 2) CF1215A~C

CF1215A. Yellow Cards简单的模拟,给定了黄票张数,判断最少和最多有多少人被罚下场. #include <bits/stdc++.h> using namespace std; int main() { int a,b,aa,bb,n,nnn; cin>>a>>b>>aa>>bb>>n; nnn=n; int t=(aa-1)*a+(bb-1)*b; int tt=0; if(aa<=bb) { if(n>

Codeforces Round #585 (Div. 2) D. Ticket Game

链接: https://codeforces.com/contest/1215/problem/D 题意: Monocarp and Bicarp live in Berland, where every bus ticket consists of n digits (n is an even number). During the evening walk Monocarp and Bicarp found a ticket where some of the digits have bee

Codeforces Round #411 (Div. 2)D. Minimum number of steps(贪心)

传送门 Description We have a string of letters 'a' and 'b'. We want to perform some operations on it. On each step we choose one of substrings "ab" in the string and replace it with the string "bba". If we have no "ab" as a subs

Codeforces Round #480 (Div. 2) E. The Number Games

E. The Number Games time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output The nation of Panel holds an annual show called The Number Games, where each district in the nation will be represented

Codeforces Round #491 (Div. 2) E - Bus Number + 反思

E - Bus Number 最近感觉打CF各种车祸.....感觉要反思一下, 上次读错题,这次想当然地以为18!肯定暴了longlong 而没有去实践, 这个题我看到就感觉是枚举每个数字的个数,但是我觉得算得时候会爆longlong 就想用大数,但是我去看别人交的全部都是C++,就感觉是不是有别的方法, 想了半天感觉时间来不及了就强行上了个java,结果时间来不及... 以后写题首先要读清楚题目,其次不要想当然,要去实践!!!!!!!!!!! 真的很烦. import java.math.Bi

Codeforces Round #460 (Div. 2) B Perfect Number(二分+数位dp)

题目传送门 B. Perfect Number time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output We consider a positive integer perfect, if and only if the sum of its digits is exactly 1010. Given a positive integ

CodeForces Round #585 (Div 2)

score:1336 (-45), pupilRank: 3313(又炸了,没啥好说的..) B.http://codeforces.com/contest/1215/problem/B 这个题万万没想到是dp,我的天,一开始直接上暴力,没仔细分析时间复杂度,估计是最近网络赛暴力惯了,没缓过神来... 分析见代码: 1 /* 2 设pos[i]表示以i为结尾的正区间,neg[i]表示以i为结尾的负区间.状态转移很容易了,见代码 3 这次真的wa到自闭了,后来改了改竟然又tle,真的自闭到家...

Codeforces Round #209 (Div. 2)——Prime Number

MySQL使用的是插件式存储引擎. 主要包括存储引擎有:MyISAM,Innodb,NDB Cluster,Maria,Falcon,Memory,Archive,Merge,Federated. 其中最为广泛的是MyISAM 和Innodb两种存储引擎,所以接下来对它们做简单介绍. MyISAM 存储引擎简介 MyISAM 存储引擎的表存储在数据库中,每一个表都被存放为三个以表名命名的物理文件. 1.(.frm文件)任何存储引擎都不可缺少的存放表结构定义信息的文件 2.(.MYD文件)存放表数

数学+DP Codeforces Round #304 (Div. 2) D. Soldier and Number Game

题目传送门 1 /* 2 题意:这题就是求b+1到a的因子个数和. 3 数学+DP:a[i]保存i的最小因子,dp[i] = dp[i/a[i]] +1;再来一个前缀和 4 */ 5 /************************************************ 6 Author :Running_Time 7 Created Time :2015-8-1 14:08:34 8 File Name :B.cpp 9 ******************************