Topic links
http://acm.hdu.edu.cn/showproblem.php?pid=5124
This problem to do a good grief, the game when the title does not understand, also did not delve into, after the game and looked at a long time did not understand, asked a lot to understand, I have a desire to cry, I have been
This question [x, Y] This segment as a coordinate, I want to cry, because of the inertia of x, y coordinates,, the original topic tells you the "x, Y" this line,
This is an interval, is the axis, is a one-dimensional coordinates, and has been regarded as a two-dimensional coordinate, tragic.
Official analysis
1002 Lines We can put a line segment[xi,yi] Divided into two endpointsxi And(yi)+1 Inxi This point is added to a new line segment, the same(yi)+1 When the point is reduced by one line segment,
Therefore, the 2n endpoints are sorted to makexi For a value of 1,yi is value-1, the problem is converted to the maximum interval and because 11 is fixed before-1, so the problem becomes the maximum prefix and,
We are looking for the maximum value is the answer, in addition, the problem can be discretized after the line of the tree to do. Complexity is the order of the complexity of nlgn, in addition if the first method of the array should be
is 2n, not n, because of various non-deterministic factors I have set the n=10w point in the small data.
Code
#include <stdio.h>
#include <algorithm>
using namespace Std;
The pair<int,int> a[200010];//is equivalent to the structural body effect.
int main (void)
{
int t,n,ans,k,i;
scanf ("%d", &t);
while (t--)
{
scanf ("%d", &n);
n=n*2;
for (i=0;i<n;i++)
{
scanf ("%d", &a[i].first);
a[i].second=1;
scanf ("%d", &a[++i].first);
a[i].first++;
A[i].second=-1;
}
Sort (a,a+n);
ans=0; k=0;
for (i=0;i<n;i++)
{
K=k+a[i].second;
Ans=max (ANS,K);
}
printf ("%d\n", ans);
}
return 0;
}
Discretization, first mapping all coordinates to the x-axis and then compressing the coordinates. How to compress it?
Can use a V array, the direct storage position can be, then the completion of the coordinates of the compression.
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <map>
#include <vector>
using namespace Std;
#define MAXN 200005
typedef pair<int,int> PII;
int X[MAXN];
PII P[MAXN];
int V[MAXN];
int main ()
{
int t;
scanf ("%d", &t);
while (t--)
{
int n;
scanf ("%d", &n);
int cnt = 0;
for (int i = 0; i<n; i++)
{
scanf ("%d%d", &p[i].first,&p[i].second);
x[cnt++] = P[i].first;
x[cnt++] = P[i].second;
}
Sort (x,x+cnt);
printf ("%d\n", CNT);
printf ("%d\n", X);
CNT = unique (x,x+cnt)-X;
printf ("%d\n", CNT);
for (int i = 0; i<cnt; i++)
{
V[i] = 0;
}
int sum = 0;
for (int i = 0; i<n; i++)
{
int L = lower_bound (X,x+cnt,p[i].first)-X;
int r = Lower_bound (X,x+cnt,p[i].second)-X;
v[l]++;
v[r+1]--;
}
int s = 0;
int mx = 0;
for (int i = 0; i<cnt; i++)
{
S+=v[i];
MX = max (mx,s);
}
printf ("%d\n", MX);
}
return 0;
}
Of course, you don't have to map the coordinates to the x-axis, vector <pair<int,int> > v; V[i].first is used to sort the coordinates and play the role of mapping. Then the subscript of the array plays the role of compression.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace Std;
Vector <pair<int,int> > v;
int main ()
{
int T;
Cin >> T;
while (t--)
{
V.clear ();
int N;
scanf ("%d", &n);
for (int i = 0; i < N; i++)
{
int x;
scanf ("%d", &x);
V.push_back (Make_pair (x,1));
scanf ("%d", &x);
V.push_back (Make_pair (x + 1,-1));
}
Sort (V.begin (), V.end ());
int ans = 0;
int MAXN = 0;
for (int i = 0; i < v.size (); i++)
{
Ans + = V[i].second;
MAXN = max (Maxn,ans);
}
cout << maxn << Endl;
}
}
Hud-5124-lines