Cf 76f tourist (maximum non-decreasing sub-sequence deformation)

Source: Internet
Author: User
Tags time 0
F. touristtime limit per test

1 second

Memory limit per test

256 megabytes

Input

Standard Input

Output

Standard output

Tourist walks alongXAxis. He can choose either of two directions and any speed not exceedingV.
He can also stand without moving anywhere. He knows from newspapers that at timeT1 In
The point with CoordinateX1
Interesting event will occur, at timeT2 In
The point with CoordinateX2-
Another one, and so on up (XN, Bytes,TN).
Interesting events are short so we can assume they are immediate. EventICounts visited if at timeTITourist
Was at point with CoordinateXI.

Write Program tourist that will find maximum number of events tourist if:

  • At the beginning (when time is equal to 0) Tourist appears at point 0,
  • Tourist can choose initial point for himself.

Yes, you shoshould answer on two similar but different questions.

Input

The first line of input contains single integer numberN(1 digit ≤ DigitNLimit ≤0000100000)
-Number of interesting events. The followingNLines contain two integersXIAndTI-
Coordinate and time ofI-Th event. The last line of the input contains integerV-
Maximum speed of the tourist. AllXIWill
Be within range between-limit 2 · 108 bytes ≤ bytesXILimit ≤ limit 2 · 108,
AllTIWill be
Between 1 and 2 · 106. Aggressive.VWill
Be positive and will not exceed 1000. The input may contain in events that happen at the same time or in the same place but not in the same place at the same time.

Output

The only line of the output shoshould contain two space-sepatated integers-Maximum number of events tourist can visit in he starts moving from point 0 at time 0, and maximum number of events tourist can visit if he chooses the initial point for himself.

Sample test (s) Input
3-1 142 740 82
Output
1 2

Title: http://codeforces.com/problemset/problem/76/F

Question: Give You n events and locations. In a straight line, you can see at most several events...

Analysis: the most direct idea is to sort by time, and then we can simply use DP, which obviously times out. In this case, we have to change the data order in another sort mode, also change the transfer equation...

One point can reach another. A condition ABS (X [I]-X [J]) <= ABS (T [I]-T [J]) * V

This inequality can be converted to-X [I] + T [I] * v <=-X [J] + T [J] * V & X [I] + T [I] * v <= x [J] + T [J] * V, why?

Suppose T [I] <= T [J], then a: X [I]-X [J] <= (T [J]-T [I]) * V (X [I]> = x [J]) B: X [J]-X [I] <= (T [J]-T [I]) * V (X [I] <X [J])

After careful consideration, we find that when X [I]> = x [J], expression B is satisfied, and expression A is satisfied. If AB is satisfied at the same time, it means that we can reach

T [I]> T [J] is similar to the above. Finally, the above inequality is introduced...

After knowing the relationship above, convert each event into two variables, A =-X [I] + T [I] * v B = x [I] + T [I] * V

Sort by A. If AIS the same, sort by B... Then there is the LIS problem.

PS: I didn't study mathematics seriously in high school. It seems that I was really wrong t_t.

Code:

#include<cstdio>#include<iostream>#include<algorithm>using namespace std;const int mm=111111;struct data{    long long p,q;}g[mm];long long q[mm],x[mm],t[mm],v;int i,j,n,r,ans;bool cmp(data a,data b){    return a.p<b.p||(a.p==b.p&&a.q<b.q);}int find(int l,int r,long long x){    int m,ret=0;    while(l<=r)    {        m=(l+r)>>1;        if(q[m]>x)ret=m,r=m-1;        else l=m+1;    }    return ret;}int main(){    while(~scanf("%d",&n))    {        for(i=0;i<n;++i)            scanf("%I64d%I64d",&x[i],&t[i]);        scanf("%I64d",&v);        for(i=0;i<n;++i)        {            g[i].p=-x[i]+t[i]*v;            g[i].q=x[i]+t[i]*v;        }        sort(g,g+n,cmp);        q[0]=-1e12,q[r=1]=1e12;        for(i=0;i<n;++i)        {            j=find(0,r,g[i].q);            if(q[j]>g[i].q)q[j]=g[i].q;            if(j>=r)q[++r]=1e12;        }        ans=r-1;        q[r=1]=1e10;        for(i=0;i<n;++i)            if(g[i].p>=0&&g[i].q>=0)            {                j=find(0,r,g[i].q);                if(q[j]>g[i].q)q[j]=g[i].q;                if(j>=r)q[++r]=1e12;            }        printf("%d %d\n",r-1,ans);    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.