Problem Descriptionjohn has several lines. The lines is covered on the X axis. Let's a point which are covered by the most lines. John wants to know how many lines cover A.
Inputthe first line contains a single integerT(1≤t≤) (The data forN> Less than cases), indicating the number of test cases.
Each test case begins with an integern(1≤n≤5) , indicating the number of lines.
Next N lines contains and integersXi andyi(1≤Xi≤Yi≤9) , describing a line.
Outputfor each case, output an integer means how many lines cover A.
Sample Input251 2 2 22 43 45 100051 12 23 34 45 5
Sample OUTPUT3 1
What a good question, through this problem I learned a little bit of knowledge of discretization, first map all coordinates to the x-axis, and then compress 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.
1#include <iostream>2#include <algorithm>3#include <stdio.h>4#include <map>5#include <vector>6 7 using namespacestd;8 9 #defineMAXN 200005Ten Onetypedef pair<int,int>PII; A - intX[MAXN]; - PII P[MAXN]; the intV[MAXN]; - - intMain () - { + intT; -scanf"%d",&t); + while(t--){ A intN; atscanf"%d",&n); - intCNT =0; - for(inti =0; i<n;i++){ -scanf"%d%d",&p[i].first,&p[i].second); -x[cnt++] =P[i].first; -x[cnt++] =P[i].second; in } -Sort (x,x+CNT); toCNT = unique (x,x+cnt)-x; + for(inti =0; i<cnt;i++){ -V[i] =0; the } * intsum =0; $ for(inti =0; i<n;i++){Panax Notoginseng intL = Lower_bound (X,x+cnt,p[i].first)-x; - intR = Lower_bound (X,x+cnt,p[i].second)-x; thev[l]++; +v[r+1]--; A } the ints =0; + intMX =0; - for(inti =0; i<cnt;i++){ $s+=V[i]; $MX =Max (mx,s); - } -printf"%d\n", MX); the } - return 0;Wuyi}View Code
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.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <vector>6 7 using namespacestd;8 9Vector <pair<int,int> >v;Ten One intMain () { A intT; -CIN >>T; - while(t--) { the v.clear (); - intN; -scanf"%d",&N); - for(inti =0; I < n;i++) { + intx; -scanf"%d",&x); +V.push_back (Make_pair (x,1)); Ascanf"%d",&x); atV.push_back (Make_pair (x +1,-1)); - } - sort (V.begin (), V.end ()); - intAns =0; - intMAXN =0; - for(inti =0; i < v.size (); i++) { inAns + =V[i].second; -MAXN =Max (Maxn,ans); to } +cout << MAXN <<Endl; - } the}View Code
HDU-5124 lines