Submission #774628

# Submission time Handle Problem Language Result Execution time Memory
774628 2023-07-06T00:02:52 Z Ozy Ekoeko (COCI21_ekoeko) C++17
110 / 110
26 ms 6408 KB
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define debug(a) cout << #a << " = " << a << endl
#define debugsl(a) cout << #a << " = " << a << ", "
#define rep(i,a,b) for(int i = (a); i <= (b); i++)
#define repa(i,a,b) for(int i = (a); i >= (b); i--)
#define pll pair<lli,lli>

#define MAX 100000
//para el vector de orden
#define val first
#define id second

lli n,a,arr[MAX*2+2],res,cont,c_2,offset;
string st;
lli cant[30],n_cant[30];
vector<pll> orden;
queue<lli> p_real[30];
lli stree[MAX*4];

lli query(lli pos, lli ini, lli fin, lli l, lli r) {

    if (ini > r || fin < l) return 0;
    if (l <= ini && fin <= r) return stree[pos];

    lli mitad = (ini+fin)/2;
    lli a = query(pos*2,ini,mitad,l,r);
    lli b = query(pos*2+1,mitad+1,fin,l,r);
    return a+b;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n >> st;
    rep(i,1,2*n) {
        a = st[i-1] - 'a';
        arr[i] = a;
        cant[a]++;
    }

    //dividir en 2 mitades sumar lo que le corresponde al resultado
    cont = 1;
    c_2 = 1;
    rep(i,1,n*2) {
        n_cant[arr[i]]++;
        if (n_cant[arr[i]] <= cant[arr[i]]/2) {

            p_real[arr[i]].push(cont);
            res += i - cont;
            cont++;
        }
        else {
            //debug(i);

            orden.push_back({p_real[arr[i]].front(), c_2});
            p_real[arr[i]].pop();
            c_2++;
        }
    }

    //procesar el vector de orden y actualizar el stree
    sort(orden.begin(), orden.end());

    offset = 1;
    while (offset < n) offset *= 2;
    for(auto act : orden) {
        res += query(1,1,offset,act.id,offset);
        a = offset+act.id-1;
        while(a != 0) {
            stree[a]++;
            a/=2;
        }
    }

    cout << res;

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 6 ms 2372 KB Output is correct
3 Correct 12 ms 4168 KB Output is correct
4 Correct 16 ms 5988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 6 ms 2372 KB Output is correct
3 Correct 12 ms 4168 KB Output is correct
4 Correct 16 ms 5988 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 364 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 6 ms 2372 KB Output is correct
3 Correct 12 ms 4168 KB Output is correct
4 Correct 16 ms 5988 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 22 ms 6356 KB Output is correct
7 Correct 22 ms 6392 KB Output is correct
8 Correct 21 ms 6280 KB Output is correct
9 Correct 21 ms 6324 KB Output is correct
10 Correct 21 ms 6116 KB Output is correct
11 Correct 21 ms 6128 KB Output is correct
12 Correct 21 ms 6016 KB Output is correct
13 Correct 20 ms 5972 KB Output is correct
14 Correct 20 ms 6004 KB Output is correct
15 Correct 26 ms 5904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 360 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 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 364 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 6 ms 2372 KB Output is correct
3 Correct 12 ms 4168 KB Output is correct
4 Correct 16 ms 5988 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 364 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 22 ms 6356 KB Output is correct
11 Correct 22 ms 6392 KB Output is correct
12 Correct 21 ms 6280 KB Output is correct
13 Correct 21 ms 6324 KB Output is correct
14 Correct 21 ms 6116 KB Output is correct
15 Correct 21 ms 6128 KB Output is correct
16 Correct 21 ms 6016 KB Output is correct
17 Correct 20 ms 5972 KB Output is correct
18 Correct 20 ms 6004 KB Output is correct
19 Correct 26 ms 5904 KB Output is correct
20 Correct 1 ms 360 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 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 364 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 340 KB Output is correct
32 Correct 23 ms 6408 KB Output is correct
33 Correct 22 ms 6284 KB Output is correct
34 Correct 22 ms 6276 KB Output is correct
35 Correct 21 ms 6248 KB Output is correct
36 Correct 21 ms 6168 KB Output is correct
37 Correct 21 ms 6160 KB Output is correct
38 Correct 21 ms 6120 KB Output is correct
39 Correct 21 ms 5984 KB Output is correct
40 Correct 20 ms 5944 KB Output is correct
41 Correct 20 ms 5840 KB Output is correct