zoukankan      html  css  js  c++  java
  • [bzoj1660][Usaco2006 Nov]Bad Hair Day_单调栈

    Bad Hair Day bzoj-1660 Usaco-2006 Nov

    题目大意:n头牛站成一列,每头牛向后看。f[i]表示第i头牛到第n头牛之间有多少牛,使得这些牛都比i矮,且中间没有比i高的牛阻隔。求$sumlimits_{i=1}nf[i]$。

    注释:$1le nle 8cdot 10^4$。


    想法:显然,直接用单调栈维护。我开始用的是权值线段树然后没调出来... ...

    最后,附上丑陋的代码... ...

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    int n,top,a[80001],s[80001];
    long long ans;
    int main()
    {
    	scanf("%d",&n);
    	for(int i=1;i<=n;i++)
    	   scanf("%d",&a[i]);
    	for(int i=1;i<=n;i++)
    	{
    		if(a[i]<s[top])
    			ans+=top;
       		else
       		{
       			while(a[i]>=s[top]&&top) top--;
    			ans+=top;
       		}
       		s[++top]=a[i];
    	}
    	printf("%lld",ans);
    	return 0;
    }
    

    小结:线段树真强,但是有一些其他的东西更巧妙...

  • 相关阅读:
    仙人球的残影
    Calculate the formula
    自定义代码段
    getter-setter方法练习
    封装
    Xcode文档安装
    Xcode模板修改
    匿名对象
    OC多文件开发介绍
    #pragma mark指令
  • 原文地址:https://www.cnblogs.com/ShuraK/p/9374935.html
Copyright © 2011-2022 走看看