namomo OJ contest2 C.序列

https://namomo.top:8081/contest/2/problem/C

妙啊,学到许多

首先最大的很好想,就是要尽可能长即可,那么就用单调栈处理出每个值作为最小值统治的区间,然后用线段树维护这个位置还剩多少个需要减就行了。。。其实这个就是我当时坐积木大赛的贪心做法,尽可能长

最小从群友那里学到了,把整个序列差分,那么我要让一段区间减1,就相当于在差分数组上左边+1,右边-1,那么对于差分数组中每个正数,都要找负数把它抵消掉,为了要平方最小,那么找最近的就好了,把负数坐标反着用个栈维护,对每个正数找最近的负数去抵消就行了。

upd:从群友那里又学到了,最大其实就是差分以后,负值找最近的正值抵消就行。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

const int maxl=5e5+10;
const int mod=1e9+7;

int n,m,cas,k,cnt,tot,ans,top;
int a[maxl],b[maxl],c[maxl],s[maxl];
bool in[maxl]; 
ll ansmi,ansmx;

inline void prework()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		c[i]=b[i]=a[i]-a[i-1];	
	}
	c[n+1]=b[n+1]=-a[n];
} 

inline ll sqr(ll x)
{
	return x*x%mod;
}

inline void mainwork()
{
	ansmi=0;
	for(int i=n+1;i>=1;i--)
	if(b[i]<0)
		s[++top]=i;
	for(int i=1;i<=n;i++)
	if(b[i]>0)
	{
		while(b[i]>0)
		{
			if(-b[s[top]]>b[i])
			{
				ansmi=(ansmi+sqr(s[top]-i)*b[i]%mod)%mod;
				b[s[top]]+=b[i];b[i]=0;
				break;
			}
			else
			{
				ansmi=(ansmi+sqr(s[top]-i)*(-b[s[top]])%mod)%mod;
				b[i]+=b[s[top]];b[s[top]]=0;
				top--;
			}
		}
	}
	top=0;ansmx=0;
	for(int i=1;i<=n+1;i++)
	if(c[i]>0)
		s[++top]=i;
	else
	{
		while(c[i]<0)
		{
			if(c[s[top]]>-c[i])
			{
				ansmx=(ansmx+sqr(i-s[top])*(-c[i]))%mod;
				c[s[top]]+=c[i],c[i]=0;
				break;
			}
			else
			{
				ansmx=(ansmx+sqr(i-s[top])*c[s[top]])%mod;
				c[i]+=c[s[top]];
				c[s[top]]=0;top--;
			}
		}
	}
}

inline void print()
{
	printf("%lld %lld",ansmi,ansmx);
}

int main()
{
	int t=1;
	//scanf("%d",&t);
	for(cas=1;cas<=t;cas++)
	{
		prework();
		mainwork();
		print();
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值