POJ 3250 Bad Hair Day(单调栈)

题目:

Bad Hair Day
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 15613 Accepted: 5219

Description

Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow is self-conscious about her messy hairstyle, FJ wants to count the number of other cows that can see the top of other cows' heads.

Each cow i has a specified height hi (1 ≤ h≤ 1,000,000,000) and is standing in a line of cows all facing east (to the right in our diagrams). Therefore, cow i can see the tops of the heads of cows in front of her (namely cows i+1, i+2, and so on), for as long as these cows are strictly shorter than cow i.

Consider this example:

        =
=       =
=   -   =         Cows facing right -->
=   =   =
= - = = =
= = = = = =
1 2 3 4 5 6 

Cow#1 can see the hairstyle of cows #2, 3, 4
Cow#2 can see no cow's hairstyle
Cow#3 can see the hairstyle of cow #4
Cow#4 can see no cow's hairstyle
Cow#5 can see the hairstyle of cow 6
Cow#6 can see no cows at all!

Let ci denote the number of cows whose hairstyle is visible from cow i; please compute the sum of c1 through cN.For this example, the desired is answer 3 + 0 + 1 + 0 + 1 + 0 = 5.

Input

Line 1: The number of cows,  N
Lines 2..N+1: Line  i+1 contains a single integer that is the height of cow  i.

Output

Line 1: A single integer that is the sum of  c 1 through  cN.

Sample Input

6
10
3
7
4
12
2

Sample Output

5

Source


题意:有n头牛,每头牛只能看到它右边比它矮的牛,直到有一头牛挡住它。问所有牛能看到的牛的数量之和。

思路:所有牛能看到的牛的数量之和等于每头牛能够被看到的次数之和,我们可以用单调栈来解决,从左到右遍历每头牛,如果栈顶的数字小于等于当前牛的高度那么就出栈,直到栈为空或栈顶元素大于当前牛的高度,这样栈中的元素的数目就等于能看到当前牛的牛的数目,这个栈是一个单调递增的栈。

代码:

#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include<climits>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;

#define PB push_back
#define MP make_pair

#define REP(i,x,n) for(int i=x;i<(n);++i)
#define FOR(i,l,h) for(int i=(l);i<=(h);++i)
#define FORD(i,h,l) for(int i=(h);i>=(l);--i)
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X) int (X); scanf("%d", &X)
#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define OI(X) printf("%d",X);
#define RS(X) scanf("%s", (X))
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
#define Swap(a, b) (a ^= b, b ^= a, a ^= b)
#define Dpoint  strcut node{int x,y}
#define cmpd int cmp(const int &a,const int &b){return a>b;}

 /*#ifdef HOME
    freopen("in.txt","r",stdin);
    #endif*/
const int MOD = 1e9+7;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef long long LL;
typedef pair<int,int> PII;
//#define HOME

int Scan()
{
	int res = 0, ch, flag = 0;

	if((ch = getchar()) == '-')				//判断正负
		flag = 1;

	else if(ch >= '0' && ch <= '9')			//得到完整的数
		res = ch - '0';
	while((ch = getchar()) >= '0' && ch <= '9' )
		res = res * 10 + ch - '0';

	return flag ? -res : res;
}
/*----------------PLEASE-----DO-----NOT-----HACK-----ME--------------------*/



int a[100000+5];
struct node
{
    int num;
    int po;
};
node q1[100000+5],q2[100000+5];
int n,m,k;
int main()
{while(RIII(n,m,k)!=EOF)
{for(int i=0;i<n;i++)
RI(a[i]);
int front1=0,rear1=0,front2=0,rear2=0;
int ans=0;
int now=0;
for(int i=0;i<n;i++)
{
    while(front1<rear1&&q1[rear1-1].num>a[i])
        rear1--;
    q1[rear1].num=a[i];
    q1[rear1++].po=i;
    while(front2<rear2&&q2[rear2-1].num<a[i])
        rear2--;
    q2[rear2].num=a[i];
    q2[rear2++].po=i;

    while(front1<rear1&&front2<rear2&&q2[front2].num-q1[front1].num>k)
    {
        if(q1[front1].po<=q2[front2].po)
        {   now=q1[front1].po+1;
            front1++;
        }
        else
        {
            now=q2[front2].po+1;
            front2++;
        }
    }
    if(front1<rear1&&front2<rear2&&q2[front2].num-q1[front1].num>=m)
        ans=max(ans,i-now+1);
}
  printf("%d\n",ans);
}



        return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值