贪心+构造,CF 761D - Dasha and Very Difficult Problem

目录

一、题目

1、题目描述

2、输入输出

2.1输入

2.2输出

3、原题链接

二、解题报告

1、思路分析

2、复杂度

3、代码详解


一、题目

1、题目描述

2、输入输出

2.1输入

2.2输出

3、原题链接

761D - Dasha and Very Difficult Problem


二、解题报告

1、思路分析

如果我们知道 c,那么根据 c[i] = b[i] - a[i],我们相当于得到了 b[]

如何得到c?

我们知道了 c 各个下标元素排名

以及每个元素的范围:[l - a[i], r - a[i]]

那么我们贪心的从低往上构造

显然越小越好,这样上面的元素容错率越高

即 最小的构造为 l - p[i],次小的构造为 max(l - p[i] + 1, l - a[j])……

2、复杂度

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

3、代码详解

 ​
#include <bits/stdc++.h>

// #define DEBUG

using u32 = unsigned;
using i64 = long long;
using u64 = unsigned long long;

constexpr int P = 1E9 + 7;
constexpr int inf32 = 1E9 + 7;
constexpr i64 inf64 = 1E18 + 7;

void solve() {
    int n, l, r;
    std::cin >> n >> l >> r;

    std::vector<int> a(n);
    for (int i = 0; i < n; ++ i) std::cin >> a[i];

    std::vector<int> p(n);
    for (int i = 0; i < n; ++ i) std::cin >> p[i];

    std::vector<int> id(n);
    std::iota(id.begin(), id.end(), 0);

    std::ranges::sort(id, [&](int i, int j) -> bool {
        return p[i] < p[j];
    });

    std::vector<int> ans(n);

    ans[id[0]] = l - a[id[0]];

    for (int i = 1; i < n; ++ i) {
        if (p[id[i]] == p[id[i - 1]])
            ans[id[i]] = ans[id[i - 1]];
        else {
            ans[id[i]] = std::max(ans[id[i - 1]] + 1, l - a[id[i]]);
        }
    }

    for (int i = 0; i < n; ++ i) {
        ans[i] += a[i];
        if (ans[i] > r) {
            std::cout << -1 << '\n';
            return;
        }
    }

    for (int i = 0; i < n; ++ i) {
        std::cout << ans[i] << " \n"[i + 1 == n];
    }
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

#ifdef DEBUG
    int cur = clock();
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif

    int t = 1;
    // std::cin >> t;

    while (t--) {
        solve();
    }
#ifdef DEBUG
    std::cerr << "run-time: " << clock() - cur << '\n';
#endif
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EQUINOX1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值