zoukankan      html  css  js  c++  java
  • 【BZOJ 1660】 [Usaco2006 Nov]Bad Hair Day 乱发节

    1660: [Usaco2006 Nov]Bad Hair Day 乱发节

    Time Limit: 2 Sec  Memory Limit: 64 MB
    Submit: 678  Solved: 326
    [Submit][Status][Discuss]

    Description

    Input

    * Line 1: 牛的数量 N。

     * Lines 2..N+1: 第 i+1 是一个整数,表示第i头牛的高度。

    Output

    * Line 1: 一个整数表示c[1] 至 c[N]的和。

    Sample Input

    6
    10
    3
    7
    4
    12
    2


    输入解释:

    六头牛排成一排,高度依次是 10, 3, 7, 4, 12, 2。



    Sample Output

    5

    3+0+1+0+1=5

    HINT

    Source


    单调队列。


    维护一个单减队列,可是要注意求值时假设有相等的,算到与他相等的后一位。


    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <cstring>
    using namespace std;
    int n,h[80005];
    struct qq
    {
    	int id,v;
    }q[80005];
    int main()
    {
        scanf("%d",&n);
    	for (int i=n;i>=1;i--)
    		scanf("%d",&h[i]);
    	long long ans=0;
    	int r=1;
    	q[1].v=h[1],q[1].id=1;
    	q[0].id=0;
    	for (int i=2;i<=n;i++)
    	{
    		while (r>=1&&q[r].v<h[i])
    			r--;
    		ans=ans+i-(q[r].id+1);
    		while (r>=1&&q[r].v==h[i])
    			r--;
    		q[++r].id=i,q[r].v=h[i];
    	}
    	cout<<ans<<endl;
    	return 0;
    }
    


  • 相关阅读:
    博客
    参考博客
    KMP
    串匹配
    简单数论
    B
    各种常用函数的模板以及自己的测试数据
    header
    memcached的图形界面监控
    缓存策略
  • 原文地址:https://www.cnblogs.com/liguangsunls/p/6923558.html
Copyright © 2011-2022 走看看