首页 > 其他 > 详细

foj 2014-4月赛 多米诺骨牌 单调栈

时间:2014-04-14 11:12:13      阅读:495      评论:0      收藏:0      [点我收藏+]

题目来源:

http://acm.fzu.edu.cn/problem.php?pid=2163

 

代码如下:

 

bubuko.com,布布扣
using namespace std ;
typedef long long LL ;
const int Max_N = 100005;
struct Elem{
    int x,h,id,index;
    bool operator<(const Elem&  p)const{
        return x<p.x;
    }
};
Elem data[Max_N], st[Max_N];
int ans[Max_N];
int top,n;
void Init(){
    for(int i=1; i <= n; i++){
        scanf("%d%d",&data[i].x,&data[i].h);
        data[i].id=i;
    }
    sort(data+1, data+n+1);
}
//单调栈 写法 与凸包模板差不多
void solve(){
    int N=n+1;
    data[N].x=1<<30;
    for(int i=1; i<= N ; i++)
        data[i].index=i;
    st[1]=data[1];
    top=1;
    for(int i = 2; i<=N; i++){
        while(top>=1 && st[top].x + st[top].h - 1 < data[i].x){
            ans[st[top].id ]=  data[i].index - st[top].index ;
           // printf("ans[%d]=%d\n",st[top].id , ans[st[top].id ]);
            top--;
        }
        st[++top]= data[i];
    }
}
int main(){
    while(scanf("%d",&n) != EOF){
        Init();
        solve();
        printf("%d",ans[1]);
        for(int i=2; i<=n ; i++){
            printf(" %d",ans[i]);
        }
        puts("");
    }
    return 0;
}
bubuko.com,布布扣

 

foj 2014-4月赛 多米诺骨牌 单调栈,布布扣,bubuko.com

foj 2014-4月赛 多米诺骨牌 单调栈

原文:http://www.cnblogs.com/zn505119020/p/3662853.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!