Tyvj4875:排列 (单调栈)
来源:互联网 发布:剑三抢激活码软件 编辑:程序博客网 时间:2024/06/17 14:05
题目传送门:http://tyvj.cn/p/4875
题目分析:真是一道神题,我考试的时候想了1h都没想出来,最后只好码了个暴力,没想到正解和暴力之间就差一个小优化……
朴素的
CODE:
#include<iostream>#include<string>#include<cstring>#include<cmath>#include<cstdio>#include<cstdlib>#include<stdio.h>#include<algorithm>using namespace std;const int maxn=100100;typedef long long LL;LL sum[maxn];int small[maxn];int big[maxn];int scur,bcur;int a[maxn];int t,n;void Push(int x){ small[++scur]=x; while ( scur>1 && a[ small[scur-1] ]>a[ small[scur] ] ) scur--,small[scur]=small[scur+1]; big[++bcur]=x; while ( bcur>1 && a[ big[bcur-1] ]<a[ big[bcur] ] ) bcur--,big[bcur]=big[bcur+1];}int main(){ freopen("sum.in","r",stdin); freopen("sum.out","w",stdout); scanf("%d",&t); while (t--) { scur=bcur=0; memset(sum,0,sizeof(sum)); scanf("%d",&n); for (int i=1; i<=n; i++) scanf("%d",&a[i]); sum[0]=n; small[++scur]=n; big[++bcur]=n; for (int L=n-1; L>=1; L--) { Push(L); int last=n+1,x=1,y=1; while (small[x]!=big[y]) { int now=max(small[x],big[y]); sum[ a[ big[y] ]-a[ small[x] ] ]+=(last-now); last=now; if (big[y]>small[x]) y++; else x++; } } for (int i=1; i<n; i++) sum[i]+=sum[i-1]; for (int i=0; i<n; i++) printf("%lld\n",sum[i]); } return 0;}
阅读全文
0 0
- Tyvj4875:排列 (单调栈)
- tyvj4875:排列(单调栈)
- tyvj4875 排列(单调栈)
- BZOJ_P3100 排列(单调栈)
- LuoguP1439 排列求最长公共子序列【DP+单调栈】
- poj3250(单调栈)
- FZU2190(单调栈)
- 单调栈(山峰)
- 山峰(单调栈)
- poj2796 (单调栈)
- 单调栈(模板)
- question (单调栈)
- poj2082(单调栈)
- 单调栈 单调队列
- BZOJ 3100 排列——单调队列
- 单调队列、单调栈(第一周DIY)
- 单调栈(单调队列的孪生兄弟?)
- Poj 2082(单调栈)
- Hdu 6214 Smallest Minimum Cut(最小割)
- 期刊投稿流程
- 小程序 按钮Button样式
- 井通区块链开发指南
- entOS 6.5下高版本Firefox和selenium报错问题
- Tyvj4875:排列 (单调栈)
- Android Studio使用SVN时添加忽略文件
- linux服务器编译命令
- B1025.数字分类
- 【能源常识】如何理解“电力电量平衡”
- mybatis配置
- 数数
- hihocoder #1441 : 后缀自动机一·基本概念
- Date获取以及格式化