CodeForces 56E-Domino Principle

E - Domino Principle
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

Vasya is interested in arranging dominoes. He is fed up with common dominoes and he uses the dominoes of different heights. He put ndominoes 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. The i-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 coordinate x 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 follow n 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 the i-th domino to the right (including the domino itself).

Sample Input

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 



很久没写博客了。这是一道dp题。
先排序,在从后往前更新,每次计算第i个卡米诺骨牌的能压倒的数量的时候先考虑是否能压倒下一个骨牌,如果能,加上这个骨牌能压倒的骨牌数,在考虑下一个骨牌的压不倒的第一个骨牌,看这个骨牌能否被要求的骨牌压倒,如果能,加上这个骨牌能压倒的骨牌数,在依次往后推。。。




//#include <iostream>
//#include<algorithm>
//using namespace std;
//
//struct dom
//{
//    int x,h,num,id;
//}d[100010];
//
//int cmp1(const dom d1,const dom d2)
//{
//    if(d1.x<d2.x)
//        return 1;
//    else
//        return 0;
//}
//int cmp2(const dom d1,const dom d2)
//{
//    if(d1.id<d2.id)
//        return 1;
//    else
//        return 0;
//}
//int main()
//{
//    int n,i;
//    cin>>n;
//    for(i=0;i<n;i++)
//    {
//        cin>>d[i].x>>d[i].h;
//        d[i].id=i;
//        d[i].num=1;
//    }
//    sort(d,d+n,cmp1);
//    int cnt=0,bg=0;
//    for(i=0;i<n-1;i++)
//    {
//        int tmp=d[i].h;
//        for(int j=i+1;j<n;j++)
//        {
//            if(j>i+1)
//                tmp=max(tmp-(d[j-1].x-d[j-2].x),d[j-1].h);
//            if(tmp>d[j].x-d[j-1].x)
//            {
//                d[i].num++;
//            }
//            else
//            {
//                int t=d[i].num;
//                for(int k=i+1;k<j;k++)
//                {
//                    d[k].num=--t;
//                }
//                i=j-1;
//                break;
//            }
//        }
//    }
//    sort(d,d+n,cmp2);
//    for(i=0;i<n;i++)
//        cout<<d[i].num<<' ';
//    cout<<endl;
//    return 0;
//}


#include <iostream>
#include<algorithm>
using namespace std;

struct dom
{
    int x,h,num,id;
}d[100010];
int ans[100010];

int cmp1(const dom d1,const dom d2)
{
    if(d1.x<d2.x)
        return 1;
    else
        return 0;
}

int main()
{
    int n,i;
    cin>>n;
    for(i=0;i<n;i++)
        ans[i]=1;
    for(i=0;i<n;i++)
    {
        cin>>d[i].x>>d[i].h;
        d[i].id=i;
        d[i].num=1;
    }
    sort(d,d+n,cmp1);
    for(i=n-2;i>=0;i--)
    {
        int j=i+1;
        while(j!=n&&d[j].x<d[i].x+d[i].h)
        {
            d[i].num+=d[j].num;
            j=d[j].num+j;
        }
        ans[d[i].id]=d[i].num;
    }
    for(i=0;i<n;i++)
        cout<<ans[i]<<' ';
    cout<<endl;
    return 0;
}
View Code

原文地址:https://www.cnblogs.com/aljxy/p/3412463.html