제출 #6112

#제출 시각아이디문제언어결과실행 시간메모리
6112kriii즐거운 채소 기르기 (JOI14_growing)C++98
0 / 100
4 ms4756 KiB
#include <stdio.h>
#include <stack>
using namespace std;

int N,D[300030],L[300030],R[300030];
stack<pair<int, int> > S; int lf;

int main()
{
	scanf ("%d",&N);
	for (int i=0;i<N;i++) scanf ("%d",&D[i]);
	for (int i=0;i<N;i++){
		int l=-1,c=0;
		while (!S.empty() && S.top().first <= D[i]){
			l = S.top().first;
			c = S.top().second;
			lf -= c;
			S.pop();
		}
		L[i] = lf;
		if (l == D[i]) c++;
		else c = 1;
		S.push(make_pair(D[i],c));
		lf += c;
	}

	while (!S.empty()) S.pop(); lf = 0;
	for (int i=N-1;i>=0;i--){
		int l=-1,c=0;
		while (!S.empty() && S.top().first <= D[i]){
			l = S.top().first;
			c = S.top().second;
			lf -= c;
			S.pop();
		}
		R[i] = lf;
		if (l == D[i]) c++;
		else c = 1;
		S.push(make_pair(D[i],c));
		lf += c;
	}

	long long ans = 0;
	for (int i=0;i<N;i++) ans += L[i] < R[i] ? L[i] : R[i];

	printf ("%lld\n",ans);

	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...