Codeforces Round #388 (Div. 2), problem: (E) Inversions After Shuffle Solution in C/C++

Codeforces Round #388 (Div. 2), problem: (E) Inversions After Shuffle Solution in C/C++

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N 100000

/* Fenwick tree */

void update(long long *tt, int i, int n, int x) {
while (i < n) {
tt[i] += x;
i |= i + 1;
}
}

long long query(long long *tt, int i) {
long long sum = 0;

while (i >= 0) {
sum += tt[i];
i &= i + 1;
i–;
}
return sum;
}

struct element {
int a, i;
} aa[N];

int compare(const void *a, const void *b) {
struct element *pa = (struct element *) a;
struct element *pb = (struct element *) b;

return pb->a – pa->a;
}

int main() {
int n, i;
long long m;
double inv;
static long long tt[N];

scanf(“%d”, &n);
for (i = 0; i < n; i++) {
scanf(“%d”, &aa[i].a);
aa[i].i = i;
}
qsort(aa, n, sizeof *aa, compare);
m = (long long) n * (n + 1) / 2;
inv = 0;
for (i = 0; i < n; i++) {
inv += query(tt, aa[i].i) * m;
update(tt, aa[i].i, n, 1);
}
memset(tt, 0, n * sizeof *tt);
for (i = 0; i < n; i++) {
inv -= query(tt, aa[i].i) * (n – aa[i].i);
update(tt, aa[i].i, n, aa[i].i + 1);
}
for (i = 1; i <= n; i++)
inv += (long long) (n – i + 1) * i * (i – 1) / 4.0;
printf(“%.12lf\n”, inv / m);
return 0;
}

 

 

Fangzhou Ranked among 2025...

In a significant milestone for the digital healthcare sector,...

Infected with TDSS and...

Dealing with TDSS Rootkit Infections and Google Search Redirects Computer...

Why Everyone is Obsessed with the USB Rechargeable Lighter

Are you tired of dealing with everyday frustrations that slow down your routine? We've all been...

Why Everyone is Obsessed with the AeroPress Coffee Maker

Are you tired of dealing with everyday frustrations that slow down your routine? We've all been...

Fangzhou Ranked among 2025 Guangzhou AI Innovation “Most Promising Enterprises” for AI‑Powered Chronic Disease Services

In a significant milestone for the digital healthcare sector, Fangzhou has been officially ranked among the 2025 Guangzhou AI Innovation 'Most Promising Enterprises' list....

Why Everyone is Obsessed with the Universal Car Cleaning Gel

Are you tired of dealing with everyday frustrations that slow down your routine? We've all been...

Why Everyone is Obsessed with the Portable Door Lock for Travel

Are you tired of dealing with everyday frustrations that slow down your routine? We've all been...

Infected with TDSS and Google Keeps redirecting

Dealing with TDSS Rootkit Infections and Google Search Redirects Computer security is a continuous battle, and some threats are significantly more stubborn than others. A...

Why Everyone is Obsessed with the Portable Handheld Misting Fan

Are you tired of dealing with everyday frustrations that slow down your routine? We've all been...

Why Everyone is Obsessed with the Mini Magnetic Bag Sealer

Are you tired of dealing with everyday frustrations that slow down your routine? We've all been...

Ronda Rousey vs. Gina Carano MVP MMA live blog

The Dream Match: Ronda Rousey vs. Gina Carano at MVP MMA The world of mixed martial arts is currently witnessing a moment many thought would...