Submission #19540704


Source Code Expand

Copy
#include <bits/stdc++.h>

using namespace std;

template <class T> 
class FenwickTree {
    int N;
    vector<T> v;

    public:
    FenwickTree(int N) : N(N), v(N) {}

    void add(int p, T val) {
        for(int i = p; i < N; i += (i & -i)) {
            v[i] += val;
        }
    }

    T query(int p) {
        T sum = 0;
        for(int i = p; i >= 1; i -= (i & -i)) {
            sum += v[i];
        }
        return sum;
    }
};

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

	string s;
	cin >> s;

	int n = s.size();

	vector<vector<int> > p(26);
	for (int i = 0; i < n; ++i) {
		p[s[i] - 'a'].emplace_back(i);
	}

	int cnt = 0;
	for (int i = 0; i < 26; ++i) {
		cnt += (int)p[i].size() & 1;
	}

	if (cnt > (n & 1)) {
		cout << -1 << '\n';
		exit(0);
	}

	vector<pair<int, int> > seg;
	for (int i = 0; i < 26; ++i) {
		int m = p[i].size();
		for (int j = 0; j < m && m - j - 1 >= j; ++j) {
			seg.emplace_back(p[i][j], p[i][m - j - 1]);	
		}
	}

	FenwickTree<int> fw(n + 1);
	long long ans = 0;
	for (auto [x, y] : seg) {
		if (x == y) {
			fw.add(y + 1, 1);
		} else {
			fw.add(y + 1, 2);	
		}
	}

	for (auto [x, y] : seg) {
		if (x == y) {
			ans += fw.query(x) / 2; 
		} else {
			ans += fw.query(x);
		}
	}

	for (auto [x, y] : seg) {
		if (x == y) {
			fw.add(y + 1, -1);
		} else {
			fw.add(y + 1, -2);
		}
	}

	sort(seg.begin(), seg.end(), [&] (auto x, auto y) {
		return x.second < y.second;
	});

	for (auto [x, y] : seg) {
		ans += fw.query(y) - fw.query(x);
		fw.add(y + 1, 1);
		fw.add(x + 1, -1);
	}

	cout << ans << '\n';

	return 0;
}

Submission Info

Submission Time
Task E - Papple Sort
User I9992147
Language C++ (GCC 9.2.1)
Score 800
Code Size 1679 Byte
Status AC
Exec Time 33 ms
Memory 6324 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 800 / 800
Status
AC × 3
AC × 50
Set Name Test Cases
Sample s1.txt, s2.txt, s3.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, 39.txt, 40.txt, 41.txt, 42.txt, 43.txt, 44.txt, 45.txt, 46.txt, 47.txt, s1.txt, s2.txt, s3.txt
Case Name Status Exec Time Memory
01.txt AC 23 ms 6032 KB
02.txt AC 25 ms 6004 KB
03.txt AC 23 ms 6096 KB
04.txt AC 24 ms 6108 KB
05.txt AC 26 ms 6080 KB
06.txt AC 23 ms 6136 KB
07.txt AC 24 ms 6220 KB
08.txt AC 30 ms 6072 KB
09.txt AC 30 ms 6052 KB
10.txt AC 22 ms 6028 KB
11.txt AC 6 ms 4684 KB
12.txt AC 5 ms 4620 KB
13.txt AC 4 ms 4756 KB
14.txt AC 9 ms 4676 KB
15.txt AC 27 ms 5932 KB
16.txt AC 24 ms 5896 KB
17.txt AC 24 ms 5900 KB
18.txt AC 29 ms 5860 KB
19.txt AC 29 ms 5936 KB
20.txt AC 6 ms 4700 KB
21.txt AC 33 ms 5932 KB
22.txt AC 31 ms 5900 KB
23.txt AC 27 ms 5944 KB
24.txt AC 6 ms 4624 KB
25.txt AC 29 ms 6000 KB
26.txt AC 25 ms 5860 KB
27.txt AC 29 ms 5944 KB
28.txt AC 26 ms 5912 KB
29.txt AC 24 ms 6016 KB
30.txt AC 5 ms 4748 KB
31.txt AC 23 ms 6000 KB
32.txt AC 6 ms 4588 KB
33.txt AC 29 ms 6316 KB
34.txt AC 5 ms 4680 KB
35.txt AC 21 ms 6320 KB
36.txt AC 9 ms 4684 KB
37.txt AC 5 ms 4752 KB
38.txt AC 6 ms 4760 KB
39.txt AC 26 ms 6324 KB
40.txt AC 5 ms 4668 KB
41.txt AC 2 ms 3500 KB
42.txt AC 2 ms 3552 KB
43.txt AC 7 ms 3584 KB
44.txt AC 3 ms 3516 KB
45.txt AC 3 ms 3480 KB
46.txt AC 3 ms 3600 KB
47.txt AC 4 ms 3512 KB
s1.txt AC 3 ms 3564 KB
s2.txt AC 2 ms 3496 KB
s3.txt AC 2 ms 3560 KB