一共才100个正方形,将所有正方形左下角和右上角的X坐标和Y坐标离散化,直接枚举新建公园的点的坐标即可。
O(n^3)的时间复杂度。
1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <algorithm> 5 6 using namespace std; 7 8 const int MAXN = 110; 9 const int INF = 255; 10 11 struct Land 12 { 13 int x, y; 14 int len; 15 int import; 16 }; 17 18 int N, K, M; 19 int cnt; 20 Land land[MAXN]; 21 int cntX, cntY; 22 int X[ MAXN << 1 ]; 23 int Y[ MAXN << 1 ]; 24 25 bool InPark( Land park, Land d ) 26 { 27 int stx = park.x, sty = park.y; 28 int edx = park.x + park.len, edy = park.y + park.len; 29 30 //printf("%d %d %d %d ", stx, sty, edx, edy ); 31 //printf("%d %d %d %d --- ", d.x, d.y, d.x + d.len, d.y + d.len ); 32 33 if ( d.x >= stx && d.x < edx ) 34 { 35 if ( d.y >= sty && d.y < edy ) return true; 36 if ( d.y + d.len > sty && d.y + d.len <= edy ) return true; 37 } 38 39 if ( d.x + d.len > stx && d.x + d.len <= edx ) 40 { 41 if ( d.y >= sty && d.y < edy ) return true; 42 if ( d.y + d.len > sty && d.y + d.len <= edy ) return true; 43 } 44 return false; 45 } 46 47 void solved() 48 { 49 int ans = INF; 50 for( int i = 0; i < cntX; ++i ) 51 for( int j = 0; j < cntY; ++j ) 52 { 53 Land park; 54 park.x = X[i], park.y = Y[j]; 55 park.len = K; 56 57 if ( park.x + park.len <= 1 + N && park.y + park.len <= 1 + N ) 58 { 59 int influence = 1; 60 for ( int k = 0; k < M; ++k ) 61 { 62 if ( InPark( park, land[k] ) ) 63 { 64 //puts("***"); 65 influence = max( influence, land[k].import ); 66 } 67 } 68 ans = min( ans, influence ); 69 } 70 } 71 72 if ( ans <= 100 ) printf( "%d ", ans ); 73 else puts("IMPOSSIBLE"); 74 75 return; 76 } 77 78 int main() 79 { 80 //freopen( "s.out", "w", stdout ); 81 while ( ~scanf( "%d%d", &N, &K ) ) 82 { 83 scanf( "%d", &M ); 84 cntX = cntY = 0; 85 for ( int i = 0; i < M; ++i ) 86 { 87 scanf("%d%d%d%d", &land[i].import, &land[i].len, &land[i].x, &land[i].y ); 88 X[cntX++] = land[i].x; 89 X[cntX++] = land[i].x + land[i].len; 90 Y[cntY++] = land[i].y; 91 Y[cntY++] = land[i].y + land[i].len; 92 } 93 94 X[cntX++] = 1; 95 X[cntX++] = 1 + N; 96 Y[cntY++] = 1; 97 Y[cntY++] = 1 + N; 98 99 sort( X, X + cntX ); 100 sort( Y, Y + cntY ); 101 cntX = unique( X, X + cntX ) - X; 102 cntY = unique( Y, Y + cntY ) - Y; 103 104 solved(); 105 } 106 return 0; 107 }