Monday, 22 February 2016

E. Domino Principle
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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 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 



First and the obvious aprroach is to sort the arrays int ascending/ descending order od starting indexes and 
then travese either backward or forward resepctively to get the answer.

Now the obvious soltion is an o(n^2) approach where for a decreasing start for any index we add the number of intemediate 
indexes + dp[j]

   i.e
         for(int i=1;i<=n;i++)
         {
             for(int j=i-1;j>=1;j--)
               {
                    if(dominoes j will fall when dominoe i is pushed)
                          dp[i]=max(dp[i],i-j+dp[j);
                }
          }
  Now this approach is 0(n^2) and will obviously fail, What we do is change the inner loop trickily

             i.e.  for(int j=i-1;j>=1;j-=dp[j])
                    {
                            //once we do this we substract go the index 
                            //  that doesnot fall due to 
                         // jth element , if the end of the ith dominoe
                       // is still covered by any dominoe we continue.
                          else
                             we break;
                     }

It may seem at first that the complexity of the jth loop is O(n) but we see that the inner loop travels a total of 
n times. summation(all traversals by j loop)=n;
                   
          This happens becuase suppose we have three element
 
                i  j.........k   l

  Now if the falling of i can cover j then we directly jump to l,and if it doesnot cover j then obviously it wont cover 
l.

Code


#include<bits/stdc++.h>
#define pb push_back
using namespace std;
struct node
{
 int s;
 int h;
 int dp;
 int id;
 }    a[100010];
bool comp(node a,node b)
{
 return a.s>b.s;
}
int ans[100010];
int main()
{
 int n;
 cin>>n;
 for(int i=1;i<=n;i++)
 {
   
   cin>>a[i].s>>a[i].h;
     a[i].id=i;
 }
 sort(a+1,a+1+n,comp);
 for(int i=1;i<=n;i++)
 a[i].dp=1;
 for(int i=1;i<=n;i++)
 {
   for(int j=i-1;j>=1;j-=a[j].dp)
   {
         if(a[j].s>a[i].s+(a[i].h)-1)
 break;
 a[i].dp+=a[j].dp;
 }
//  cout<<a[i].id<<" "<<a[i].dp<<endl;
 }
 
 for(int i=1;i<=n;i++)
 ans[a[i].id]=a[i].dp;
 for(int i=1;i<=n;i++)
 cout<<ans[i]<<" ";
 cout<<endl;
 return 0;
}

No comments:

Post a Comment