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;
}

 

 

Hegseth Hails US Allies...

In a striking shift of foreign policy rhetoric, newly...

Labour markets may risk...

The Reality Check of AI in the Modern Labor...

Why Everyone is Obsessed with the Universal Charging Cable

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

Why Everyone is Obsessed with the Fridge Odor Eliminator

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

Hegseth Hails US Allies in Asia, Hits Out at Europe Partners

In a striking shift of foreign policy rhetoric, newly appointed U.S. Defense Secretary Pete Hegseth has praised America's allies in Asia while delivering sharp...

Why Everyone is Obsessed with the Reusable Silicone Food Bags

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

Why Everyone is Obsessed with the Motion Sensor Night Lights

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

Labour markets may risk a milder shock than AI fantasies suggest, but that’s only partial relief

The Reality Check of AI in the Modern Labor Market For the past few years, the dominant narrative surrounding artificial intelligence (AI) has been one...

Why Everyone is Obsessed with the TSA Approved Toiletry Bags

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

Why Everyone is Obsessed with the Phone Screen Cleaner Spray

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

Edmonton hospital using medical technology to offer non-surgical weight loss procedure

The Royal Alexandra Hospital in Edmonton is pioneering a new era of healthcare in Alberta by offering an innovative, non-surgical weight loss procedure. This...