E. K整数
トピック接続:https://codeforces.com/contest/1269/problem/E
問題の意味
順列pを与え、動作は次の2つの隣接する要素は、今あなたの最小動作を何度も要求し、そのような1,2,3,4..kなどの形状のサブセグメントを形成するたびに交換することができます
kは1〜Nからです
ソリューション:
最終試験では、この問題を解決することは遅すぎる、私は流出の出です
まず、ソートされたサブセグメントを取得するために、我々が得ることができる質問の意味に従って
kは= 1、答えは0でなければならないときのために
、Kに関して= N、pが1,2,3配列に配置されなければなりません。。。操作の最小数N
場合K〜N 1との間で、その後、操作の最小数はどのくらいであるべきです
それは操作の数を最小限に抑えることができるように、各ソートサブセグメントのために、我々は、参照点としてこのサブセクションの中点を取る、サブセクションのポイントの残りの部分は、この基準点に依存することが可能でなければなりません
私たちは私のサブセグメント/ 2要素の半分だけ中点位置を見つける必要があるたびにそう
したがって、この位置のために、すべての要素のサブセグメントの移動距離は、次式で表すことができる
\ [和= \和| T_I -p_i | \\ = \ sum_ {TI> PI} TI-\ sum_ {TI> PI} PI + \ sum_ {TI <PI} PI-
\ sum_ {TI <PI} TI \] 我々が維持するツリーアレイ/セグメントツリーを形成することができ、この方程式の
運動の各要素について、私たちは、この要素は、その要素よりも大きくなる前に、この要素から要素の逆数に移動した後に必要なことを、維持するために、逆の順序で彼の番号を使用することができますです
学生のための清の妹は、この式は、説明ビデオを理解していないお勧めします
https://www.bilibili.com/video/av80409992?p=4
コード
/**
* ┏┓ ┏┓
* ┏┛┗━━━━━━━┛┗━━━┓
* ┃ ┃
* ┃ ━ ┃
* ┃ > < ┃
* ┃ ┃
* ┃... ⌒ ... ┃
* ┃ ┃
* ┗━┓ ┏━┛
* ┃ ┃ Code is far away from bug with the animal protecting
* ┃ ┃ 神兽保佑,代码无bug
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
*/
// warm heart, wagging tail,and a smile just for you!
//
// _ooOoo_
// o8888888o
// 88" . "88
// (| -_- |)
// O\ = /O
// ____/`---'\____
// .' \| |// `.
// / \||| : |||// \
// / _||||| -:- |||||- \
// | | \ - /// | |
// | \_| ''\---/'' | |
// \ .-\__ `-` ___/-. /
// ___`. .' /--.--\ `. . __
// ."" '< `.___\_<|>_/___.' >'"".
// | | : `- \`.;`\ _ /`;.`/ - ` : | |
// \ \ `-. \_ __\ /__ _/ .-` / /
// ======`-.____`-.___\_____/___.-`____.-'======
// `=---='
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// 佛祖保佑 永无BUG
#include <set>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <string>
#include <bitset>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define ls rt<<1
#define rs rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
const int maxn = 3e5 + 5;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double Pi = acos(-1);
LL gcd(LL a, LL b) {
return b ? gcd(b, a % b) : a;
}
LL lcm(LL a, LL b) {
return a / gcd(a, b) * b;
}
double dpow(double a, LL b) {
double ans = 1.0;
while(b) {
if(b % 2)ans = ans * a;
a = a * a;
b /= 2;
} return ans;
}
LL quick_pow(LL x, LL y) {
LL ans = 1;
while(y) {
if(y & 1) {
ans = ans * x % mod;
} x = x * x % mod;
y >>= 1;
} return ans;
}
int n;
int a[maxn], c[maxn];
int lowbit(int x) {
return x & (-x);
}
LL bit1[maxn], bit2[maxn];
void add(LL *bit, int pos, int val) {
while(pos < maxn) {
bit[pos] += val;
pos += lowbit(pos);
}
}
LL query(LL *bit, int pos) {
LL ans = 0;
while(pos) {
ans += bit[pos];
pos -= lowbit(pos);
}
return ans;
}
int bs(LL *bit, int val) {
int i = 0;
for(int j = 19; j >= 0; j--) {
if((i | 1 << j) < maxn) {
if(bit[i | 1 << j] <= val) {
val -= bit[i |= 1 << j];
}
}
}
return i;
}
int main() {
#ifndef ONLINE_JUDGE
FIN
#endif
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
c[a[i]] = i;
}
LL cnt = 0;
for(int i = 1; i <= n; i++) {
int p = c[i];
add(bit1, p, 1);
cnt += i - query(bit1, p);
add(bit2, p, p);
int pos = bs(bit1, i / 2) + 1;
debug2(i, pos);
LL sum = 0;
LL aa = i / 2; //t之前
LL bb = i - i / 2 - 1; //t之后
sum += (LL)aa * pos - (LL)aa * (aa + 1) / 2 - query(bit2, pos - 1); //p
sum += (query(bit2, maxn) - query(bit2, pos)) - (LL)bb * pos - (LL)bb * (bb + 1) / 2;//p
printf("%lld\n", sum + cnt);
}
return 0;
}