牛客多校第四场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 (线段树+单调栈)的更多相关文章

  1. 2019牛客多校第四场B xor——线段树&&线性基的交

    题意 给你 $n$ 个集合,每个集合中包含一些整数.我们说一个集合表示一个整数当且仅当存在一个子集其异或和等于这个整数.现在你需要回答 $m$ 次询问 ($l, r, x$),是否 $l$ 到 $r$ ...

  2. Explorer(2019年牛客多校第八场E题+线段树+可撤销并查集)

    题目链接 传送门 题意 给你一张无向图,每条边\(u_i,v_i\)的权值范围为\([L_i,R_i]\),要经过这条边的条件是你的容量要在\([L_i,R_i]\),现在问你你有多少种容量使得你可以 ...

  3. 2019牛客多校第七场C-Governing sand(线段树+枚举)

    Governing sand 题目传送门 解题思路 枚举每一种高度作为最大高度,则需要的最小花费的钱是:砍掉所有比这个高度高的树的所有花费+砍掉比这个高度低的树里最便宜的m棵树的花费,m为高度低的里面 ...

  4. 牛客多校第四场 G Maximum Mode

    链接:https://www.nowcoder.com/acm/contest/142/G来源:牛客网 The mode of an integer sequence is the value tha ...

  5. 牛客多校第四场 F Beautiful Garden

    链接:https://www.nowcoder.com/acm/contest/142/F来源:牛客网 题目描述 There's a beautiful garden whose size is n ...

  6. 2019牛客多校第四场 I题 后缀自动机_后缀数组_求两个串de公共子串的种类数

    目录 求若干个串的公共子串个数相关变形题 对一个串建后缀自动机,另一个串在上面跑同时计数 广义后缀自动机 后缀数组 其他:POJ 3415 求两个串长度至少为k的公共子串数量 @(牛客多校第四场 I题 ...

  7. 2019牛客多校第四场 A meeting

    链接:https://ac.nowcoder.com/acm/contest/884/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K,其他语言10485 ...

  8. 2019牛客多校第四场C-sequence(单调栈+线段树)

    sequence 题目传送门 解题思路 用单调栈求出每个a[i]作为最小值的最大范围.对于每个a[i],我们都要乘以一个以a[i]为区间内最小值的对应的b的区间和s,如果a[i] > 0,则s要 ...

  9. 【HDU】4092 Nice boat(多校第四场1006) ——线段树 懒惰标记

    Nice boat Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) To ...

随机推荐

  1. [React Native]去掉WebStorm中黄色警告

    用WebStorm开发RN难免会碰到一大堆黄色警告.就像下面这样. 其实这个错误并不会影响开发,但是作为一个上升处女座的,我很难忍.于是各种想办法. 上网查了半天发现这篇文章 http://blog. ...

  2. Dom4j(Dom for Java) Day24

    TestDomForJava.java package com.sxt.dom4j; import java.io.File; import java.util.Iterator; import ja ...

  3. [java]网上商城错误集锦 2016-05-08 21:49 499人阅读 评论(32) 收藏

    网上商城敲到了第三天,马上就要踏入第四天啦,不过敲得这几天,学习到了不少东西,也接触了很多新东西,当然,遇到最多的,就是各种bug!下面总结一下自己遇到的这些bug. 一.时间获取不到 这个bug起源 ...

  4. Vue CLI图形化创建项目

  5. hdu 1503 LCS输出路径【dp】

    hdu 1503 不知道最后怎么输出,因为公共部分只输出一次.有人说回溯输出,感觉好巧妙!其实就是下图,输出的就是那条灰色的路径,但是初始时边界一定要初始化一下,因为最第一列只能向上走,第一行只能向左 ...

  6. 三 nginx+uWSGI+django+virtualenv+supervisor发布web服务器

    https://www.cnblogs.com/pyyu/p/9481344.html?tdsourcetag=s_pcqq_aiomsg 一 uwsgi安装 1.安装uwsgi,进入到一个虚拟机环境 ...

  7. C++类继承中的虚方法

    #include <bits/stdc++.h> using namespace std; class A { public: void Show() { cout << &q ...

  8. part12.5-定时器去抖

  9. Mysql 数据库优化(一)

    一 避免网页访问错误 1  数据库连接timeout产生页面5xx错误 2 慢查询造成页面无法加载 3 阻塞造成数据无法提交 二 增加数据库的稳定性 三 优化用户体验 1 流畅的页面访问速度 2 良好 ...

  10. @bzoj - 4380@ [POI2015] Myjnie

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 有 n 家洗车店从左往右排成一排,每家店都有一个正整数价格 p[ ...