POJ 3250 Bad Hair day monotonic stack

Source: Internet
Author: User

Maintaining a monotonically decreasing stack every time the home enters the top of the stack the cow must be seen by the cows inside the other stacks

#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 80010;
typedef unsigned long long LL;
int A[MAXN], S[MAXN];
int main ()
{
	int n;
	scanf ("%d", &n);
	for (int i = 1; I <= n; i++)
		scanf ("%d", &a[i]);
	int top = 0;
	LL ans = 0;
	for (int i = 1, i <= N; i++)
	{while
		(top && a[i] >= A[s[top]])
			top--;
		Ans + = top;
		S[++top] = i;
	}	
	printf ("%llu\n", ans);
	return 0;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.