CF problem-56-E. Domino Principle 维护栈

E. Domino Principle

Vasya is interested in arranging dominoes. He is fed up with common dominoes and he uses the dominoes of different heights. He putn dominoes on the table along one axis, going from left to right. Every domino stands perpendicular to that axis so that the axis passes through the center of its base. Thei-th domino has the coordinate xi and the height hi. Now Vasya wants to learn for every domino, how many dominoes will fall if he pushes it to the right. Help him do that.

Consider that a domino falls if it is touched strictly above the base. In other words, the fall of the domino with the initial coordinatex and height h leads to the fall of all dominoes on the segment[x + 1, x + h - 1].

Input

The first line contains integer n (1 ≤ n ≤ 105) which is the number of dominoes. Then follown lines containing two integers xi and hi ( - 108 ≤ xi ≤ 108, 2 ≤ hi ≤ 108) each, which are the coordinate and height of every domino. No two dominoes stand on one point.

Output

Print n space-separated numbers zi — the number of dominoes that will fall if Vasya pushes thei-th domino to the right (including the domino itself).

Sample test(s)
Input
4
16 5
20 5
10 10
18 2
Output
3 1 4 1 
Input
4
0 10
1 5
9 10
15 10
Output
4 1 2 1 

思路: 先按牌的坐标排序,从后往前,如果第 K 张牌可以推到第 i 张, 那么理所当然 第K张牌可以推到 I 的推到的牌 + 第 I 到第K张中间的牌数;把I从栈里面pop出去;
当第K张牌前的牌,如果它能推到K,那肯定能推到I,如果不能推到K,理所当然也不能推到I; 

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<string>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<list>
#include<map>
#include<set>
#include<stack>
using namespace std;
int n;
struct node{
    int id;
    int x,h;
    int len;
    int i;
    void init(int i){
        id = i;
        scanf("%d%d",&x,&h);
        len = x + h - 1;
    }
};
node a[100010];
int ans[100010];

bool cmp(node a, node b){
    return a.x < b.x;
}

stack<node>que;

void deal(int i){

    int id = a[i].id;
    int x = a[i].len;
    ans[id] = 1;
    while(!que.empty()){
        node tp = que.top();
        if(x >= tp.x) que.pop();
        else break;
        ans[id] = max(ans[id],tp.i - a[i].i + ans[tp.id]);
    }
    que.push(a[i]);
}

void work(){

    while(!que.empty()) que.pop();
    ans[a[n-1].id] = 1;
    a[n-1].i = n - 1;
    que.push(a[n-1]);

    for(int i = n-2; i >= 0 ;i--){
        a[i].i=i;
        deal(i);
    }
}
int main()
{

//	freopen("in.in","r",stdin);
    while(~scanf("%d",&n)){
        for(int i=0; i<n; i++)
            a[i].init(i);
        sort(a,a+n,cmp);

        work();
        for(int i =0; i< n-1;i++)
            printf("%d ", ans[i]);
        printf("%d\n",ans[n-1]);

    }
	return 0;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值