The Preliminary Contest for ICPC China Nanchang National Invitational I.Max answer单调栈

题面

题意:一个5e5的数组,定义一个区间的值为 这个区间的和*这个区间的最小值,注意数组值有负数有正数,求所有区间中最大的值

题解:如果全是正数,那就是原题 POJ2796 单调栈做一下就ok

我们现在有负数,考虑这段区间,他的和必须是负数,由于导致和为负数,最小值一定也是负数,

那对于这样一个和为负的区间进行扩展的时候,遇见下一个数,是负数,我们一定会扩展,无论这个负数大小

遇见下一个是正数,如果和没有变正,那就可以继续扩展下去(不更新答案罢了)

所以我们对于那些和为负的区间,单独统计一下答案就好了,顺路就统计这个区间的最小值,因为一旦抛弃她就再也不会回头了!

所以压根不用线段树 ST表之类的,对于负数的和,整体按POJ2796做的取个min,就行了.复杂度O(n)

 1 #include<bits/stdc++.h>
 2 #define lld long long
 3 #define N 500050
 4 using namespace std;
 5 int n;
 6 lld a[N],sum[N],x[N],xx[N];
 7 lld L[N],R[N],ansv,ans1;
 8 int main ()
 9 {
10     scanf("%d", &n);
11     for (int i=1;i<=n;i++)
12     {
13         scanf("%lld",&x[i]);
14         xx[i]=x[i];
15     }
16     lld ww=xx[1],minx=xx[1];
17     for (int i=1;i<=n;i++)
18     {
19         if (xx[i-1]<0)
20         {
21               minx=min(minx,xx[i]);
22             xx[i]+=xx[i-1];
23         }else
24         {
25             xx[i]+=0;
26             minx=1e18;
27         }
28         if (xx[i]<ww)
29         {
30             ww=xx[i];
31             ansv=minx;
32         }
33     }
34     if (ww<0) ans1=ww*ansv;
35     for (int i=1;i<=n;i++) a[i]=x[i];
36     for (int i=1;i<=n;i++)
37     {
38         sum[i]=sum[i-1]+a[i];
39         L[i]=R[i]=i;
40      }
41      a[0]=a[n+1]=-1e9;
42     for (int i=1;i<=n;i++)
43         while(a[i]<=a[L[i]-1]) L[i]=L[L[i]-1];
44     for (int i=n;i>=1;i--)
45         while(a[i]<=a[R[i]+1]) R[i]=R[R[i]+1];
46     lld ans=-1e18,l,r;
47     for (int i=1;i<=n;i++)
48     {
49         lld T=a[i]*(sum[R[i]]-sum[L[i]-1]);
50         if(ans<T) ans=T,l=L[i],r=R[i];
51     }
52     cout<<max(ans,ans1)<<endl;
53 }

原文地址:https://www.cnblogs.com/qywhy/p/10747666.html

时间: 2024-10-09 21:50:08

The Preliminary Contest for ICPC China Nanchang National Invitational I.Max answer单调栈的相关文章

The Preliminary Contest for ICPC China Nanchang National Invitational and International Silk-Road Programming Contest

打网络赛 比赛前的准备工作要做好 确保 c++/java/python的编译器能用 打好模板,放在桌面 A. PERFECT NUMBER PROBLEM 1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <cstring> 5 #include <string> 6 #include <algorithm> 7 #include <se

The Preliminary Contest for ICPC China Nanchang National Invitational I题

Alice has a magic array. She suggests that the value of a interval is equal to the sum of the values in the interval, multiplied by the smallest value in the interval. Now she is planning to find the max value of the intervals in her array. Can you h

The 2019 ICPC China Nanchang National Invitational and International Silk-Road Programming Contest C. Xyjj’s sequence(动态规划+欧拉降幂)

题目链接:https://nanti.jisuanke.com/t/40255 中文题面: 解题思路:先用欧拉降幂求出A,B两个序列,定义dp[0][i][j]为取A的前i个元素,B的前j个元素,且C的最后一个元素为B[j],同理dp[1][i][j]为取A的前i个元素,B的前j个元素,且C的最后一个元素为A[i],那么就很容易得到状态转移方程.那么最后答案即为max(dp[0][n][n],dp[1][n][n]).还有值得注意的是:该题需要使用滚动数组,不然会超内存. 在此贴两个关于欧拉降幂

The 2019 ICPC China Nanchang National Invitational and International Silk-Road Programming Contest E. Interesting Trip 长链剖分

题库链接 考虑莫比乌斯, 套上去之后就是变成了统计长度为d的一共有多少路径, 直接长链剖分, 在计蒜客上极度卡常, 卡了一万年才卡过去, 现场好像还有用点分治过去的, 这都能过?? #include<bits/stdc++.h> #define LL long long using namespace std; const int N = (int)5e5 + 7; const int M = 30000; int n, d, a[N], vis[N], miu[M + 1]; int now

The 2019 ICPC China Nanchang National Invitational and International Silk-Road Programming Contest - F.Sequence(打表+线段树)

题意:给你一个长度为$n$的数组,定义函数$f(l,r)=a_{l} \oplus a_{l+1} \oplus...\oplus a_{r}$,$F(l,r)=f(l,l)\oplus f(l,l+1)\oplus ...\oplus f(l,r)\oplus f(l+1,l+1)\oplus ...f(l+1,r)\oplus ...\oplus f(r,r)$,有两种操作,第一种将数组中某个元素$a[x]$变为$y$,第二种计算$F(l,r)$的值. 思路:打表后发现只有当$l$和$r$同

The Preliminary Contest for ICPC Asia Nanchang 2019

目录 Solutions B. Fire-Fighting Hero E. Magic Master Link Solutions B. Fire-Fighting Hero 题意: 思路: dijkstra最短路 先以 hero 为起点 跑一遍 dijkstra 建立 起点 \(p\) 并与各 fire-fighting point 建立权为 \(0\) 的边,跑一遍 dijkstra 第二次 dijkstra 中 dis[i] 为各 fire-fighting point 到各点的最短路径中

C:Dawn-K&#39;s water (The Preliminary Contest for ICPC Asia Shenyang 2019)

Dawn-K recently discovered a very magical phenomenon in the supermarket of Northeastern University: The large package is not necessarily more expensive than the small package. On this day, Dawn-K came to the supermarket to buy mineral water, he found

The Preliminary Contest for ICPC Asia Shenyang 2019

The Preliminary Contest for ICPC Asia Shenyang 2019 Texas hold'em Poker #include <bits/stdc++.h> using namespace std; const int maxn=1e6+10; int num[1000]; int shun(){ for (int i=15;i>=5;i--){ if (num[i]&&num[i-1]&&num[i-2]&&a

The Preliminary Contest for ICPC Asia Shanghai 2019 C Triple(FFT+暴力)

The Preliminary Contest for ICPC Asia Shanghai 2019 C Triple(FFT+暴力) 传送门:https://nanti.jisuanke.com/t/41400 题意: 给你三个数组a,b,c,要你求有多少个三元组(i,j,k),使得 \[ \begin{array}{l}{\left|A_{i}-B_{j}\right| \leq C_{k}, \text { and }} \\ {\left|B_{j}-C_{k}\right| \leq