牛客多校第四场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})\)的最大值就是右边部分的最小的前缀和减去左边部分的最大前缀和即可

代码:

  1. /**
  2. *        ┏┓    ┏┓
  3. *        ┏┛┗━━━━━━━┛┗━━━┓
  4. *        ┃       ┃  
  5. *        ┃   ━    ┃
  6. *        ┃ >   < ┃
  7. *        ┃       ┃
  8. *        ┃... ⌒ ...  ┃
  9. *        ┃       ┃
  10. *        ┗━┓   ┏━┛
  11. *          ┃   ┃ Code is far away from bug with the animal protecting          
  12. *          ┃   ┃ 神兽保佑,代码无bug
  13. *          ┃   ┃           
  14. *          ┃   ┃       
  15. *          ┃   ┃
  16. *          ┃   ┃           
  17. *          ┃   ┗━━━┓
  18. *          ┃       ┣┓
  19. *          ┃       ┏┛
  20. *          ┗┓┓┏━┳┓┏┛
  21. *           ┃┫┫ ┃┫┫
  22. *           ┗┻┛ ┗┻┛
  23. */
  24. // warm heart, wagging tail,and a smile just for you!
  25. //
  26. // _ooOoo_
  27. // o8888888o
  28. // 88" . "88
  29. // (| -_- |)
  30. // O\ = /O
  31. // ____/`---'\____
  32. // .' \| |// `.
  33. // / \||| : |||// \
  34. // / _||||| -:- |||||- \
  35. // | | \ - /// | |
  36. // | \_| ''\---/'' | |
  37. // \ .-\__ `-` ___/-. /
  38. // ___`. .' /--.--\ `. . __
  39. // ."" '< `.___\_<|>_/___.' >'"".
  40. // | | : `- \`.;`\ _ /`;.`/ - ` : | |
  41. // \ \ `-. \_ __\ /__ _/ .-` / /
  42. // ======`-.____`-.___\_____/___.-`____.-'======
  43. // `=---='
  44. // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  45. // 佛祖保佑 永无BUG
  46. #include <set>
  47. #include <map>
  48. #include <stack>
  49. #include <cmath>
  50. #include <queue>
  51. #include <cstdio>
  52. #include <string>
  53. #include <vector>
  54. #include <cstring>
  55. #include <iostream>
  56. #include <algorithm>
  57. using namespace std;
  58. typedef long long LL;
  59. typedef pair<int, int> pii;
  60. typedef unsigned long long uLL;
  61. #define ls rt<<1
  62. #define rs rt<<1|1
  63. #define lson l,mid,rt<<1
  64. #define rson mid+1,r,rt<<1|1
  65. #define bug printf("*********\n")
  66. #define FIN freopen("input.txt","r",stdin);
  67. #define FON freopen("output.txt","w+",stdout);
  68. #define IO ios::sync_with_stdio(false),cin.tie(0)
  69. #define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
  70. #define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
  71. #define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
  72. const int maxn = 3e6 + 5;
  73. const int INF = 0x3f3f3f3f;
  74. const int mod = 1e9 + 7;
  75. const double Pi = acos(-1);
  76. LL gcd(LL a, LL b) {
  77. return b ? gcd(b, a % b) : a;
  78. }
  79. LL lcm(LL a, LL b) {
  80. return a / gcd(a, b) * b;
  81. }
  82. double dpow(double a, LL b) {
  83. double ans = 1.0;
  84. while(b) {
  85. if(b % 2)ans = ans * a;
  86. a = a * a;
  87. b /= 2;
  88. } return ans;
  89. }
  90. LL quick_pow(LL x, LL y) {
  91. LL ans = 1;
  92. while(y) {
  93. if(y & 1) {
  94. ans = ans * x % mod;
  95. } x = x * x % mod;
  96. y >>= 1;
  97. } return ans;
  98. }
  99. int stal[maxn], star[maxn], sta[maxn];
  100. LL a[maxn];
  101. LL b[maxn];
  102. LL Max[maxn << 2];
  103. LL Min[maxn << 2];
  104. LL sum[maxn];
  105. void push_up(int rt) {
  106. Max[rt] = max(Max[ls], Max[rs]);
  107. Min[rt] = min(Min[ls], Min[rs]);
  108. }
  109. void build(int l, int r, int rt) {
  110. Max[rt] = 0;
  111. Min[rt] = INF;
  112. if(l == r) {
  113. Max[rt] = sum[l];
  114. Min[rt] = sum[l];
  115. return;
  116. }
  117. int mid = (l + r) >> 1;
  118. build(lson);
  119. build(rson);
  120. push_up(rt);
  121. }
  122. LL query_max(int L, int R, int l, int r, int rt) {
  123. if(L <= l && r <= R) {
  124. return Max[rt];
  125. }
  126. int mid = (l + r) >> 1;
  127. LL ans = 0;
  128. if(L <= mid) {
  129. ans = max(ans, query_max(L, R, lson));
  130. }
  131. if(R > mid) {
  132. ans = max(ans, query_max(L, R, rson));
  133. }
  134. return ans;
  135. }
  136. LL query_min(int L, int R, int l, int r, int rt) {
  137. if(L <= l && r <= R) {
  138. return Min[rt];
  139. }
  140. int mid = (l + r) >> 1;
  141. LL ans = INF;
  142. if(L <= mid) {
  143. ans = min(ans, query_min(L, R, lson));
  144. }
  145. if(R > mid) {
  146. ans = min(ans, query_min(L, R, rson));
  147. }
  148. return ans;
  149. }
  150. int main() {
  151. #ifndef ONLINE_JUDGE
  152. FIN
  153. #endif
  154. sum[0] = 0;
  155. int n;
  156. scanf("%d", &n);
  157. for(int i = 1; i <= n; i++) {
  158. scanf("%lld", &a[i]);
  159. }
  160. for(int i = 1; i <= n; i++) {
  161. scanf("%lld", &b[i]);
  162. sum[i] = sum[i - 1] + b[i];
  163. }
  164. LL ans = -INF;
  165. int l = 0, r = 0;
  166. while(l <= n && r <= n) {
  167. while(a[l] <= 0 & l <= n) l++;
  168. if(a[l] > 0) {
  169. r = l;
  170. while(a[r + 1] > 0) r++;
  171. int top = 0;
  172. sta[0] = l - 1;
  173. for(int i = l; i <= r; i++) {
  174. while(top > 0 && a[sta[top]] > a[i]) {
  175. star[sta[top]] = i;//可以往右走的最远距离
  176. top--;
  177. }
  178. sta[++top] = i;
  179. stal[i] = sta[top - 1];//可以往左走的最远距离
  180. }
  181. while(top > 0) {
  182. star[sta[top]] = r + 1;
  183. top--;
  184. }
  185. for(int i = l; i <= r; i++) {
  186. ans = max(ans, (sum[star[i] - 1] - sum[stal[i]]) * a[i]);
  187. }
  188. } else {
  189. r = l;
  190. }
  191. l = r + 1;
  192. }
  193. build(0, n, 1);
  194. for(int i = 1; i <= n; i++) {
  195. if(a[i] < 0) {
  196. LL maxx = query_max(0, i - 1, 0, n, 1);
  197. LL minn = query_min(i, n, 0, n, 1);
  198. ans = max(ans, (minn - maxx) * a[i]);
  199. }
  200. }
  201. printf("%lld\n", ans);
  202. return 0;
  203. }

