Manhattan Mornings
4991: Manhattan Mornings
时间限制: 1 Sec 内存限制: 128 MB提交: 164 解决: 19
[提交][状态][讨论版][命题人:admin]
题目描述
As a New Yorker you are always very busy. Apart from your long work day you tend to have a very long list of
errands that need to be done on any particular day. You really hate getting up early so you always end up going over
your to-do list after work, but this is starting to seriously hurt your free time.
One day you realize that some of the places you have to go by lie on your walk to the office, so you can visit them before work. The next day you notice that if you take a slightly different route to work you can run most of your errands without increasing the length of your route.
Since errands take a negligible amount of time, you don’t even have to get up any earlier to do this! This nice little side effect of the grid-like New York streets gets you thinking. Given all the locations of your errands on the New York grid, how many can you visit on your way to work without getting up any earlier?
The New York grid is modelled with streets that are parallel to the x-axis and avenues that are parallel to the y-axis. Specifically, there is a street given by y = a for every a ∈ Z , and there is an avenue given by x = b for every b ∈ Z . It is given that an errand always takes place on an intersection between a street and an avenue. Since you walk to your work, you can use all roads in both directions.
errands that need to be done on any particular day. You really hate getting up early so you always end up going over
your to-do list after work, but this is starting to seriously hurt your free time.
One day you realize that some of the places you have to go by lie on your walk to the office, so you can visit them before work. The next day you notice that if you take a slightly different route to work you can run most of your errands without increasing the length of your route.
Since errands take a negligible amount of time, you don’t even have to get up any earlier to do this! This nice little side effect of the grid-like New York streets gets you thinking. Given all the locations of your errands on the New York grid, how many can you visit on your way to work without getting up any earlier?
The New York grid is modelled with streets that are parallel to the x-axis and avenues that are parallel to the y-axis. Specifically, there is a street given by y = a for every a ∈ Z , and there is an avenue given by x = b for every b ∈ Z . It is given that an errand always takes place on an intersection between a street and an avenue. Since you walk to your work, you can use all roads in both directions.
输入
• The first line contains an integer 0 ≤ n ≤ 105, the number of errands you have to run that day.
• The next line contains four integers 0 ≤ xh, yh, xw, yw ≤ 109 corresponding to the locations of your house and workplace.
• The next n lines each contain two integers 0 ≤ xi, yi ≤ 109 , the coordinates of your ith errand.
• The next line contains four integers 0 ≤ xh, yh, xw, yw ≤ 109 corresponding to the locations of your house and workplace.
• The next n lines each contain two integers 0 ≤ xi, yi ≤ 109 , the coordinates of your ith errand.
输出
Output a single line, containing the number of errands you can run before work without taking a longer route than necessary.
样例输入
3
0 0 6 6
5 4
2 6
3 1
样例输出
2
提示
来源
#include <bits/stdc++.h> using namespace std; struct pap { int x,y; }a[100005]; int s[100005]; int cmp1(pap xx,pap yy) { if(xx.x==yy.x) { return xx.y<yy.y; } return xx.x<yy.x; } int cmp2(pap xx,pap yy) { if(xx.x==yy.x) { return xx.y<yy.y; } return xx.x>yy.x; } int pan(int l,int r,int x) { int mid; while(l<=r) { mid=(l+r)>>1; if(s[mid]<=x) { l=mid+1; } else { r=mid-1; } } return l; } int main() { int n; while(~scanf("%d",&n)) { int x1,y1,x2,y2; scanf("%d%d%d%d",&x1,&y1,&x2,&y2); if(y1>y2) { swap(x1,x2); swap(y1,y2); } int p,q,k=0; for(int i=1;i<=n;i++) { scanf("%d%d",&p,&q); if(p>=min(x1,x2)&&p<=max(x1,x2)&&q>=min(y1,y2)&&q<=max(y1,y2)) { a[++k].x=p; a[k].y=q; } } if(k==0) { printf("0\n"); } else { if(x1<x2) { sort(a+1,a+1+k,cmp1); } else { sort(a+1,a+1+k,cmp2); } int sum=1; s[1]=a[1].y; for(int i=2;i<=k;i++) { if(a[i].y>=s[sum]) { s[++sum]=a[i].y; } else { s[pan(1,sum+1,a[i].y)]=a[i].y; } } printf("%d\n",sum); } } }