Submission #961020

# Submission time Handle Problem Language Result Execution time Memory
961020 2024-04-11T11:56:27 Z Pety Ekoeko (COCI21_ekoeko) C++14
110 / 110
14 ms 3656 KB
#include <bits/stdc++.h>
 
using namespace std;
 
const int N = 1e6+2;
const int mod = 1e9 + 7;

string s;
int n, fr[2][26], total[26], plm[200002];

struct AIB {
  int n;
  vector<int>aib;
  AIB(int N): n(N), aib(N+2) {}
  void update (int x, int val) {
    for (int i = x; i <= n; i += (i & -i))
      aib[i] += val;
  }
  int query (int x) {
    int s = 0;
    for (int i = x; i; i -= (i & -i))
      s += aib[i];
    return s;
  }
};
using ll = long long;

ll CountSteps(string s1, string s2, int size)
{
    AIB pom(size);
    deque<int>poz[26];
    for (int i = 0; i < size; i++)
      poz[s1[i] - 'a'].push_back(i);
    ll ans = 0;
    for (int i = 0; i < size; i++) {
      int k = poz[s2[i] - 'a'].front();
      poz[s2[i] - 'a'].pop_front();
      ans += pom.query(size - k);
      pom.update(size - k, 1);
    }
    return ans;
}
 

int main () 
{
  ios_base::sync_with_stdio(false);
  cin.tie(0); cout.tie(0);
  cin >> n;
  cin >> s;
  for (int i = 0; i < 2 * n; i++) {
    fr[i / n][s[i] - 'a']++;
  }
  for (int i = 0; i < 26; i++)
    total[i] = fr[0][i] + fr[1][i];
  deque<char>q1, q2;
  int stop = n;
  int ans = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (fr[0][s[i] - 'a'] > total[s[i] - 'a'] / 2) {
      ans += stop - i;
      stop--;
      q1.push_front(s[i]);
      plm[i] = '?';
      fr[0][s[i] - 'a']--;
    }
  }
  stop = n-1;
  for (int i = n; i < 2 * n; i++) {
    if (fr[1][s[i] - 'a'] > total[s[i] - 'a'] / 2) {
      q2.push_back(s[i]);
      ans += i - stop;
      stop++;
      plm[i] = '?';
      fr[1][s[i] - 'a']--;
    }
  }
  string a, b;
  for (int i = 0; i < n; i++)
    if (plm[i] != '?')
      a += s[i];  
  for (auto it : q2) a += it;
  for (auto it : q1) b += it;
  for (int i = n; i < 2 * n; i++)
     if (plm[i] != '?')
      b += s[i];
  //cout << a << " " << b << "\n";
  cout << CountSteps(a, b, n) + CountSteps(s, a + b, 2 * n);
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 3 ms 1496 KB Output is correct
3 Correct 7 ms 2412 KB Output is correct
4 Correct 11 ms 3656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 3 ms 1496 KB Output is correct
3 Correct 7 ms 2412 KB Output is correct
4 Correct 11 ms 3656 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 472 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 3 ms 1496 KB Output is correct
3 Correct 7 ms 2412 KB Output is correct
4 Correct 11 ms 3656 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 11 ms 3260 KB Output is correct
7 Correct 11 ms 3244 KB Output is correct
8 Correct 14 ms 3052 KB Output is correct
9 Correct 11 ms 3180 KB Output is correct
10 Correct 11 ms 3208 KB Output is correct
11 Correct 11 ms 3188 KB Output is correct
12 Correct 11 ms 3152 KB Output is correct
13 Correct 10 ms 2940 KB Output is correct
14 Correct 10 ms 2944 KB Output is correct
15 Correct 12 ms 2948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 3 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 3 ms 1496 KB Output is correct
3 Correct 7 ms 2412 KB Output is correct
4 Correct 11 ms 3656 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 472 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 11 ms 3260 KB Output is correct
11 Correct 11 ms 3244 KB Output is correct
12 Correct 14 ms 3052 KB Output is correct
13 Correct 11 ms 3180 KB Output is correct
14 Correct 11 ms 3208 KB Output is correct
15 Correct 11 ms 3188 KB Output is correct
16 Correct 11 ms 3152 KB Output is correct
17 Correct 10 ms 2940 KB Output is correct
18 Correct 10 ms 2944 KB Output is correct
19 Correct 12 ms 2948 KB Output is correct
20 Correct 1 ms 600 KB Output is correct
21 Correct 3 ms 344 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 468 KB Output is correct
32 Correct 11 ms 3168 KB Output is correct
33 Correct 13 ms 3432 KB Output is correct
34 Correct 11 ms 3176 KB Output is correct
35 Correct 11 ms 3432 KB Output is correct
36 Correct 11 ms 3180 KB Output is correct
37 Correct 11 ms 3184 KB Output is correct
38 Correct 11 ms 2932 KB Output is correct
39 Correct 10 ms 3048 KB Output is correct
40 Correct 13 ms 3200 KB Output is correct
41 Correct 10 ms 2948 KB Output is correct