其他分享
首页 > 其他分享> > Codeforces Round #585 (Div. 2) B. The Number of Products(状态机)

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

作者:互联网

https://codeforces.com/contest/1215/problem/B

给你一个序列a1,a2,…,an,由n个非零整数组成(即ai≠0)。

您必须计算以下两个值:

使得al⋅al+1…ar−1⋅ar为负的指数对(l,r) (l≤r)的个数;
使得al⋅al+1…ar−1⋅ar为正的指数对(l,r) (l≤r)的个数;

输出
打印两个整数—分别是负乘积的子段数和正乘积的子段数。
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

状压dp写法,可惜一直Compilation error
实在是不理解wa?
此写法仅供参考

//#include<bits/stdc++.h>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<string>
#include<cstdio>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<deque>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const int N=500200;
const int M=5002;
const int mod=998244353;
LL a[N],b[N];
//int h[N],w[N],e[N],ne[N],dist[N],idx;
LL f[N][2],dp[N][M];
bool vis[N],st[N];
int dx[]={-1,0,0,1,-1,-1,1,1},dy[]={0,1,-1,0,1,-1,-1,1};
LL minn=0,maxn=0;
int n,d,e;
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    int T=1;
    //cin>>T;
    while(T--)
    {
        LL n;
        cin>>n;
        for(LL i=1;i<=n;i++)
            cin>>a[i];
        LL fu=0,zh=0;
        for(LL i=1;i<=n;i++)
        {
            if(a[i]>0)
            {
                f[i][1]+=f[i-1][1]+1;//正数个数+1
                f[i][0]+=f[i-1][0];//当前负数个数不变
            }
            else
            {
                f[i][0]+=f[i-1][1]+1;//负数个数是在前面正数基础上加1
                f[i][1]+=f[i-1][0];//正数是在之前的负数的基础上负负得正来的
            }
            fu+=f[i][0];
            zh+=f[i][1];
        }
        /*for(int i=1;i<=n;i++)
            cout<<f[i][0]<<" ";
        cout<<endl;
        for(int i=1;i<=n;i++)
            cout<<f[i][1]<<" ";
        cout<<endl;*/
        cout<<fu<<" "<<zh<<endl;
    }
    return 0;
}

附上简单正解

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
/*#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<string>
#include<cstdio>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<deque>
typedef pair<int,int> PII;
const int N=200200;
const int M=5002;
const int mod=998244353;
int a[N],b[N];
//int h[N],w[N],e[N],ne[N],dist[N],idx;
int f[N][3],dp[N][M];
bool vis[N],st[N];
int dx[]={-1,0,0,1,-1,-1,1,1},dy[]={0,1,-1,0,1,-1,-1,1};*/
int main()
{
    //cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    int T=1;
    //cin>>T;
    while(T--)
    {
        LL n;
        cin>>n;
        LL fu=0,zh=0;
        LL num=0,sum=0;
        for(int i=1;i<=n;i++)
        {
            LL x;
            cin>>x;
            num++;
            if(x<0) swap(num,sum);
            zh+=num;
            fu+=sum;
        }
        cout<<fu<<" "<<zh<<endl;
    }
    return 0;
}

标签:typedef,585,const,int,LL,Number,cin,状态机,include
来源: https://www.cnblogs.com/Vivian-0918/p/16531847.html