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('
    ');}
    
  • 相关阅读:
    边框颜色为 tintColor 的 UIButton
    iPhone: 在 iPhone app 里使用 UIPopoverController
    CCSprite: fade 效果切换图片
    iOS7以上: 实现如“日历”的 NavigationBar
    jqgrid自适应宽度
    NFine框架学习
    sql server 查看数据库文件的大小
    eclipse没有server选项
    MySql的数据导入到Sql Server数据库中
    eclispe javaw.exe in your current path的解决方法
  • 原文地址:https://www.cnblogs.com/ylwtsq/p/13417619.html
Copyright © 2011-2022 走看看