牛客多校第五场 D.inv
本文最后更新于 2092 天前,其中的信息可能已经有所发展或是发生改变。

内容纲要

链接:https://www.nowcoder.com/acm/contest/143/D
来源:牛客网

题面

Kanade has an even number n and a permutation b of all of the even numbers in [1,n]
Let a denote an array [1,3,5….n-1] , now you need to find a permutation of [1,n] satisfy both a and b are subsequence of it and minimize the number of inverse pair of it.

题意

给定一个 [1,n] 之间所有偶数的排列 b,其中 n 是偶数 现在有一个数组 a=[1,3,5…..n-1]
要求归并 a 和 b,使得他们归并后逆序对数量最少 1<=n<=200000

题解

没有看这题,等着身体舒服点再看发现是板题,很难受。
相当于是要将每个 2i+1 插入到 b 中 通过推导可以发现,每个2i+1插入到 b 中的最优位置一定是递增的 所以直接对于每个 2i+1 计算他能产生的最少的逆序对数量,加起来即可
树状数组算逆元 然后维护一个最大堆,贪心的将目标数字放到最大值的后面即可

AC代码

#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <array>

#define lowbit(x) ((x) & (-x))

using namespace std;
const int maxn = 3e5 + 6;
using ll = long long;
int n, a[maxn], tree[maxn];
ll ans = 0;
priority_queue<int> q;

void add(int x) {
    for (; x <= n; x += lowbit(x))++tree[x];
}

int sum(int x) {
    int ret = 0;
    for (; x; x -= lowbit(x))ret += tree[x];
    return ret;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    n >>= 1;
    for (int i = 1; i <= n; i++)cin >> a[i];
    for (int i = 1; i <= n; i++)
        add(a[i] >>= 1), ans += i - sum(a[i]);
    for (int i = 1; i <= n; i++) {
        q.push(a[i]);
        if (q.top() > a[i])
            ans += q.top() - a[i], q.pop(), q.push(a[i]);
    }
    cout << ans << "\n";
    return 0;
}
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