ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2022-07-29 12:33:16  阅读:134  来源: 互联网

标签:typedef 585 const int LL Number cin 状态机 include


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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有