Pagini recente » Cod sursa (job #1492341) | Cod sursa (job #401288) | Cod sursa (job #1691968) | Cod sursa (job #2136547) | Cod sursa (job #3176156)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("litere.in");
ofstream fout ("litere.out");
char c, x[10001];
void trans (int ind)
{
char aux[10001];
strcpy (aux, x + ind);
strcpy (x + ind + 1, aux);
x[ind] = c;
}
int main()
{
int n, cnt = 0;
fin >> n;
for (int i = 1; i <= n; i ++)
{
fin >> c;
int ind;
if (i == 1) x[0] = c;
else
{
int st = 0, dr = strlen(x) - 1, mij;
ind = 0;
while (st <= dr)
{
mij = (st + dr) / 2;
if (x[mij] <= c) ind = mij, st = mij + 1;
else dr = mij - 1;
}
ind ++;
cnt = cnt + strlen(x) - ind;
trans(ind);
}
}
fout << cnt;
return 0;
}