题意:给定 n 个区间,问最多重复的子区间?
题解:(离散化思想)讲所有的数都排个序,将区间的左值定为 1 ,右值定为 -1 ,这样对所有的数搜一遍过去找最大的值即可
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <math.h> 5 #include <time.h> 6 #include <ctype.h> 7 #include <string> 8 #include <queue> 9 #include <set> 10 #include <map> 11 #include <stack> 12 #include <vector> 13 #include <algorithm> 14 #include <iostream> 15 #define PI acos( -1.0 ) 16 using namespace std; 17 typedef long long ll; 18 19 const int NO = 1e5 + 10; 20 struct ND 21 { 22 int x, y; 23 }st[NO<<1]; 24 int n; 25 26 bool cmp( const ND &a, const ND &b ) 27 { 28 if( a.x == b.x ) return a.y > b.y; 29 return a.x < b.x; 30 } 31 32 int main() 33 { 34 int T; 35 scanf( "%d",&T ); 36 while( T-- ) 37 { 38 scanf( "%d", &n ); 39 int cur = 0; 40 for( int i = 0; i < n; ++i ) 41 { 42 scanf( "%d", &st[cur].x ); 43 st[cur++].y = 1; 44 scanf( "%d", &st[cur].x ); 45 st[cur++].y = -1; 46 } 47 sort( st, st+cur, cmp ); 48 int ans = 0; 49 int Max = 0; 50 for( int i = 0; i < cur; ++i ) 51 { 52 ans += st[i].y; 53 Max = max( ans, Max ); 54 } 55 printf( "%d ", Max ); 56 } 57 return 0; 58 }