当前位置:网站首页>Luogu p2422 good feeling solution
Luogu p2422 good feeling solution
2022-07-19 13:52:00 【q779】
Luogu P2422 A good feeling Answer key
Topic link :P2422 A good feeling
The question :
kkk Made a human sensory analyzer . every single day , Everyone has a feeling value A i A_i Ai, A i A_i Ai The bigger it is , It means that people feel more comfortable . For a while [ i , j ] \left[i, j\right] [i,j] Inside , Human comfort is defined as [ i , j ] \left[i, j\right] [i,j] The feeling value of the most uncomfortable day in × \times × [ i , j ] \left[i, j\right] [i,j] The sum of the feeling value of each day . Now give kkk In succession N N N The feeling value in the sky , Excuse me, , At what time ,kkk Feel the most comfortable ?
about 100 % 100\% 100% The data of , 1 ≤ N ≤ 100000 1\le N\le 100000 1≤N≤100000, 1 ≤ Receptive value ≤ 1000000 1\le \texttt{ Receptive value }\le 1000000 1≤ Receptive value ≤1000000.
To simplify the meaning of the question is
Find an interval , Multiply the minimum value of the interval by the sum of the interval
Consider the contribution of each minimum to the interval
Let the current number be a i a_i ai ,
It can become the interval of the minimum [ l , r ] [l,r] [l,r] Satisfy l ∈ [ j + 1 , i ] , r ∈ [ i , k − 1 ] l\in [j+1,i],r\in [i,k-1] l∈[j+1,i],r∈[i,k−1]
among j j j It's all a j < a i a_j <a_i aj<ai The largest of j j j , k k k It's all a k < a i a_k < a_i ak<ai The smallest of all k k k
set up f i f_i fi Before presentation i i i The largest answer in the number
f i = ( f j + S i − S j ) + ( f k + S k − 1 − S i ) f_i = (f_j + S_i-S_j)+(f_k+S_{k-1}-S_i) fi=(fj+Si−Sj)+(fk+Sk−1−Si)
there j , k j,k j,k Synonymous with the above , S i S_i Si Prefix and , namely S i = ∑ j = 1 i a j S_i = \sum_{j=1}^{i}a_j Si=∑j=1iaj
So the monotone stack is used to maintain the minimum , From left to right , You can finish the left half
So what happened to the right half ?
It's not hard to find out a k a_k ak It must be the first one to add to the monotone stack a i a_i ai Number kicked out
We just have to kick a i a_i ai When the time is right i i i To k − 1 k-1 k−1 Contribution to add f i f_i fi On it
Be sure to put a n + 1 a_{n+1} an+1 Set to a minimum , So that it can kick out all the front
Time complexity O ( n ) O(n) O(n)
Code :
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iomanip>
#include <random>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
#define N (int)(1e5+15)
int n,top,a[N],stk[N],f[N],sum[N];
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
// freopen("check.in","r",stdin);
// freopen("check.out","w",stdout);
cin >> n;
for(int i=1; i<=n; i++)
cin >> a[i];
a[++n]=0;
for(int i=1; i<=n; i++)
{
sum[i]=sum[i-1]+a[i];
while(a[stk[top]]>a[i])
{
f[stk[top]]+=sum[i-1]-sum[stk[top]];
--top;
}
f[i]=sum[i]-sum[stk[top]];
stk[++top]=i;
}
int res=0;
for(int i=1; i<n; i++)
res=max(res,f[i]*a[i]);
cout << res << '\n';
return 0;
}
Reprint please explain the source
边栏推荐
- uniapp 高德地图定位功能
- Codeforces Round #808 (Div. 2)ABCD
- 关于XML文件(七)- XML DTD
- QT use qlisview to realize QQ login history list
- CBS类型PVC回收策略
- Analyze and hook sshd to hijack password
- Li Kou's 302 weekly match
- Deep learning from getting started to giving up the 100 day challenge
- Codeforce:g. good key, bad key [greed]
- [7.14] code source - [open box] [XOR inverse] [continuous subsequence] [trigonometric fruit count]
猜你喜欢

Start from here by counting the information of the broadcast room

「津津乐道播客」#392 原汤话原食:仲夏夜,马砂、肉串儿、趿拉板儿

NO.6浮点数的表示与运算

FreeRTOS personal notes - multi priority support

Onvif protocol related: 4.1.3 WS username token method to obtain screenshot URL

onvif协议相关:3.1.3 Digest方式获取截图url
![[postgraduate entrance examination vocabulary training camp] day 5 - alarm, cooperate, point, benefit, industrial, revolution, mechanism](/img/50/07dd47d1bc46681e19d133b2e219c4.png)
[postgraduate entrance examination vocabulary training camp] day 5 - alarm, cooperate, point, benefit, industrial, revolution, mechanism
![[code hoof set novice village question 600] format specifier of float and double](/img/19/433f794617f3c3c72732f1a4efe252.png)
[code hoof set novice village question 600] format specifier of float and double
![Codeforce:a. doremy's IQ [reverse greed]](/img/3d/065f9f1cbd857d324b6ed074d1afbf.png)
Codeforce:a. doremy's IQ [reverse greed]

Onvif protocol related: 3.1.1 digest access authorization
随机推荐
[code hoof set novice village 600 question] calculate the number of digits of an integer
【码蹄集新手村 600 题】科学计数法的实现方式,输出指数形式
[7.15] code source - [neat array 2] [ternary cycle] [reverse pair on tree] [sequence of cochlea]
onvif协议相关:2.1.3 none方式获取流地址
【7.14】代码源 -【拆方块】【XOR Inverse】【连续子序列】【三角果计数】
Flutter uses animatedswitcher to switch scenes
AcWing 136. 邻值查找
Onvif protocol related: 3.1.4 get the stream address in digest mode
[7.14] code source - [open box] [XOR inverse] [continuous subsequence] [trigonometric fruit count]
命令行的一些常用操作命令及常见错误的解决办法
Helloword and led: a quick start to Hongmeng device development -- Huawei cloud 14 day Hongmeng device development practical learning notes Chapter 2
「津津樂道播客」#392 原湯話原食:仲夏夜,馬砂、肉串兒、趿拉板兒
关于XML文件(七)- XML DTD
TKE(K8S)部署mysql使用CFS存储
[code hoof set novice village question 600] operator / type conversion in different operation sequences
No.3汇编进阶
Running node for getting started with eth
深度学习从入门到放弃100天挑战
ModuleNotFoundError: No module named ‘_distutils_hack‘
2. Sum of three numbers