답안 #647137

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
647137 2022-10-01T16:37:34 Z Alma Ekoeko (COCI21_ekoeko) C++14
110 / 110
22 ms 5852 KB
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
using ll = long long;
using ii = pair<int,int>;

const int INF = 1e9;
const ll LLINF = 1e18;
using vi = vector<int>;
using vvi = vector<vi>;

void setIO (string fileName) {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    if (fileName != "std") {
        freopen((fileName + ".in").c_str(), "r", stdin);
        freopen((fileName + ".out").c_str(), "w", stdout);
    }
}

int n;
vi bit;

void update (int i, int val) {
	for (; i <= n; i += i&-i) {
		bit[i] += val;
	}
}

int query (int i) {
	int sum = 0;
	for (; i > 0; i -= i&-i) {
		sum += bit[i];
	}
	return sum;
}

int main() {
    setIO("std");

	cin >> n;
	n *= 2;
	string s;
	cin >> s;

	vi a(n), b, c;
	for (int i = 0; i < n; i++) {
		a[i] = s[i] - 'a';
	}
	c = a;
	sort(c.begin(), c.end());
	vi frec(26, 0);
	for (int i = 0; i < n; i += 2) {
		frec[c[i]]++;
	}
	for (int i = 0; i < n; i++) {
		if (frec[a[i]] > 0) {
			b.push_back(a[i]);
			frec[a[i]]--;
		}
	}
	vector<queue<int>> pos(26);
	for (int i = 0; i < n/2; i++) {
		b.push_back(b[i]);
		pos[b[i]].push(i);
	}
	for (int i = 0; i < n/2; i++) {
		b.push_back(b[i]);
		pos[b[i]].push(n/2 + i);
	}

	// turn a into b
	// obtain permutation -> count inversions

	vi perm(n);
	for (int i = 0; i < n; i++) {
		int idx = pos[a[i]].front();
		pos[a[i]].pop();
		perm[i] = idx+1;
	}

	// for (int i = 0; i < n; i++) {
	// 	cout << a[i] << ' ';
	// } cout << '\n';

	// for (int i = 0; i < n; i++) {
	// 	cout << b[i] << ' ';
	// } cout << '\n';

	// for (int i = 0; i < n; i++) {
	// 	cout << perm[i] << ' ';
	// } cout << '\n';

	ll ans = 0;
	bit = vi(n+1, 0);
	for (int i = n-1; i >= 0; i--) {
		ans += query(perm[i]-1);
		update(perm[i], 1);
	}

	cout << ans << '\n';

    return 0;

	/*
	aaaabbbb
	aabb
	aaababbb
	aabbaabb
	*/
}

Compilation message

Main.cpp: In function 'void setIO(std::string)':
Main.cpp:18:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   18 |         freopen((fileName + ".in").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:19:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   19 |         freopen((fileName + ".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 6 ms 2192 KB Output is correct
3 Correct 12 ms 4052 KB Output is correct
4 Correct 17 ms 5852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 6 ms 2192 KB Output is correct
3 Correct 12 ms 4052 KB Output is correct
4 Correct 17 ms 5852 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 6 ms 2192 KB Output is correct
3 Correct 12 ms 4052 KB Output is correct
4 Correct 17 ms 5852 KB Output is correct
5 Correct 1 ms 316 KB Output is correct
6 Correct 21 ms 5828 KB Output is correct
7 Correct 20 ms 5724 KB Output is correct
8 Correct 22 ms 5780 KB Output is correct
9 Correct 20 ms 5660 KB Output is correct
10 Correct 21 ms 5584 KB Output is correct
11 Correct 20 ms 5624 KB Output is correct
12 Correct 20 ms 5456 KB Output is correct
13 Correct 20 ms 5436 KB Output is correct
14 Correct 21 ms 5456 KB Output is correct
15 Correct 21 ms 5320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 324 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 320 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 6 ms 2192 KB Output is correct
3 Correct 12 ms 4052 KB Output is correct
4 Correct 17 ms 5852 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 320 KB Output is correct
9 Correct 1 ms 316 KB Output is correct
10 Correct 21 ms 5828 KB Output is correct
11 Correct 20 ms 5724 KB Output is correct
12 Correct 22 ms 5780 KB Output is correct
13 Correct 20 ms 5660 KB Output is correct
14 Correct 21 ms 5584 KB Output is correct
15 Correct 20 ms 5624 KB Output is correct
16 Correct 20 ms 5456 KB Output is correct
17 Correct 20 ms 5436 KB Output is correct
18 Correct 21 ms 5456 KB Output is correct
19 Correct 21 ms 5320 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 324 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 320 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 324 KB Output is correct
32 Correct 20 ms 5816 KB Output is correct
33 Correct 20 ms 5716 KB Output is correct
34 Correct 21 ms 5704 KB Output is correct
35 Correct 19 ms 5664 KB Output is correct
36 Correct 20 ms 5584 KB Output is correct
37 Correct 20 ms 5548 KB Output is correct
38 Correct 22 ms 5448 KB Output is correct
39 Correct 21 ms 5456 KB Output is correct
40 Correct 20 ms 5448 KB Output is correct
41 Correct 20 ms 5312 KB Output is correct