Submission #767778

# Submission time Handle Problem Language Result Execution time Memory
767778 2023-06-27T07:27:07 Z dxz05 Ekoeko (COCI21_ekoeko) C++17
0 / 110
2 ms 1236 KB
#include <bits/stdc++.h>

using namespace std;

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define bpc(x) __builtin_popcount(x)
#define bpcll(x) __builtin_popcountll(x)
#define MP make_pair
#define BIT(x, i) (((x) >> (i)) & 1)
//#define endl '\n'

mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

typedef long long ll;
const int MOD = 1e9 + 7;
const int N = 1e6 + 3e2;

ll calc(const string &s, const string &t){
    int n = (int) s.size();

    vector<queue<int>> q(26);
    for (int i = 0; i < n; i++) q[s[i] - 'a'].push(i);

    vector<int> a(n);
    for (int i = 0; i < n; i++){
        a[i] = q[t[i] - 'a'].front();
        q[t[i] - 'a'].pop();
    }

    ll inv = 0;

    vector<int> f(n + 1);
    for (int i = n - 1; i >= 0; i--){
        int x = a[i] + 1;

        while (x > 0) inv += f[x], x -= (-x) & x;

        x = a[i] + 1;
        while (x <= n) f[x]++, x += (-x) & x;
    }

    return inv;
}

void solve(){
    int n; string s;
    cin >> n >> s;

    string t = s.substr(n, n);
    s.erase(s.begin() + n, s.end());

    cout << calc(s, t) << endl;

}

int main(){
    clock_t startTime = clock();
    ios_base::sync_with_stdio(false);

#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    int test_cases = 1;
//    cin >> test_cases;

    for (int test = 1; test <= test_cases; test++){
        // cout << (solve() ? "YES" : "NO") << endl;
        solve();
    }

#ifdef LOCAL
    cerr << "Time: " << int((double) (clock() - startTime) / CLOCKS_PER_SEC * 1000) << " ms" << endl;
#endif

    return 0;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:58:13: warning: unused variable 'startTime' [-Wunused-variable]
   58 |     clock_t startTime = clock();
      |             ^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Runtime error 2 ms 1236 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Runtime error 2 ms 1236 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Runtime error 2 ms 1236 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Incorrect 1 ms 320 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Runtime error 2 ms 1236 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -