zoukankan      html  css  js  c++  java
  • 【BZOJ】3401: [Usaco2009 Mar]Look Up 仰望(单调栈)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3401

    还能更裸一些吗。。

    维护一个递减的单调栈

    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <string>
    #include <iostream>
    #include <algorithm>
    #include <queue>
    using namespace std;
    #define rep(i, n) for(int i=0; i<(n); ++i)
    #define for1(i,a,n) for(int i=(a);i<=(n);++i)
    #define for2(i,a,n) for(int i=(a);i<(n);++i)
    #define for3(i,a,n) for(int i=(a);i>=(n);--i)
    #define for4(i,a,n) for(int i=(a);i>(n);--i)
    #define CC(i,a) memset(i,a,sizeof(i))
    #define read(a) a=getint()
    #define print(a) printf("%d", a)
    #define dbg(x) cout << #x << " = " << x << endl
    #define printarr2(a, b, c) for1(i, 1, b) { for1(j, 1, c) cout << a[i][j]; cout << endl; }
    #define printarr1(a, b) for1(i, 1, b) cout << a[i]; cout << endl
    inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
    inline const int max(const int &a, const int &b) { return a>b?a:b; }
    inline const int min(const int &a, const int &b) { return a<b?a:b; }
    
    const int N=1e5+100;
    int n, h[N], top, q[N], d[N];
    
    void work() {
    	for1(i, 1, n) {
    		while(top && h[q[top]]<h[i]) d[q[top--]]=i;
    		q[++top]=i;
    	}
    }
    
    int main() {
    	read(n);
    	for1(i, 1, n) read(h[i]);
    	work();
    	for1(i, 1, n) printf("%d
    ", d[i]);
    	return 0;
    }
    

    Description

    约翰的N(1≤N≤105)头奶牛站成一排,奶牛i的身高是Hi(l≤Hi≤1,000,000).现在,每只奶牛都在向左看齐.对于奶牛i,如果奶牛j满足i<j且Hi<Hj,我们可以说奶牛i可以仰望奶牛j.    求出每只奶牛离她最近的仰望对象.

    Input

     
        第1行输入N,之后每行输入一个身高.

    Output

     
        共N行,按顺序每行输出一只奶牛的最近仰望对象.如果没有仰望对象,输出0.

    Sample Input

    6
    3
    2
    6
    1
    1
    2

    Sample Output

    3
    3
    0
    6
    6
    0

    HINT

    Source

  • 相关阅读:
    web自动化学习1--环境搭建以及web初识
    python接口自动化之openpyxl学习
    python接口自动化之requests学习
    接口自动化面试
    面试基础-linux
    git的使用
    php_PDO的事务处理和异常处理
    PHP_PDO简单操作
    PHP_MySql扩展主要操作
    Linux_使用yum快速安装、配置amp环境
  • 原文地址:https://www.cnblogs.com/iwtwiioi/p/3976979.html
Copyright © 2011-2022 走看看