滑动窗口记录左右的最大值

server/2024/9/22 13:46:30/

前言:看到这个题目的时候分析了一下,就是最大值问题,但是要注意分类讨论

以后遇到离散化的问题,还可以开一个map来记录存在的点,免得二分查找的点不存在


在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;const int N = (int)5e4 + 5;
int ti[N], rain[N];
int n, m;
int back, top = -1;
int b[N];
int c[N];
int ans[N];
int big[N];
vector<int> v;int record[N];int getid(int t) {return lower_bound(v.begin(), v.end(), t) - v.begin() + 1;
}map<int, int> mp;void fun1() {// 维护一个前面都比自己高的滑动窗口
//cout << getid(-110) << endl;for (int i = n; i; i--) {int u = rain[i];while (back <= top) {int pos = b[top];if (rain[pos] <= u) {ans[pos] = ti[i]; top--;}else break;}b[++top] = i;}while (back <= top) {int pos = b[top];ans[pos] = ti[1] - 1;top--;}
}void fun2() {back = 0, top = -1;for (int i = 1; i <= n; i++) {int u = rain[i];while (back <= top) {int pos = c[top];if (rain[pos] <= u) {big[pos] = ti[i]; top--;}else break;}c[++top] = i;}while (back <= top) {int pos = c[top];big[pos] = ti[n] + 1;top--;}
}int main() {cin >> n;int ma = 0;for (int i = 1; i <= n; i++) {cin >> ti[i] >> rain[i];v.push_back(ti[i]);if (ti[i] != ti[i - 1] + 1) {record[i] = record[i - 1] + 1;}else record[i] = record[i - 1];mp[ti[i]] = 1;}fun1();fun2();//for (int i = 1; i <= n; i++) {//	cout << " i " << ans[i] << " " << big[i] << endl;//	cout << record[i] << endl;//}cin >> m;for (int i = 1; i <= m; i++) {int l, r; cin >> l >> r;int idr = getid(r), idl = getid(l);if (mp[l] && mp[r]) {if (ans[idr] == l) {if (record[idr] - record[idl]) {cout << "maybe" << endl;}else cout << "true" << endl;}else cout << "false" << endl;}else if (mp[r]) {int v = ans[idr];if (v > l && mp[v]) {cout << "false" << endl;}else cout << "maybe" << endl;}else if (mp[l]) {int v = big[idl];//cout << " v " << v << endl;if (v < r && mp[v]) {cout << "false" << endl;}else cout << "maybe" << endl;}else cout << "maybe" << endl;}return 0;
}

http://www.ppmy.cn/server/100857.html

相关文章

AWS认证SAA-C03每日一题

本题库由云计算狂魔微信公众号分享。 【SAA-C03助理级解决方案架构师认证】 A company hosts an application on AWS Lambda functions that are invoked by an Amazon API Gateway API. The Lambda functions save customer data to an Amazon Aurora MySQL database Wheneve…

Unity 6 预览版正式发布

Unity 6 预览版发布啦&#xff0c;正式版本将于今年晚些时候正式发布&#xff01; 下载链接&#xff1a; https://unity.com/releases/editor/whats-new/6000.0.0 Unity 6 预览版是 Unity 6 开发周期的最后一个版本&#xff0c;在去年 11 月 Unite 大会上&#xff0c;我们宣…

【Next】全局样式和局部样式

不同于 nuxt &#xff0c;next 的样式绝大部分都需要手动导入。 全局样式 使用 sass 先安装 npm i sass -D 。 我们可以定义一个 styles 文件&#xff0c;存放全局样式。 variables.scss $fs30: 30px;mixin border() {border: 1px solid red; }main.scss use ./variables …

AI芯片:高性能卷积计算中的数据复用

随着深度学习的飞速发展&#xff0c;对处理器的性能要求也变得越来越高&#xff0c;随之涌现出了很多针对神经网络加速设计的AI芯片。卷积计算是神经网络中最重要的一类计算&#xff0c;本文分析了高性能卷积计算中的数据复用&#xff0c;这是AI芯片设计中需要优化的重点之一&a…

旅行商问题变体:欧几里德平面中线段最小连接算法

问题描述 假设在欧几里德平面上有有限多条线段&#xff0c;如何将它们连接起来&#xff0c;形成一条最小长度的线段链&#xff1f; 首先&#xff0c;自然可以穷举所有情况&#xff0c;找到最优解。还可以采用动态规划、贪心算法找到局部最优解。 另外&#xff0c;则将其作为T…

tcpdump入门——四次挥手

客户端断开tcp连接&#xff1a; 数据包分析&#xff1a; 上面抓到的四次挥手包确实展示了 TCP 连接终止的过程&#xff0c;但观察到的包顺序和标志位可能会和经典的四次握手示例稍有不同&#xff0c;这是因为在实际网络中&#xff0c;TCP 连接的终止过程可能会有一些优化或变化…

vue2子组件调用父组件传递prop得函数

在Vue中&#xff0c;props是父组件与子组件通信的桥梁。而prop的type选项可以用来指定传入的数据类型&#xff0c;以确保数据的正确性。 当prop的type为function时&#xff0c;这意味着父组件需要传递一个函数给子组件&#xff0c;子组件可以在适当的时候调用这个函数。 以下…

LeetCode 热题100-22

相交链表 给你两个单链表的头节点 headA 和 headB &#xff0c;请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点&#xff0c;返回 null 。 图示两个链表在节点 c1 开始相交&#xff1a; 题目数据 保证 整个链式结构中不存在环。 注意&#xff0c;函数返…