p170.ans1
======================
4

=================
p170.cpp
======================
#include <bits/stdc++.h>

using namespace std;

template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& x) {
    return out << x.first << ' ' << x.second;
}

template<typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& x) {
    return in >> x.first >> x.second;
}

template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for(auto& x: a) {
        in >> x;
    }
    return in;
};

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
};

string a, b;

void read() { cin >> a >> b; }

pair<vector<int>, vector<int>> get_pos_plus_minus(const string& s) {
    vector<int> pos, neg;
    for(int i = 0; i < (int)s.size(); i++) {
        if(s[i] == '+') {
            pos.push_back(i);
        } else {
            neg.push_back(i);
        }
    }
    return {pos, neg};
}

int64_t count_inversions(vector<int> a) {
    function<int64_t(vector<int>&, int, int)> merge_sort = [&](vector<int>& a,
                                                               int l, int r) {
        if(l == r) {
            return 0LL;
        }
        int m = (l + r) / 2;
        int64_t ans = merge_sort(a, l, m) + merge_sort(a, m + 1, r);
        vector<int> b;
        int i = l, j = m + 1;
        while(i <= m && j <= r) {
            if(a[i] <= a[j]) {
                b.push_back(a[i++]);
            } else {
                b.push_back(a[j++]);
                ans += m - i + 1;
            }
        }
        while(i <= m) {
            b.push_back(a[i++]);
        }
        while(j <= r) {
            b.push_back(a[j++]);
        }
        for(int i = l; i <= r; i++) {
            a[i] = b[i - l];
        }
        return ans;
    };

    return merge_sort(a, 0, a.size() - 1);
}

void solve() {
    // Adjacent swaps of unlike particles preserve the relative order of the
    // '+' particles among themselves and of the '-' particles among
    // themselves, so a transformation exists only if both strings have the
    // same count of each sign. When it does, the i-th '+' of a must become the
    // i-th '+' of b (likewise for '-'). Build the target permutation perm:
    // perm[i] is the final position (index in b) of the particle at position i
    // in a. The minimum number of adjacent swaps equals the number of
    // inversions of perm, counted with a merge sort.

    auto [pos_a, neg_a] = get_pos_plus_minus(a);
    auto [pos_b, neg_b] = get_pos_plus_minus(b);

    if(pos_a.size() != pos_b.size() || neg_a.size() != neg_b.size()) {
        cout << -1 << '\n';
        return;
    }

    reverse(pos_b.begin(), pos_b.end());
    reverse(neg_b.begin(), neg_b.end());
    vector<int> perm;
    for(int i = 0; i < (int)a.size(); i++) {
        if(a[i] == '+') {
            perm.push_back(pos_b.back());
            pos_b.pop_back();
        } else {
            perm.push_back(neg_b.back());
            neg_b.pop_back();
        }
    }

    cout << count_inversions(perm) << '\n';
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int T = 1;
    // cin >> T;
    for(int test = 1; test <= T; test++) {
        read();
        // cout << "Case #" << test << ": ";
        solve();
    }

    return 0;
}

=================
p170.in1
======================
++-+--+
-++--++

=================
statement.txt
======================
170. Particles
time limit per test: 0.25 sec.
memory limit per test: 4096 KB
input: standard
output: standard



A new kind of elementary particles was discovered in recent physical experiments. These particles were named "buons". It was also discovered that there exist two types of buons, which were named "Y+" (psi +) and "Y-" (psi -) buons. It became known that buons tend to form chains. Moreover, a very interesting property of buons, named "going through", was discovered: if two buons with different types are neighbors in a chain, they can easily exchange places one with other.

Because one "going through" takes very short time, the order of buons in chain can quickly change. Now the physicists are interested, how many "goings through" must happen for one chain to become other.

You are to help them. Write a program, which determines the minimal number of "goings through" which will allow one chain to become other.

Input
First two lines of input contains strings of "+" and "-" characters, describing first and second buon chains. The "+" character stands for Y+ buon, and "-" character stands for Y- buon. The length of each string doesn't exceed 5000; moreover, there is at leat one buon in each chain.

Output
If it is possible to make the second chain from the first, using only "goings trough", write on the first line of output the minimal number K of "goings through" which are required. If it is impossible to make the secong chain from the first, output only integer "-1".

Sample test(s)

Input
++-+--+
-++--++

Output
4
Author:	NNSU #2 team
Resource:	Compiled from many sources
Date:	








=================
