codeforces 1269 E K Integers


E. K Integers

題目連接:https://codeforces.com/contest/1269/problem/E

題意

給了一個排列p,你每次操作可以交換兩個相鄰的元素,現在問你最少操作多少次可以形成一個形如1,2,3,4..k 這樣的子段

k從1~n

題解:

都在期末考試了,這題解出的也太慢了,我來水一發

首先根據題意可得,要得到一個排好序的子段

對於k=1時,答案必為0

對於k=n時,肯定是將排列p排成1,2,3,。。。n的最少操作次數

那么當k在1~n之間時,最少操作次數應該是多少呢

對於每一個排好序的子段來說,我們取這個子段的中點作為基准點,子段中其余的點一定是盡可能往這個基准點靠,這樣才可以使得操作次數最小

所以我們每次只需要二分找到第i個子段的中點i/2的元素的位置

那么對於這個位置,這個子段中所有元素移動的距離可以用如下公式表示

\[sum=\sum|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;

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM