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

Advertisement

spot_img

How Viral Kitchen Gadgets...

📢 As an Amazon Associate, I earn from qualifying...

Science of a city:...

When we discuss the world’s greatest metropolises, the conversation...

Stop Buying Cheap Alternatives...

📢 As an Amazon Associate, I earn from qualifying...

Pip Edwards turns heads...

Fashion entrepreneur Pip Edwards knows exactly how to command...

Why Viral Kitchen Gadgets...

📢 As an Amazon Associate, I earn from qualifying...

Sensex, Nifty may gain...

The Indian equity benchmarks, Sensex and Nifty, could witness...

How Viral Kitchen Gadgets Is Quietly Changing the Game

📢 As an Amazon Associate, I earn from qualifying purchases.After putting the Viral Kitchen Gadgets through a proper 30-day real-world test, I can tell...

Science of a city: The maths, physics, ecology & psychology of urban life

When we discuss the world’s greatest metropolises, the conversation almost invariably revolves around politics, zoning regulations, and economic indicators. Yet, beneath the concrete grid...

Stop Buying Cheap Alternatives This Viral Kitchen Gadgets Is the Real Deal

📢 As an Amazon Associate, I earn from qualifying purchases.If you had told me six months ago that a single purchase would change how...

Pip Edwards turns heads at Derby Day in a very bizarre headpiece – as she shares rare update on new romance with boyfriend Nicholas...

Fashion entrepreneur Pip Edwards knows exactly how to command attention, and her latest appearance at Flemington Racecourse proved to be no exception. Stepping out...

Why Viral Kitchen Gadgets Is Currently Breaking the Internet

📢 As an Amazon Associate, I earn from qualifying purchases.After putting the Viral Kitchen Gadgets through a proper 30-day real-world test, I can tell...

Sensex, Nifty may gain 5-8% if external headwinds recede: Baldev Prakash

The Indian equity benchmarks, Sensex and Nifty, could witness a notable rebound of 5% to 8% in the near term provided persistent external headwinds...

Is Viral Kitchen Gadgets Worth Your Money Here Is Our Full Review

📢 As an Amazon Associate, I earn from qualifying purchases.After putting the Viral Kitchen Gadgets through a proper 30-day real-world test, I can tell...

Taiwan’s Ministry of Environment deploys digital twin to simulate urban cooling

As global temperatures continue to climb and metropolitan centers face severe heat island effects, urban planners are turning to high-tech solutions to keep cities...

We Tested Viral Kitchen Gadgets Here Is What You Must Know

📢 As an Amazon Associate, I earn from qualifying purchases.I stumbled across this while desperately trying to fix a recurring problem in my routine....