1215B - The Number of Products - CodeForces Solution


combinatorics dp implementation *1400

Please click on ads to support us..

Python Code:

import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

n = int(input())
a = list(map(int, input().split()))
b = []
for i in a:
    if i > 0:
        b.append(1)
    else:
        b.append(0)
l, r = [], []
c = 1
for i in b:
    if not i:
        l.append(c)
        c = 0
    c += 1
c = 1
for i in reversed(b):
    if not i:
        r.append(c)
        c = 0
    c += 1
r.reverse()
u = 0
m = len(l)
for i in range(2):
    s = 0
    for j in range(i, m, 2):
        s += r[j]
    for j in range(i, m, 2):
        u += l[j] * s
        s -= r[j]
ans = [u, n * (n + 1) // 2 - u]
sys.stdout.write(" ".join(map(str, ans)))

C++ Code:

#include <bits/stdc++.h>

using std::vector;

using std::string;



namespace IO {

int fasti() {

#ifdef ONLINE_JUDGE

    const int L = 1 << 21;

    static char ibuf[L], *iS, *iT;

    return iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, L, stdin), (iS == iT ? EOF : *iS++)) : *iS++;

#else

    return getchar();

#endif

}



void read(int& res) {

    res = int();

    bool flag = false;

    char ch = fasti();

    for (; !isdigit(ch); ch = fasti())

        flag |= ch == '-';

    for (; isdigit(ch); ch = fasti())

        (res *= 10) += ch - 48;

    if (flag) res = -res;

}



void read(string& s) {

    s = string();

    char ch = fasti();

    while (isspace(ch)) ch = fasti();

    for (; !isspace(ch); ch = fasti())

        s.push_back(ch);

}



template <class Header, class ...Args>

void read(Header& header, Args &...args) {

    read(header);

    read(args...);

}

}



int main() {

    int n;

    IO::read(n);

    vector<int> a(n);

    for (int i = 0; i < n; i++) {

        IO::read(a[i]);

        a[i] = a[i] < 0;

    }

    int64_t ans[] = { 0, 0 };

    int cnt[] = { 1, 0 };

    for (int i = 0, pre = 0; i < n; i++) {

        pre = pre ^ a[i];

        for (int i : { 0, 1 })

            ans[i] += cnt[pre ^ i];

        cnt[pre]++;

    }

    printf("%lld %lld\n", ans[1], ans[0]);

    return 0;

}


Comments

Submit
0 Comments
More Questions

938A - Word Correction
159C - String Manipulation 10
258A - Little Elephant and Bits
1536C - Diluc and Kaeya
1428C - ABBB
1557A - Ezzat and Two Subsequences
255A - Greg's Workout
1059A - Cashier
1389C - Good String
1561A - Simply Strange Sort
1337B - Kana and Dragon Quest game
137C - History
1443C - The Delivery Dilemma
6C - Alice Bob and Chocolate
1077C - Good Array
285B - Find Marble
6A - Triangle
1729A - Two Elevators
1729B - Decode String
1729C - Jumping on Tiles
1729E - Guess the Cycle Size
553B - Kyoya and Permutation
1729D - Friends and the Restaurant
1606C - Banknotes
580C - Kefa and Park
342A - Xenia and Divisors
1033A - King Escape
39D - Cubical Planet
1453A - Cancel the Trains
645A - Amity Assessment