贪心+树状数组,CF1579E2 - Array Optimization by Deque

一、题目

1、题目描述

2、输入输出

2.1输入

2.2输出

3、原题链接

1579E2 - Array Optimization by Deque

二、解题报告

1、思路分析

很好想也很好证明的贪心

因为添加的顺序是确定的,我们每次只需决策放左边还是放右边

x放左边,那么贡献就是右边比x小的个数

x放右边,那么贡献就是左边比x大的个数

两个位置,哪个贡献少放哪边

证明:反证法

假设存在最优解,存在a[i] 没有按照贪心策略放置,不妨假设应该放左边P1但是放了右边P2

那么我们将操作修改把a[i]放左边P1,其它操作不变

那么a[i]只影响了a[P1 + 1, P2 - 1]之间的数字的贡献

由于贪心策略是放P1,所以区间内比a[i]小的个数小于等于比a[i]大的个数,所以贡献不会变大

我们不断调整最优解中不是贪心策略的操作能够得到另一个最优甚至更优解。

证毕

由于值域很大需要离散化

2、复杂度

时间复杂度:O(NlogN) 空间复杂度:O(N)

3、代码详解

#include <bits/stdc++.h>
using PII = std::pair<int, int>;
using i64 = long long;
 
const int inf = 1e9;

template<typename T = int>
class FenWick {
private:
    T n;
    std::vector<T> tr;
public:
    FenWick(T _n) : n(_n), tr(_n + 1) 
    {}

    void add(T x, T k) {
        for (; x <= n; x += x & -x) tr[x] += k;
    }

    T query(T x) const {
        T res = 0;
        for (; x; x &= x - 1) res += tr[x];
        return res;
    }
};
 
void solve() {
    int n;
    std::cin >> n;
    std::vector<int> nums(n);
    for (int i = 0; i < n; i ++ ) std::cin >> nums[i];
    std::vector<int> id(nums);
    std::sort(id.begin(), id.end());
    id.erase(std::unique(id.begin(), id.end()), id.end());
    int m = id.size();
    
    FenWick<int> st(m + 1);

    i64 res = 0;
    for (int x : nums) {
        x = std::lower_bound(id.begin(), id.end(), x) - id.begin() + 1;
        int L = st.query(x - 1), R = st.query(m) - st.query(x);
        st.add(x, 1);
        res += std::min(L, R);
    }
    std::cout << res << '\n';
}
 
 
int main () {
    std::ios::sync_with_stdio(false);   std::cin.tie(0);  std::cout.tie(0);
    int _ = 1;
    std::cin >> _;
    while (_ --)
        solve();
    return 0;
}

相关推荐

  1. [CF704E] Iron man

    2024-06-06 07:32:04       60 阅读
  2. CF1869 D2. Candy Party (Easy&Hard Version) [二进制贪心]

    2024-06-06 07:32:04       52 阅读
  3. CF1951E No Palindromes 题解

    2024-06-06 07:32:04       40 阅读
  4. 题解:CF1951E(No Palindromes)

    2024-06-06 07:32:04       25 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-06 07:32:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-06 07:32:04       106 阅读
  3. 在Django里面运行非项目文件

    2024-06-06 07:32:04       87 阅读
  4. Python语言-面向对象

    2024-06-06 07:32:04       96 阅读

热门阅读

  1. 洛谷 P8740 [蓝桥杯 2021 省 A] 填空问题 题解

    2024-06-06 07:32:04       27 阅读
  2. folyd算法求最短路径

    2024-06-06 07:32:04       43 阅读
  3. AudioSet 本体与声音实体对象

    2024-06-06 07:32:04       29 阅读
  4. Android AAudio——C API创建AudioTrack(六)

    2024-06-06 07:32:04       29 阅读
  5. 力扣2831.找出最长等值子数组

    2024-06-06 07:32:04       30 阅读
  6. axios

    axios

    2024-06-06 07:32:04      29 阅读
  7. iOS中的UIScene和UISceneDelegate

    2024-06-06 07:32:04       28 阅读
  8. Android AAudio——音频流释放死锁(七)

    2024-06-06 07:32:04       31 阅读
  9. Python中的上下文管理:深入探索contextlib模块

    2024-06-06 07:32:04       27 阅读
  10. centos系统编译openssl和openssl-lib的rpm安装包

    2024-06-06 07:32:04       22 阅读
  11. godot.bk2

    godot.bk2

    2024-06-06 07:32:04      30 阅读
  12. Git commit规范

    2024-06-06 07:32:04       24 阅读
  13. 入门级python编程题(12)洛谷(分类平均)

    2024-06-06 07:32:04       20 阅读