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

  • 相关阅读:
    P1440 求m区间内的最小值
    P1569 Generic Cow Protests
    P3252 [JLOI2012]树
    P3009 [USACO11JAN]Profits S
    <二分查找+双指针+前缀和>解决子数组和排序后的区间和
    常见算法技巧之——双指针思想
    设计模式——单例模式
    操作系统实验——读者写者模型(写优先)
    操作系统——内存管理学习笔记
    Altium Designer 16下载与安装教程
  • 原文地址:https://www.cnblogs.com/iwtwiioi/p/3976979.html
Copyright © 2011-2022 走看看