牛客多校第四场sequence C (线段树+单调栈)
牛客多校第四场sequence C (线段树+单调栈)
传送门:https://ac.nowcoder.com/acm/contest/884/C
题意:
求一个$$ \max {1 \leq l \leq r \leq n}\left{\min \left(a{l \dots r}\right) \times \operatorname{sum}\left(b_{l \dots r}\right)\right} $$
题解:
枚举最小值
最大值可能有两种情况:两个正数相乘,两个负数相乘,我们先讨论正数相乘的情况
对于当前的\(a_i\)我们可以利用单调栈找到左边第一个比他小的数\(a_l\)和右边第一个比他小的数\(a_r\)
那么在区间\([l+1,r-1]\)的区间最小值就是\(a_i\)
我们现在已知\(a_i\)在区间内已经是最小的了,所以为了得到最大值,我们需要得到\(sum(b_{l...r})\)的最大值
将区间分为两个部分\([l+1,i]和[i,r-1]\)那么\(sum(b_{l..r})\)的最大值就是右边部分最大的前缀和减去左边部分最小的前缀和即可
对于负数相乘的情况
\(sum(b_{l...r})\)的最大值就是右边部分的最小的前缀和减去左边部分的最大前缀和即可
代码:
/**
* ┏┓ ┏┓
* ┏┛┗━━━━━━━┛┗━━━┓
* ┃ ┃
* ┃ ━ ┃
* ┃ > < ┃
* ┃ ┃
* ┃... ⌒ ... ┃
* ┃ ┃
* ┗━┓ ┏━┛
* ┃ ┃ Code is far away from bug with the animal protecting
* ┃ ┃ 神兽保佑,代码无bug
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
*/
// warm heart, wagging tail,and a smile just for you!
//
// _ooOoo_
// o8888888o
// 88" . "88
// (| -_- |)
// O\ = /O
// ____/`---'\____
// .' \| |// `.
// / \||| : |||// \
// / _||||| -:- |||||- \
// | | \ - /// | |
// | \_| ''\---/'' | |
// \ .-\__ `-` ___/-. /
// ___`. .' /--.--\ `. . __
// ."" '< `.___\_<|>_/___.' >'"".
// | | : `- \`.;`\ _ /`;.`/ - ` : | |
// \ \ `-. \_ __\ /__ _/ .-` / /
// ======`-.____`-.___\_____/___.-`____.-'======
// `=---='
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// 佛祖保佑 永无BUG
#include <set>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define ls rt<<1
#define rs rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
const int maxn = 3e6 + 5;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double Pi = acos(-1);
LL gcd(LL a, LL b) {
return b ? gcd(b, a % b) : a;
}
LL lcm(LL a, LL b) {
return a / gcd(a, b) * b;
}
double dpow(double a, LL b) {
double ans = 1.0;
while(b) {
if(b % 2)ans = ans * a;
a = a * a;
b /= 2;
} return ans;
}
LL quick_pow(LL x, LL y) {
LL ans = 1;
while(y) {
if(y & 1) {
ans = ans * x % mod;
} x = x * x % mod;
y >>= 1;
} return ans;
}
int stal[maxn], star[maxn], sta[maxn];
LL a[maxn];
LL b[maxn];
LL Max[maxn << 2];
LL Min[maxn << 2];
LL sum[maxn];
void push_up(int rt) {
Max[rt] = max(Max[ls], Max[rs]);
Min[rt] = min(Min[ls], Min[rs]);
}
void build(int l, int r, int rt) {
Max[rt] = 0;
Min[rt] = INF;
if(l == r) {
Max[rt] = sum[l];
Min[rt] = sum[l];
return;
}
int mid = (l + r) >> 1;
build(lson);
build(rson);
push_up(rt);
}
LL query_max(int L, int R, int l, int r, int rt) {
if(L <= l && r <= R) {
return Max[rt];
}
int mid = (l + r) >> 1;
LL ans = 0;
if(L <= mid) {
ans = max(ans, query_max(L, R, lson));
}
if(R > mid) {
ans = max(ans, query_max(L, R, rson));
}
return ans;
}
LL query_min(int L, int R, int l, int r, int rt) {
if(L <= l && r <= R) {
return Min[rt];
}
int mid = (l + r) >> 1;
LL ans = INF;
if(L <= mid) {
ans = min(ans, query_min(L, R, lson));
}
if(R > mid) {
ans = min(ans, query_min(L, R, rson));
}
return ans;
}
int main() {
#ifndef ONLINE_JUDGE
FIN
#endif
sum[0] = 0;
int n;
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
}
for(int i = 1; i <= n; i++) {
scanf("%lld", &b[i]);
sum[i] = sum[i - 1] + b[i];
}
LL ans = -INF;
int l = 0, r = 0;
while(l <= n && r <= n) {
while(a[l] <= 0 & l <= n) l++;
if(a[l] > 0) {
r = l;
while(a[r + 1] > 0) r++;
int top = 0;
sta[0] = l - 1;
for(int i = l; i <= r; i++) {
while(top > 0 && a[sta[top]] > a[i]) {
star[sta[top]] = i;//可以往右走的最远距离
top--;
}
sta[++top] = i;
stal[i] = sta[top - 1];//可以往左走的最远距离
}
while(top > 0) {
star[sta[top]] = r + 1;
top--;
}
for(int i = l; i <= r; i++) {
ans = max(ans, (sum[star[i] - 1] - sum[stal[i]]) * a[i]);
}
} else {
r = l;
}
l = r + 1;
}
build(0, n, 1);
for(int i = 1; i <= n; i++) {
if(a[i] < 0) {
LL maxx = query_max(0, i - 1, 0, n, 1);
LL minn = query_min(i, n, 0, n, 1);
ans = max(ans, (minn - maxx) * a[i]);
}
}
printf("%lld\n", ans);
return 0;
}
牛客多校第四场sequence C (线段树+单调栈)的更多相关文章
- 2019牛客多校第四场B xor——线段树&&线性基的交
题意 给你 $n$ 个集合,每个集合中包含一些整数.我们说一个集合表示一个整数当且仅当存在一个子集其异或和等于这个整数.现在你需要回答 $m$ 次询问 ($l, r, x$),是否 $l$ 到 $r$ ...
- Explorer(2019年牛客多校第八场E题+线段树+可撤销并查集)
题目链接 传送门 题意 给你一张无向图,每条边\(u_i,v_i\)的权值范围为\([L_i,R_i]\),要经过这条边的条件是你的容量要在\([L_i,R_i]\),现在问你你有多少种容量使得你可以 ...
- 2019牛客多校第七场C-Governing sand(线段树+枚举)
Governing sand 题目传送门 解题思路 枚举每一种高度作为最大高度,则需要的最小花费的钱是:砍掉所有比这个高度高的树的所有花费+砍掉比这个高度低的树里最便宜的m棵树的花费,m为高度低的里面 ...
- 牛客多校第四场 G Maximum Mode
链接:https://www.nowcoder.com/acm/contest/142/G来源:牛客网 The mode of an integer sequence is the value tha ...
- 牛客多校第四场 F Beautiful Garden
链接:https://www.nowcoder.com/acm/contest/142/F来源:牛客网 题目描述 There's a beautiful garden whose size is n ...
- 2019牛客多校第四场 I题 后缀自动机_后缀数组_求两个串de公共子串的种类数
目录 求若干个串的公共子串个数相关变形题 对一个串建后缀自动机,另一个串在上面跑同时计数 广义后缀自动机 后缀数组 其他:POJ 3415 求两个串长度至少为k的公共子串数量 @(牛客多校第四场 I题 ...
- 2019牛客多校第四场 A meeting
链接:https://ac.nowcoder.com/acm/contest/884/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K,其他语言10485 ...
- 2019牛客多校第四场C-sequence(单调栈+线段树)
sequence 题目传送门 解题思路 用单调栈求出每个a[i]作为最小值的最大范围.对于每个a[i],我们都要乘以一个以a[i]为区间内最小值的对应的b的区间和s,如果a[i] > 0,则s要 ...
- 【HDU】4092 Nice boat(多校第四场1006) ——线段树 懒惰标记
Nice boat Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) To ...
随机推荐
- 小爬爬1.requests基础操作
1.requests安装的问题 (1)如果requests没有安装,我们需要先安装这个模块,在cmd安装不了,我们可以在下面的位置,打开的窗体安装requests模块 pip install requ ...
- python 异常层级
- 利用IDEA构建springboot应用-数据库操作(Mysql)
Spring-Date-Jpa 定义了一系列对象持久化的标准 例如Hibernate,TopLink等 spring data jpa让我们解脱了DAO层的操作,基本上所有CRUD都可以依赖于它来 ...
- MySQL列出当前月的每一天
因为工作的原因,要用MySQL列出当前月份每一天的日期,自己查了下网上资料都是列出最近一个月的日期的解决方案,自己根据查到的的方案,修改成了下面两个方案,在此记录下: 方案一: SELECT date ...
- IO NIO AIO及常用框架概述
概述 nio 同步: 自己亲自出马持银行卡到银行取钱(使用同步IO时,Java自己处理IO读写). 异步: 委托一小弟拿银行卡到银行取钱,然后给你(使用异步IO时,Java将IO读写委托给OS处理,需 ...
- Javascript 严格模式下不允许删除一个不允许删除的属性
如下代码,在严格模式下,如果删除 Object.prototype 浏览器会报错,目前 IE10 也支持 严格模式. <script> "use strict"; de ...
- 阿里云DataWorks正式推出Stream Studio:为用户提供大数据实时计算的数据中台
5月15日 阿里云DataWorks正式推出Stream Studio,正式为用户提供大数据的实时计算能力,同时标志着DataWorks成为离线.实时双计算领域的数据中台. 据介绍,Stream St ...
- codeforces1249-div3
A B C 等比数列的性质,前面的i项的和,不会超过第i+1项 D 有若干个区间,要求每一个点被区间覆盖的次数不能超过k个.问移除的最少的区间的数目. 贪心: 若某个点被覆盖了k次以上,那么肯定是移除 ...
- laravel 使用create 报错 MassAssignmentException
在使用: 模型:create时报错, Add [name] to fillable property to allow mass assignment on [App\AdminUser].,因为 ...
- H3C 星型以太网拓扑扩展