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

 

 

Is Desk Productivity Tools...

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

The Outer Worlds 2...

Gamers and RPG enthusiasts have reason to celebrate as...

Why Travel Organization Gear...

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

Why Everyone Is Talking...

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

Why Everyone Is Talking...

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

Why Everyone Is Talking...

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

Is Desk Productivity Tools Worth Your Money Here Is Our Full Review

📢 As an Amazon Associate, I earn from qualifying purchases.💰 Official Price: Check on Amazon🌟 Why Buy the Desk Productivity Tools?The Desk Productivity Tools...

The Outer Worlds 2 v1.2.0.1-TENOKE

Gamers and RPG enthusiasts have reason to celebrate as Obsidian Entertainment's acclaimed franchise returns with new developments. The highly anticipated sequel to the award-winning...

Why Travel Organization Gear Is Currently Breaking the Internet

📢 As an Amazon Associate, I earn from qualifying purchases.💰 Official Price: Check on Amazon🌟 Why Buy the Travel Organization Gear?The Travel Organization Gear...

Why Everyone Is Talking About the Car Detailing Essentials

📢 As an Amazon Associate, I earn from qualifying purchases.💰 Official Price: Check on Amazon🌟 Why Buy the Car Detailing Essentials?The Car Detailing Essentials...

Why Everyone Is Talking About the Smart Home Hacks

📢 As an Amazon Associate, I earn from qualifying purchases.💰 Official Price: Check on Amazon🌟 Why Buy the Smart Home Hacks?The Smart Home Hacks...

Why Everyone Is Talking About the Viral Kitchen Gadgets

📢 As an Amazon Associate, I earn from qualifying purchases.💰 Official Price: Check on Amazon🌟 Why Buy the Viral Kitchen Gadgets?The Viral Kitchen Gadgets...

Why Everyone Is Talking About the Travel Organization Gear

📢 As an Amazon Associate, I earn from qualifying purchases.💰 Official Price: Check on Amazon🌟 Why Buy the Travel Organization Gear?The Travel Organization Gear...

Why Everyone Is Talking About the Travel Organization Gear

📢 As an Amazon Associate, I earn from qualifying purchases.💰 Official Price: Check on Amazon🌟 Why Buy the Travel Organization Gear?The Travel Organization Gear...

GLP-1 Drugs Like Ozempic Carry Elevated Risks of Muscle Loss and Malnutrition in Adults Over 65

Understanding the Risks of GLP-1 Drugs for Older Adults GLP-1 receptor agonists, such as Ozempic, Wegovy, and Mounjaro, have transformed the landscape of weight management...