【每日一题】【想通后的诈骗题】B. Index and Maximum Value Codeforces Round 969 (Div. 2) C++

news/2024/9/15 9:22:53 标签: c++, 算法

Codeforces Round 969 (Div. 2) B题

B. Index and Maximum Value

题目背景

Codeforces Round 969 (Div. 2)

题目描述

给一个数组a

有m次操作,每次操作都是以下二选一
+ l r . 让数组里每一个在 [ l , r ] [l,r] [l,r]闭区间里面的数字 + 1
- l r . 让数组里每一个在 [ l , r ] [l,r] [l,r]闭区间里面的数字 - 1

每次操作后输出该数组里的最大值。

一共有 T T T组数据。
保证原始数组a里面的数字不会超过 1 0 9 10^9 109
1 ≤ l ≤ r ≤ 1 0 9 1\le l \le r \le 10^9 1lr109

样例 #1

样例输入 #1

5
5 5
1 2 3 2 1
+ 1 3
- 2 3
+ 1 2
+ 2 4
- 6 8
5 5
1 3 3 4 5
+ 1 4
+ 2 3
- 4 5
- 3 3
- 2 6
5 5
1 1 1 1 1
+ 2 3
- 4 5
+ 1 6
- 2 5
+ 1 8
1 1
1
- 1 1
1 1
1000000000
+ 1000000000 1000000000

样例输出 #1

4 4 4 5 5
5 5 4 4 3
1 1 2 1 2
0
1000000001

备注(Note)

In the first test case, the process of the operations is listed below:

After the first operation, the array becomes equal [2,3,4,3,2].The maximum value is 4

After the second operation, the array becomes equal [1,2,4,2,1].The maximum value is 4

After the third operation, the array becomes equal [2,3,4,3,2].The maximum value is 4

After the fourth operation, the array becomes equal [3,4,5,4,3].The maximum value is 5

After the fifth operation, the array becomes equal [3,4,5,4,3].The maximum value is 5

In the second test case, the process of the operations is listed below:

After the first operation, the array becomes equal [2,4,4,5,5].The maximum value is 5

After the second operation, the array becomes equal [3,4,4,5,5].The maximum value is 5

After the third operation, the array becomes equal [3,3,3,4,4].The maximum value is 4

After the fourth operation, the array becomes equal [2,2,2,4,4].The maximum value is 4

After the fifth operation, the array becomes equal [1,1,1,3,3].The maximum value is 3

做题思路

考虑到最后答案只与最大值相关。

先提出数组里面的最大值。

因为每次操作都是改变 [ l , r ] [l,r] [l,r]闭区间里面的已有值,所以如果最大值不在该区间内,最大值不变。反之最大值加1或者减1

代码

#include <iostream>
#include <algorithm>
#define int long long
#define For(a,b) for(int i = (a) ; i<= (b) ; i++)
#define IOS {ios::sync_with_stdio(0); cin.tie(0);}
using namespace  std;
const int N = 2e5+10;
int n , m;
int a[N];
void solve(){
    cin >> n >> m;
    int maxa = 0;
    For(1,n){
        cin >> a[i];
        maxa = max(maxa , a[i]);
    }
    char op;int l,r;
    while(m--){
        cin >> op;
        cin >> l >> r;
        if(maxa >= l && maxa <= r){
            if(op == '+')maxa++;
            else maxa--;
        }
        cout << maxa << ' ';
    }
    cout << '\n';
}
signed main(){
    IOS
    int _=1;cin >> _;
    while(_--)solve();
    return 0;
}



http://www.niftyadmin.cn/n/5634854.html

相关文章

【AI】自动驾驶的分级

国际汽车工程学会&#xff08;SAE&#xff09;自动驾驶标准将汽车驾驶技术分为从L0&#xff08;无驾驶自动化&#xff09;至L5&#xff08;完全驾驶自动化&#xff09;的6个级别&#xff0c;通常称L3及以上级别的自动驾驶为高级别自动驾驶&#xff0c;车辆驾驶任务的主导权由驾…

基于STM32开发的智能家居温度控制系统

目录 引言环境准备工作 硬件准备软件安装与配置系统设计 系统架构硬件连接代码实现 系统初始化温度监测与显示风扇/加热器控制Wi-Fi通信与远程监控应用场景 家庭环境的智能温度管理办公楼的节能温控系统常见问题及解决方案 常见问题解决方案结论 1. 引言 随着人们对生活质量…

鸿蒙模拟器篇

1、首先需要在华为官网申请模拟器资格&#xff0c;附链接&#xff1a;鸿蒙模拟器&#xff08;HarmonyOS Emulator&#xff09;Beta活动申请 填写相关信息提交申请&#xff0c;申请结果状态在个人中心 — 我的活动页面查看 2、申请通过之后开始下载模拟器 注意&#xff1a…

机器视觉-4 检测原理之OpenCV Blob特征检测

在OpenCV中&#xff0c;BLOB&#xff08;Binary Large OBjects&#xff09;检测是一种用于识别和分析二值图像中连通区域的技术。OpenCV提供了专门的工具类SimpleBlobDetector来帮助实现这一功能。以下是关于OpenCV中BLOB检测的详细说明&#xff0c;包括其原理、使用方法和应用…

Android 设置动态库依赖路径

在 Android 中&#xff0c;使用 dlopen 打开动态库时&#xff0c;可以通过以下方法设置动态库的依赖路径 LD_LIBRARY_PATH 在调用 dlopen 之前&#xff0c;使用 setenv 设置 LD_LIBRARY_PATH 环境变量 #include <jni.h> #include <cstdlib> // For setenv #incl…

【QNX+Android虚拟化方案】107 - QNX NFS Server + Android NFS Client 完整配置

【QNX+Android虚拟化方案】107 - QNX NFS Server + Android NFS Client 完整配置 一、QNX 侧 NFS Server 修改:ip 为 192.168.1.21.1 配置拷贝 nfsd、rpcbind 到 /mnt 目录下1.2 配置 exports1.3 为NFS 共享目录挂载镜像1.4 修 startup.sh 开机自启动 nfsd Server1.5 关闭 QNX…

python之os处理文件和目录的函数

1. 获取当前工作目录 import oscurrent_directory os.getcwd() print("Current Directory:", current_directory)样例输出: Current Directory: /home/user/project2. 改变当前工作目录 import osos.chdir(/path/to/new/directory) print("Changed Director…

SpringBoot3之支持GraalVM介绍

Spring Boot 3 对 GraalVM 原生镜像的支持是一个重要的更新&#xff0c;它标志着 Spring Boot 在追求更高性能和更小占用空间方面迈出了重要的一步。GraalVM 是一种高性能的运行时环境&#xff0c;它支持多种语言&#xff0c;并且可以生成原生镜像&#xff0c;这使得基于 JVM 的…