Codeforces Round 955 (Div. 2, with prizes from NEAR!) F. Sorting Problem Again(multiset二分/线段树二分)

题目

思路来源

灵茶群、propane代码

题解

线段树+set或者multiset均可

感觉pyy这个代码写的还是挺巧妙的,也避免了使用线段树求区间最值

注意到序列由最多三段拼接而成,

一段增序(非严格),一段存在逆序的部分,一段增序(非严格)

贴一下灵茶群群友的图,

对于存在逆序的部分,记录下来这一段最左最右的位置,求出这一段最小值和最大值

前面那段的最大值只能小于等于这一段最小值,后面那段的最小值只能大于等于这段最大值

分别二分即可,可以线段树二分,也可以在multiset上二分

代码

#include<iostream>
#include<cstring>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;
using LL = long long;

int main(){

#ifdef LOCAL
    freopen("data.in", "r", stdin);
    freopen("data.out", "w", stdout);
#endif

    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);

    int T;
    cin >> T;
    while(T--){
        int n;
        cin >> n;
        vector<int> a(n + 1);
        for(int i = 1; i <= n; i++) cin >> a[i];
        multiset<int> s, s1, s2;
        for(int i = 1; i + 1 <= n; i++){
            if (a[i] > a[i + 1]){
                s.insert(i);
                s1.insert(a[i]);
                s2.insert(a[i + 1]);
            }
        }

        auto add = [&](int x){
            if (x - 1 >= 1 and a[x - 1] > a[x]){
                s.insert(x - 1);
                s1.insert(a[x - 1]);
                s2.insert(a[x]);
            }
            if (x + 1 <= n and a[x] > a[x + 1]){
                s.insert(x);
                s1.insert(a[x]);
                s2.insert(a[x + 1]);
            }
        };

        auto del = [&](int x){
            if (x - 1 >= 1 and a[x - 1] > a[x]){
                s.erase(x - 1);
                s1.erase(s1.find(a[x - 1]));
                s2.erase(s2.find(a[x]));
            }
            if (x + 1 <= n and a[x] > a[x + 1]){
                s.erase(x);
                s1.erase(s1.find(a[x]));
                s2.erase(s2.find(a[x + 1]));
            }
        };

        auto get = [&](){
            if (s.empty()){
                cout << -1 << ' ' << -1 << '\n';
                return;
            }
            int L = *s.begin();
            int R = *s.rbegin() + 1;
            {
                int mn = *s2.begin();
                cout << int(upper_bound(a.begin() + 1, a.begin() + L + 1, mn) - a.begin()) << ' ';
            }
            {
                int mx = *s1.rbegin();
                cout << int(lower_bound(a.begin() + R, a.end(), mx) - a.begin() - 1) << '\n';
            }
        };

        get();
    
        int q;
        cin >> q;
        while(q--){
            int x, y;
            cin >> x >> y;
            del(x);
            a[x] = y;
            add(x);
            get();
        }

    }

}

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
抱歉,根据提供的引用内容,我无法理解你具体想要问什么问。请提供更清晰明确的问,我将竭诚为你解答。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Codeforces Round 860 (Div. 2)解](https://blog.csdn.net/qq_60653991/article/details/129802687)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [【CodeforcesCodeforces Round 865 (Div. 2) (补赛)](https://blog.csdn.net/t_mod/article/details/130104033)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Codeforces Round 872 (Div. 2)(前三道](https://blog.csdn.net/qq_68286180/article/details/130570952)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Code92007

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

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

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

打赏作者

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

抵扣说明:

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

余额充值