Submission #1003894

# Submission time Handle Problem Language Result Execution time Memory
1003894 2024-06-20T19:42:37 Z vjudge1 Ekoeko (COCI21_ekoeko) C++17
110 / 110
24 ms 9192 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
vector<int> tree;

void update(int pos, int i, int f, int idx){
    if(i == f and i == idx){
        tree[pos]++;
        return;
    }
    int meio = (i+f)/2;
    if(idx <= meio) update(2*pos+1, i, meio, idx);
    else update(2*pos+2, meio+1, f, idx);
    tree[pos] = tree[2*pos+1] + tree[2*pos+2];
    return;
}

int query(int pos, int i, int f, int l, int r){
    if(l<=i and f<=r){
        return tree[pos];
    }
    int meio =(i+f)/2;
    int res = 0;
    if(l <= meio) res += query(2*pos+1, i, meio, l, r);
    if(r > meio) res += query(2*pos+2, meio+1, f, l, r);
    return res;
}

signed main(){

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    vector<vector<int>> letra(30);
    int n; cin >> n;
    n *=2;
    tree.assign(4*(n+1), 0);

    string s; cin >> s;
    
    for(int i=0; i<n; i++){
        int nmr = s[i] - 'a';
        letra[nmr].push_back(i);
    }

    string inicio = "", fim = "";
    vector<int> qnt(30,0);
    int esq = 0, dir = 0;
    
    int res = 0;

    for(int i=0; i<n; i++){
        int nmr = s[i] - 'a';

        if(i<n/2){
            if(qnt[nmr]< (letra[nmr].size()/2)){
                inicio+=s[i];
            }
            else{
                fim += s[i];
                dir++;
            }
        }
        else{
            if(qnt[nmr]< (letra[nmr].size()/2)){
                inicio+=s[i];
                esq++;
            }
            else{
                fim += s[i];
            }
        }
        qnt[nmr]++;
    }

    res += dir*esq;

    int ii= 0, ff = 0;
    qnt.assign(30,0);

    for(int i=0; i<n; i++){
        int nmr = s[i] - 'a';

        if(i<n/2){
            if(qnt[nmr]>= (letra[nmr].size()/2)){
                res += (n/2-1 - (dir-ii) + 1 - i);
                ii++;
            }
        }
        else{
            if(qnt[nmr]< (letra[nmr].size()/2)){
                res += (i - (n/2 + ff));
                ff++;
            }
        }
        qnt[nmr]++;
    }


    s = inicio + fim;

    for(int i=0; i<30; i++) letra[i].clear();

    for(int i=0; i<n; i++){
        int nmr = s[i] - 'a';
        letra[nmr].push_back(i);
    }

    qnt.assign(30,0);

    for(int i = 0; i<n/2; i++){
        int nmr = s[i] - 'a';
        int sz = letra[nmr].size();
        sz/=2;
        int posicao = letra[nmr][sz + qnt[nmr]];

        res += query(0,0,n,posicao, n);
        update(0,0,n, posicao);
        qnt[nmr]++;
    }

    cout << res << endl;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:56:24: warning: comparison of integer expressions of different signedness: '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   56 |             if(qnt[nmr]< (letra[nmr].size()/2)){
Main.cpp:65:24: warning: comparison of integer expressions of different signedness: '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |             if(qnt[nmr]< (letra[nmr].size()/2)){
Main.cpp:85:24: warning: comparison of integer expressions of different signedness: '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   85 |             if(qnt[nmr]>= (letra[nmr].size()/2)){
Main.cpp:91:24: warning: comparison of integer expressions of different signedness: '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   91 |             if(qnt[nmr]< (letra[nmr].size()/2)){
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 6 ms 3608 KB Output is correct
3 Correct 11 ms 6748 KB Output is correct
4 Correct 17 ms 9048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 6 ms 3608 KB Output is correct
3 Correct 11 ms 6748 KB Output is correct
4 Correct 17 ms 9048 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 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 6 ms 3608 KB Output is correct
3 Correct 11 ms 6748 KB Output is correct
4 Correct 17 ms 9048 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 20 ms 9192 KB Output is correct
7 Correct 24 ms 9192 KB Output is correct
8 Correct 20 ms 8932 KB Output is correct
9 Correct 21 ms 8964 KB Output is correct
10 Correct 20 ms 8936 KB Output is correct
11 Correct 20 ms 9188 KB Output is correct
12 Correct 20 ms 8596 KB Output is correct
13 Correct 19 ms 8680 KB Output is correct
14 Correct 19 ms 8680 KB Output is correct
15 Correct 19 ms 8676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 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 0 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 348 KB Output is correct
2 Correct 6 ms 3608 KB Output is correct
3 Correct 11 ms 6748 KB Output is correct
4 Correct 17 ms 9048 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 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 20 ms 9192 KB Output is correct
11 Correct 24 ms 9192 KB Output is correct
12 Correct 20 ms 8932 KB Output is correct
13 Correct 21 ms 8964 KB Output is correct
14 Correct 20 ms 8936 KB Output is correct
15 Correct 20 ms 9188 KB Output is correct
16 Correct 20 ms 8596 KB Output is correct
17 Correct 19 ms 8680 KB Output is correct
18 Correct 19 ms 8680 KB Output is correct
19 Correct 19 ms 8676 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 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 0 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 348 KB Output is correct
32 Correct 22 ms 9112 KB Output is correct
33 Correct 20 ms 9188 KB Output is correct
34 Correct 20 ms 8932 KB Output is correct
35 Correct 20 ms 9192 KB Output is correct
36 Correct 20 ms 9188 KB Output is correct
37 Correct 19 ms 8936 KB Output is correct
38 Correct 20 ms 8680 KB Output is correct
39 Correct 22 ms 8680 KB Output is correct
40 Correct 19 ms 8680 KB Output is correct
41 Correct 19 ms 8676 KB Output is correct