Wang Guojun's chief command, Brigadier Caius, decided to re-establish the Intelligence Agency. He needed to select competent soldiers from all over the country. The selection criteria were A and B. For each candidate soldier, if two capabilities of another soldier are greater than or equal to him, the other soldier will be eliminated. (Note: If the two soldiers have the same capabilities, they are considered to be behind the rule ).
It can be considered that soldiers with minor differences in capabilities are more suitable for one team. (We define the difference in capabilities as follows: Set the and B capacities of soldiers I to AI, Bi, and soldiers J to A and B to AJ, BJ, then their difference is: | ai-AJ | + | BI-BJ |)
For every uneliminated soldier, as long as there is another soldier in a team that does not differ more than K from his or her abilities, he can join the team.
Currently, casius wants to select the most popular team to meet the above conditions.
Input: Row 1st: two integers separated by spaces: N and K. N indicates the total number of candidate soldiers, as described in the meaning.
Line 2nd. n + 1: line I + 1 uses two spaces to separate integer AI and Bi, indicating the capabilities of soldiers A and B.
Output: Only one integer indicates the maximum number of users.
Sample input: 5 3
2 11
4 10
3 9
1 12
6 8
Sample output: 3
Data range: 20% of data 1 <= n <= 1000
The total number of soldiers whose data are not eliminated by 40% is no more than 3000.
100% of data. 1 <= n <= 100000, and the remaining data is in [0,].
I think this question is quite good...
The problem solution should be complicated. In fact, this question is just a silly question (which question does it mean a silly question ?)... It is not necessary to use and query the set.
First, let's talk about the first problem we need to solve: If we can determine which points can be left behind?
Based on the question, we know that to satisfy the fact that the abscissa and ordinate of no point are greater than or equal to that point, that point can be left behind.
It seems difficult to judge the two conditions unless you are violent.
We can first consider the one-dimensional situation. We certainly know that it is a sort;
In fact, the same is true for two dimensions. We sort all vertices by X coordinates (assuming that the order is sorted from large to small)
We can be sure that the preceding vertex cannot be eliminated by the following vertex, right? (because the preceding vertex x is bigger than the following vertex x. If it is equal, you can delete any vertex. You can delete the vertex following it as the standard)
Now the question is, how can we determine that the Y of the following vertex is smaller than the y of the preceding vertex?
In fact, it is also very simple. We constantly update the largest y and compare the following points with the current maximum y size;
Next, the second question: what if we determine the maximum number of people in the set?
What magical properties will we find when we try to draw all the remaining points on the paper?
A straight line formed by all vertices is a falling curve! (In the case of sorting by X from large to small, and the remaining points, Y is gradually increasing)
What is the purpose of this nature?
Let's sort the remaining points (or sort by X in ascending order)
If the distance between the following vertex and the previous vertex is not equal to or less than K, can we know that it is not in the previous set? The points behind it are even less likely to be in the front set (you can just think about them)
In this case, do you just scan it and update the maximum number of sets continuously?
Code Attached: (the Code itself is sorted by X from small to large, but there is no big difference)
#include<cstdio>#include<algorithm>#include<cstring>#include<iostream>#include<queue>#include<cmath>#include<map>#include<set>#include<cstdlib>using namespace std;const int INF = 2147483647,maxn = 100005;struct node{ int x,y; bool operator <(const node &a)const{ return x==a.x?y<a.y:x<a.x; }}a[maxn];node b[maxn];int n,ans,k;inline int solve(int x,int y){ return abs(b[x].x-b[y].x)+abs(b[x].y-b[y].y);}int main(){ //freopen("data","r",stdin); freopen("troops.in","r",stdin); freopen("troops.out","w",stdout); scanf("%d%d",&n,&k); for(int i=1;i<=n;i++) scanf("%d%d",&a[i].x,&a[i].y); sort(a+1,a+n+1); int Max = a[n].y,tmp = 0,t = 1; b[++tmp] = a[n]; for(int i=n-1;i;i--) if(Max<a[i].y)Max = a[i].y,b[++tmp] = a[i]; for(int i=2;i<=tmp;i++){ if(solve(i,i-1)<=k)t++;else ans = max(ans,t),t = 1; } ans = max(ans,t); cout<<ans; return 0;}
Noip simulation competition: troops [tips? Idea?]