1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <iostream>
#include <vector>
// #include <set>
// #include <string>

using namespace std;

int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    string s;
    cin >> s;

    vector<int> va;
    vector<int> vb;
    
    for(int i=0; i<s.length() ; i++){
        if (s[i]=='a') {
            va.push_back(i);
        } else {
            vb.push_back(i);
        }      
    }

    //check sanity
    int totalsize=va.size()+vb.size();
    if ( (va.size()%2==1) && (vb.size()%2==1) ){
        // obydwie nieparzyste , to zle 
        cout << "-1" << '\n';
        exit(0);
    }

    int half=totalsize/2;

    char left=' ';
    char right=' ';
    long long int swaps=0;
    for (int i=0;i<half;i++){
        if ( va.empty() || vb.empty() ){
            // no more swaps
            break;
        }


        // what we have on the left end
        // int sabegin=va.begin();
        if ( va.front() < vb.front() ) {
           left='a';
        } else {
           left='b';
        }
        // what we have on right end
        if ( va.back() > vb.back() ) {
           right='a';
        } else {
           right='b';
        }

    if (left != right){
        // what we need to swap
        std::vector<int>::iterator r_it; // if we swap left 
        std::vector<int>::iterator l_it; 
        
        //swap refernece 
        if (left == 'a'){

            // find on right side how many "b" we need to jummp
            r_it=std::lower_bound(vb.begin(),vb.end(),va.back()) ;
            // for "b" on the left side lets check how many "a we need to jump
            l_it=std::lower_bound(va.begin(),va.end(),vb.front()) ;
            int r_distance=vb.end()-r_it;
            int l_distance=l_it-va.begin();
            // cout << va.back() << '\n';     

            if (r_distance < l_distance ){
                // we are swapping on the right and we are swapping "a" over  "b"
                swaps+=r_distance;
                // remove elements on the edges
                va.erase(va.begin());
                va.pop_back();
            } else {
                swaps+=l_distance;
                vb.erase(vb.begin());
                vb.pop_back();
            }


        } else {
            // a jest po prawo b jest po lewo

            // find on right side for "b" how many "a" we need to jummp
            r_it=std::lower_bound(va.begin(),va.end(),vb.back()) ;
            // for "a" on the left side lets check how many "b we need to jump
            l_it=std::lower_bound(vb.begin(),vb.end(),va.front()) ;
            
            int r_distance=va.end()-r_it;
            int l_distance=l_it-vb.begin();
            // cout << vb.back() << '\n';     

            if (r_distance < l_distance ){
                // we are swapping on the right and we are mowing to the side "b"
                swaps+=r_distance;
                // remove elements on the edges
                vb.erase(vb.begin());
                vb.pop_back();
            } else {
                swaps+=l_distance;
                va.erase(va.begin());
                va.pop_back();
            }


        }
             
    
    } else {
        // left equals right
        // nothing to swap
        if (left == 'a'){
                va.erase(va.begin());
                va.pop_back();
        } else {
            vb.erase(vb.begin());
            vb.pop_back();
        }

    }
        
    }
    cout << swaps << '\n';     
}