牛客多校第四场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. sed 命令用法

    Sed:对文件进行编辑操作,对象是行.操作后在屏幕输出结果.如果要直接修改则加-i命令-f filename: 执行某文件内的sed命令-n 只显示被修改的那一行 如文件test内容:Letyou a ...

  2. Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十五章:第一人称摄像机和动态索引

    原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十五章:第一人称摄像机和动态索引 代码工程地址: https://g ...

  3. JAVA代码规范 标签: java文档工作 2016-06-12 21:50 277人阅读 评论(5) 收藏

    开始做java的ITOO了,近期的工作内容就是按照代码规范来改自己负责的代码,之前做机房收费系统的时候,也是经常验收的,甚至于我们上次验收的时候,老师也去了.对于我们的代码规范,老师其实是很重视的,他 ...

  4. 测试安装phpmyadmin4.0

    在测试环境准备测试安装phpmyadmin,测试环境上为一台zabbix 3.4的服务器,已经安装lamp环境. 根据安装文档,从phpmyadmin官网上下载了4.0版本,复制到/var/www/h ...

  5. CTR+A组合键 以及终止按键事件传递

    Key UP 或Down 事件中 实现CTR+A全选 if ( Control.ModifierKeys==Keys.Control && e.KeyCode == Keys.A)   ...

  6. Java练习 SDUT-1580_闰年

    闰年 Time Limit: 1000 ms Memory Limit: 32768 KiB Problem Description 时间过得真快啊,又要过年了,同时,我们的人生也增长了一年的阅历,又 ...

  7. macOS上搭建RabbitMQ+MQTT服务器

    1. 下载RabbitMQhttps://www.rabbitmq.com/install-standalone-mac.html或通过brew直接安装RabbitMQ brew install ra ...

  8. PHP_APC扩展dll上传大文件及进度条实例

    1.弄好了APC之后,就是使用它了,下面是个例子,是一个进度条上传的例子,作为笔记记录下来 在这个例子之前,我们需要做如下的设置,如果我们需要上传的是大文件的话,请在您的php.ini文件中做如下的设 ...

  9. 2019-9-2-git-需要知道的1000个问题

    title author date CreateTime categories git 需要知道的1000个问题 lindexi 2019-09-02 10:12:31 +0800 2018-2-13 ...

  10. Python--day48--ORM框架SQLAlchemy操作表

    ORM框架SQLAlchemy操作表: 表结构和数据库连接: #!/usr/bin/env python # -*- coding:utf-8 -*- from sqlalchemy.ext.decl ...