Codeforces Round #585 (Div. 2) B. The Number of Products(DP)
2024-09-03 04:59:00
链接:
https://codeforces.com/contest/1215/problem/B
题意:
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;
思路:
DP[i][0]为i位置往前连续的负数, DP[i][1] 为偶数.
最后累计一下.
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 2e5+10;
int a[MAXN];
LL Dp[MAXN][2];
int n;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 1;i <= n;i++)
cin >> a[i];
if (a[1] > 0)
Dp[1][1] = 1;
else
Dp[1][0] = 1;
for (int i = 2;i <= n;i++)
{
if (a[i] > 0)
{
Dp[i][1]++;
Dp[i][0] += Dp[i-1][0];
Dp[i][1] += Dp[i-1][1];
}
else
{
Dp[i][0]++;
Dp[i][0] += Dp[i-1][1];
Dp[i][1] += Dp[i-1][0];
}
}
LL sum1 = 0, sum2 = 0;
for (int i = 1;i <= n;i++)
sum1 += Dp[i][0], sum2 += Dp[i][1];
cout << sum1 << ' ' << sum2 << endl;
return 0;
}
最新文章
- js/javascript代码注释规范与示例
- html5之canvas画图基础
- OPENGL若干重要基础概念
- phpMailer在thinkPHP框架中邮件发送
- 【BZOJ】2237: [NCPC2009]Flight Planning
- Java List 如何传值
- (委托事件处理)关于多线程执行显示进度条的实例(转)&;&;线程间操作无效: 从不是创建控件“rtxtEntryNO”的线程访问它。
- Skyline中使用AxTE3DWindowEx打开新的一个球体
- .htaccess和license文件编写
- ThinkPHP中对系统常量的使用
- Android艺术开发探索第四章——View的工作原理(下)
- Java异常处理-----自行处理
- css3图片垂直居中
- python插入记录后取得主键id的方法(cursor.lastrowid和conn.insert_id())
- 3.《想成为黑客,不知道这些命令行可不行》(Learn Enough Command Line to Be Dangerous)——检查文件
- octave基本指令4
- LL(1)文法分析表的构造和分析过程示例
- MySQL 跨库主从
- 【HDOJ1043】【康拓展开+BFS】
- Warning:Configuration &#39;compile&#39; is obsolete and has been replaced with &#39;implementation&#39;. It will be