zoukankan      html  css  js  c++  java
  • [POI2008]PLA-Postering

    题目链接

    [POI2008]PLA-Postering

    description

    春春幼儿园升级版

    solution

    此题数据较强,于是我们可以用单调栈来维护.具体维护方式如下:

    首先将(ans)初始化为(n),对于当前高度(h),如果当前栈顶大于(h),则将栈顶弹出,直到栈顶不大于(h)或栈空.进行此番操作后,将栈顶元素与(h)比较,若相等则(--ans).然后将当前元素入栈即可.

    正确性证明:(大眼观察法)我们容易发现,宽度是无关变量,答案只与高度有关.而且,(n)是肯定满足答案的,所以我们可以从(n)开始考虑.欲省掉一张海报,当且仅当两值之间的值均大于此二值且此二值相等,所以可以用单调栈维护.

    code

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<cstdlib>
    #include<queue>
    #define R register
    #define next jkflsdlkf
    #define debug puts("mlg")
    #define mod 10007
    #define Mod(x) ((x%mod+mod)%mod)
    using namespace std;
    typedef long long ll;
    typedef long double ld;
    typedef unsigned long long ull;
    inline ll read();
    inline void write(ll x);
    inline void writesp(ll x);
    inline void writeln(ll x);
    ll n,_stack[1000000],t,ans;
    int main(){
    	ans=n=read();
    	for(R ll i=1,h;i<=n;i++){
    		read();h=read();
    		while(t&&_stack[t]>h) t--;
    		if(_stack[t]==h) --ans;
    		_stack[++t]=h;
    	}
    	writeln(ans);
    } 
    inline ll read(){ll x=0,t=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-') t=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*t;}
    inline void write(ll x){if(x<0){putchar('-');x=-x;}if(x<=9){putchar(x+'0');return;}write(x/10);putchar(x%10+'0');}
    inline void writesp(ll x){write(x);putchar(' ');}
    inline void writeln(ll x){write(x);putchar('
    ');}
    
  • 相关阅读:
    1265 四点共面
    1003 阶乘后面0的数量
    1080 两个数的平方和
    1090 3个数和为0
    1087 1 10 100 1000
    1082 与7无关的数
    OpenLayers工作原理
    CI(持续集成)CD(持续交付)
    打包命令
    文件与目录管理重点
  • 原文地址:https://www.cnblogs.com/ylwtsq/p/13417619.html
Copyright © 2011-2022 走看看