Norma
来源:互联网 发布:弹幕朗读软件 编辑:程序博客网 时间:2024/06/02 04:36
Norma
单调栈 好题
题目大意:
米尔科从他的祖母诺玛获得了整数数组的生日礼物。 和其他孩子一样,他希望有一些钱,但是得到了一个数组。 幸运的是,在他的城镇里有一个当铺买数组。 整数数组的代价是min·max·L元,其中min是数组中的最小整数,max是最大值,L是数组长度。 米尔科将从他的阵列中连续出售一连串的数字。 他计算了所有这些子序列的平均价格。
为了检查他的结果,他希望你做同样的事情。 他只需要知道价格的最后9位数字,所以你不需要大整数和实数。
输入:
第一行N,代表数字个数
接下来N行,每行代表数内的数。
输出:所有子序列的价格的和,保留最后九位数字。
题解:
我们枚举一个右端点,它左边会被分成一段一段的,每一段有最小值a、最大值b、长度L。也就是左端点在某一段中,这些子区间对答案的贡献就是a*b*L。
我们a和b分别是单调的,因此两个单调栈维护它们,然后再开一个答案的单调栈,它里面的断点就是a、b两个单调栈的并。
然后考虑如何维护答案。
令tot为当前固定右端点的答案。
当在右边加入长为1的一段时,前面的段对答案的贡献增加a*b*L。
因此令
注意区别何时更新delta.
而增加一个长为L的段,tot+=delta*L+新一段单独的贡献。
删除一段同理。
每次先更新a、b的单调栈,把答案单调栈里有改动的段全删掉,然后重新按新的加回来。
每次ans+=tot.
Code:
#include <iostream>#include <cstring>#include <cstdio>#define D(x) cout<<#x<<" = "<<x<<" "#define E cout<<endlusing namespace std;const int mod = 1e9;typedef long long ll;const int N = 100005;int n,a[N];ll tot,delta,ans;struct Queue{ int d[N],p[N],t; Queue(){ t=0; } int & top(){ return d[t]; } int & pos(){ return p[t]; } int len(){ return p[t]-p[t-1]; } void debug(){ for(int i=1;i<=t;i++)D(d[i]),D(p[i]),E; }} qmx, qmn, que;void inc(){ tot=( tot+ delta*que.len() )%mod; tot=( tot+ que.top()*(que.len()*(que.len()+1)/2) )%mod; delta=( delta+ que.top()*que.len() )%mod;}void dec(){ delta=( delta- que.top()*que.len() )%mod; tot=( tot- que.top()*(que.len()*(que.len()+1)/2) )%mod; tot=( tot- delta*que.len() )%mod;}int main(){ freopen("a.in","r",stdin); scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",a+i); for(int i=1;i<=n;i++){// E; D(i); E; while(qmn.t && qmn.top()>=a[i]) qmn.t--; while(qmx.t && qmx.top()<=a[i]) qmx.t--; int tp=min(qmn.pos(),qmx.pos()); while(que.t && que.pos()>tp){ dec(); que.t--; } qmn.t++; qmn.top()=a[i]; qmn.pos()=i; qmx.t++; qmx.top()=a[i]; qmx.pos()=i; int p1=qmn.t+1, p2=qmx.t+1; while(que.pos()<qmn.p[p1-1]) p1--; while(que.pos()<qmx.p[p2-1]) p2--; while(p1<=qmn.t || p2<=qmx.t){// D(p1); D(p2); E; que.t++; que.top()=qmn.d[p1]*qmx.d[p2]; que.pos()=min(qmn.p[p1],qmx.p[p2]); if(p1>qmn.t)p2++; else if(p2>qmx.t)p1++; else if(qmn.p[p1]<qmx.p[p2])p1++; else if(qmn.p[p1]>qmx.p[p2])p2++; else if(qmn.p[p1]==qmx.p[p2])p1++,p2++; inc(); } ans=( ans+tot )%mod;// puts("qmn:"); qmn.debug();// puts("qmx:"); qmx.debug();// puts("que:"); que.debug();// D(tot); D(delta); E; } printf("%lld\n",( ans+mod )%mod);}
阅读全文
0 0
- Norma
- bzoj3745: [Coci2015]Norma
- 3745: [Coci2015]Norma
- bzoj 3745: [Coci2015]Norma
- 【bzoj3745】[Coci2015]Norma
- [分治] BZOJ3745: [Coci2015]Norma
- 无法使用norma进行连接
- [分治] BZOJ 3745 [Coci2015]Norma
- 【分治计数】BZOJ3745 [Coci2015]Norma
- bzoj 3745: [Coci2015]Norma 分治&数学
- BZOJ 3745: [Coci2015]Norma cdq分治
- Fedora 编译 RT288X Makefile: *** 混合的隐含和普通规则。 停止。 Makefile: *** mixed implicit and norma
- Jenkins+Jmeter+Ant自动化环境搭建及报告生成
- Spring Boot 出现 in a frame because it set 'X-Frame-Options' to 'DENY'
- 循环的“妙用”
- POJ
- 2-SAT 题目整理
- Norma
- Nim游戏详解
- 整数划分问题
- [LeetCode]371. Sum of Two Integers
- 【个人模板】 KMP
- MyEclipse中导入数据库jar包 and JDBC实现与数据库连接
- 2017 Android github上优秀开源项目分类汇总
- 【欧拉函数】51Nod1040[最大公约数之和]题解
- SpringCloud API网关服务(Spring Cloud Zuul)