[AGC005B] Minimum Sum
从每一个 入手,什么情况下 在能作为一段的最小值:这一段中没有比 更小的数。我们可以设一个 从 开始往前延伸,直到 ,这一段 的最小值就是 ;在设一个 从 开始往后延伸,直到 ,这一段 的最小值也是 ,所以 作为一段最小值的情况就有 种。那么如何快速求出 和 呢,可以利用单调栈来求出左侧第一个小于自己的元素和右侧第一个小于自己的元素。
代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[300005];
int st[300005];
int l[300005];
int r[300005];
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
a[0]=1e9+1;
a[n+1]=-1e9-1;
int top=0;
for(int i=0;i<=n+1;i++){
while(top>0&&a[st[top]]>=a[i]){
top--;
}
l[i]=st[top];
st[++top]=i;
}
top=0;
for(int i=0;i<=n+1;i++){
while(top>0&&a[st[top]]>a[i]){
r[st[top]]=i;
top--;
}
st[++top]=i;
}
int cnt=0;
for(int i=1;i<=n;i++){
cnt+=a[i]*(i-l[i])*(r[i]-i);
}
cout<<cnt;
return 0;
}小公式
[COCI2010-2011#3] DIFERENCIJA
化简 ,直接两遍单调栈,一遍就最大值,一边求最小值即可。
代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[300005];
int st[300005];
int l[300005];
int r[300005];
int L[300005];
int R[300005];
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin>>n;
bool flag=0;
for(int i=1;i<=n;i++){
cin>>a[i];
}
a[0]=1e9+1;
a[n+1]=-1e9-1;
int top=0;
for(int i=0;i<=n+1;i++){
while(top>0&&a[st[top]]>a[i]){
l[st[top]]=st[top-1];
r[st[top]]=i;
top--;
}
st[++top]=i;
}
a[0]=-1e9-1;
a[n+1]=1e9+1;
top=0;
for(int i=0;i<=n+1;i++){
while(top>0&&a[st[top]]<a[i]){
L[st[top]]=st[top-1];
R[st[top]]=i;
top--;
}
st[++top]=i;
}
int cnt=0;
for(int i=1;i<=n;i++){
cnt+=a[i]*(R[i]-i)*(i-L[i])-a[i]*(r[i]-i)*(i-l[i]);
}
cout<<cnt;
return 0;
}[UESTCPC 2024] 饮料
由于前面不可预知后面是否需要预定,所以得从后往前推。如果相邻两个的时间一样,说明要预定,入栈;否则,根据相邻两个时间的 差将栈内元素弹出。
代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[200005];
int b[200005];
int st[200005];
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
if(i>a[i]){
cout<<-1;
return 0;
}
}
for(int i=1;i<=n;i++){
cin>>b[i];
}
int cnt=b[n];
int top=0;
st[++top]=b[n];
for(int i=n-1;i>=0;i--){
if(a[i]==a[i+1]){
if(top>0&&st[top]>=b[i]){
int t=st[top];
st[++top]=t;
cnt+=st[top];
}else{
st[++top]=b[i];
cnt+=b[i];
}
}else{
for(int j=1;j<=a[i+1]-a[i];j++){
if(top>0){
top--;
}else{
break;
}
}
if(top>0&&st[top]>=b[i]){
int t=st[top];
st[++top]=t;
cnt+=st[top];
}else{
st[++top]=b[i];
cnt+=b[i];
}
}
}
cout<<cnt;
return 0;
}求区间所有后缀最大值的位置
这不是求区间 的最大值,而是求区间 的最大值,这个区间的变化可以用队列来实现,这便是单调队列。
单调队列三步法:
-
让不合法的元素从尾部弹出。
-
加入新元素。
-
让不在当前区间内的元素从顶部弹出。
代码
#include<bits/stdc++.h>
using namespace std;
unsigned long long a[1000005];
int q[1000005];
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
}
int l=1,r=0;
for(int i=1;i<=n;i++){
while(l<=r&&a[q[r]]<=a[i]){
r--;
}
q[++r]=i;
while(l<=r&&q[l]<=i-m){
l++;
}
if(i>=m){
cout<<r-l+1<<"\n";
}
}
return 0;
}滑动窗口 /【模板】单调队列
两遍单调队列即可。
代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[1000005];
int q[1000005];
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
}
int l=1,r=0;
for(int i=1;i<=n;i++){
while(l<=r&&a[q[r]]>=a[i]){
r--;
}
q[++r]=i;
while(l<=r&&q[l]<=i-m){
l++;
}
if(i>=m){
cout<<a[q[l]]<<" ";
}
}
cout<<"\n";
l=1,r=0;
for(int i=1;i<=n;i++){
while(l<=r&&a[q[r]]<=a[i]){
r--;
}
q[++r]=i;
while(l<=r&&q[l]<=i-m){
l++;
}
if(i>=m){
cout<<a[q[l]]<<" ";
}
}
return 0;
}