Ryan's WorkSpace
  • 首页
  • 关于我
  1. 首页
  2. C++
  3. 正文

牛客多校第五场 D.inv

2018年08月02日 1260点热度 0人点赞 0条评论

内容纲要

链接: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;
}

相关

本作品采用 知识共享署名 4.0 国际许可协议 进行许可
标签: 暂无
最后更新:2018年08月02日

Ryan Lee

如果帮助到你,请点击广告,谢谢!

点赞
< 上一篇
下一篇 >

文章评论

您需要 登录 之后才可以评论

如果帮助到你,请点击广告,谢谢!

用户您好!请先登录!
登录 注册
Social Media
Github: ryanlee2014
标签聚合
C Java C++ JavaScript php hustoj Apache GitHub
友链
Pacolyon
Lucien's blog
Slian's DreamWork
卡拉搜索
  • 0
  • 16,155
  • 5,990
  • 0
广告

COPYRIGHT © 2020 Ryan's WorkSpace. ALL RIGHTS RESERVED.

THEME KRATOS MADE BY VTROIS

登录
注册|忘记密码?