QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#140212#5500. BarsPhantomThreshold#TL 0ms5468kbC++20859b2023-08-15 14:48:082023-08-15 14:48:09

Judging History

你现在查看的是最新测评结果

  • [2023-08-15 14:48:09]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:5468kb
  • [2023-08-15 14:48:08]
  • 提交

answer

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

const int maxn = 500050;

ll ans;
int n;
int pi[maxn];
pair<int,int>ai[maxn];

void go(int l,int r,int i)
{
	if(i>n || ( pi[l]>=ai[i].first && pi[r]>=ai[i].first ) ) return;
	int pos= ai[i].second;
	if(pos<=l||pos>=r) go(l,r,i+1);
	
	ll temp= 1ll*(pos-l)*(ai[i].first-pi[r])+ 1ll*(r-pos)*(ai[i].first-pi[l]);
	if(temp<=0) go(l,r,i+1);
	else
	{
		ans+=temp;
		go(l,pos,i+1);
		go(pos,r,i+1);
	}
}

signed main()
{
	ios_base::sync_with_stdio(false);
	
	int Tcase; cin>>Tcase;
	while(Tcase--)
	{
		cin>>n;
		for(int i=1;i<=n;i++) 
		{
			cin>>pi[i];
			ai[i]=make_pair(pi[i],i);
		}
		sort(ai+1,ai+n+1,greater< pair<int,int> >());
		
		ans=1ll*(n-1)*(pi[1]+pi[n]);
		
		go(1,n,1);
		
		cout<<ans<<endl;
	}
	
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 5468kb

input:

2
4
5 2 2 6
5
1 5 4 4 1

output:

33
29

result:

ok 2 lines

Test #2:

score: -100
Time Limit Exceeded

input:

10000
4
5 2 2 6
5
1 5 4 4 1
197
763787596 15221694 898228999 187472305 466351873 822742732 437754202 800092772 843092246 915675776 166265020 346340615 796714085 497548541 182089610 64356048 363276768 181268733 257949015 236568898 752096761 928725929 443146784 114577469 833053207 38120723 14891030 41...

output:

33
29

result: