上了洛谷提高课的帮我一把
  • 板块灌水区
  • 楼主zero2005
  • 当前回复0
  • 已保存回复0
  • 发布时间2020/10/10 21:15
  • 上次更新2023/11/5 11:15:48
查看原帖
上了洛谷提高课的帮我一把
182260
zero2005楼主2020/10/10 21:15

模拟赛T2莫名WA

#include<bits/stdc++.h>
using namespace std;
const int maxn=2000001;
int a[maxn];
int c[maxn];
int n,m;
stack< pair<int,int> >p;
queue< pair<int,int> >q; 
int ans1=0;int ans2=0;
void tanxin1()
{
	for(int i=1;i<=n+1;i++)
	{
		if(c[i]>0)
		{
			q.push(make_pair(c[i],i)); 
		}
		if(c[i]<0)
		{
			int s=c[i];
			while(s<0)
			{
				int x=q.front().first;
				int y=q.front() .second;
			    if(s+x<=0)
			    {
			    ans1+=x*(pow(i-y,2));
				s+=x;
				q.pop() ;
				}
				else
				{
					ans1+=-s*(pow(i-y,2));
					q.front().first+=s;
					break;
				}
			}
		}
	}
	
}
void tanxin2()
{
		for(int i=1;i<=n+1;i++)
	{
		if(c[i]>0)
		{
			p.push(make_pair(c[i],i)); 
		}
		if(c[i]<0)
		{
			int s=c[i];
			while(s<0)
			{
				int x=p.top().first;
				int y=p.top() .second;
			    if(s+x<=0)
			    {
			    ans2+=x*(pow(i-y,2));
				s+=x;
				p.pop() ;
				}
				else
				{
					ans2+=-s*(pow(i-y,2));
					p.top().first+=s;
					break;
				}
			}
		}
	}
}
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		c[i]=a[i]-a[i-1];
	}
	c[n+1]=0-a[n];
//	for(int i=1;i<=n+1;i++)
	//cout<<c[i]<<' ';
	tanxin1();
	cout<<ans1<<'\n';
	
	tanxin2();
	cout<<ans2;
	return 0;
}
2020/10/10 21:15
加载中...