Distant Galaxy
https://vjudge.net/problem/UVALive-3695
You are observing a distant galaxy using a telescope above the Astronomy Tower, and you think that a rectangle drawn in that galaxy whose edges are parallel to coordinate axes and contain maximum star systems on its edges has a great deal to do with the mysteries of universe. However you do not have the laptop with you, thus you have written the coordinates of all star systems down on a piece of paper and decide to work out the result later. Can you finish this task? Input There are multiple test cases in the input file. Each test case starts with one integer N, (1 ≤ N ≤ 100), the number of star systems on the telescope. N lines follow, each line consists of two integers: the X and Y coordinates of the K-th planet system. The absolute value of any coordinate is no more than 109 , and you can assume that the planets are arbitrarily distributed in the universe. N = 0 indicates the end of input file and should not be processed by your program. Output For each test case, output the maximum value you have found on a single line in the format as indicated in the sample output. Sample Input 10 2 3 9 2 7 4 3 4 5 7 1 5 10 4 10 6 11 4 4 6 0 Sample Output Case 1: 7
//left[i]表示x < i且位于边界的点数 on1[i]表示x = i且不包含上下边界的点数 on2[i]表示x = i且包含上下边界的点数
//对于选线i,j答案:left[j] - left[i] + on1[i] + on2[j]
//维护on1[i] - left[i]的最大值即可
两个特殊情况:只有一种x或只有一种y需要特判
我特判x的时候手残写成y调了很久。。。。。。。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <algorithm> 6 #include <queue> 7 #include <vector> 8 #define min(a, b) ((a) < (b) ? (a) : (b)) 9 #define max(a, b) ((a) > (b) ? (a) : (b)) 10 #define abs(a) ((a) < 0 ? (-1 * (a)) : (a)) 11 inline void swap(long long &a, long long &b) 12 { 13 long long tmp = a;a = b;b = tmp; 14 } 15 inline void read(long long &x) 16 { 17 x = 0;char ch = getchar(), c = ch; 18 while(ch < ‘0‘ || ch > ‘9‘) c = ch, ch = getchar(); 19 while(ch <= ‘9‘ && ch >= ‘0‘) x = x * 10 + ch - ‘0‘, ch = getchar(); 20 if(c == ‘-‘) x = -x; 21 } 22 23 const long long INF = 0x3f3f3f3f; 24 const long long MAXN = 1000 + 10; 25 26 long long x[MAXN], y[MAXN], cntx[MAXN], cnty[MAXN], left[MAXN], on1[MAXN], on2[MAXN], t, n, tot, ans; 27 28 //left[i]表示x < i且位于边界的点数 on1[i]表示x = i且不包含上下边界的点数 on2[i]表示x = i且包含上下边界的点数 29 //对于选线i,j答案:left[j] - left[i] + on1[i] + on2[j] 30 //维护on1[i] - left[i]的最大值即可 31 32 bool cmp1(long long a, long long b) 33 { 34 return x[a] == x[b] ? y[a] < y[b] : x[a] < x[b]; 35 } 36 37 bool cmp2(long long a, long long b) 38 { 39 return y[a] == y[b] ? x[a] < x[b] : y[a] < y[b]; 40 } 41 42 int main() 43 { 44 while(scanf("%lld", &n) != EOF && n) 45 { 46 ++ t;ans = 0; 47 for(register long long i = 1;i <= n;++ i) read(x[i]), read(y[i]), cntx[i] = cnty[i] = i; 48 std::sort(cntx + 1, cntx + 1 + n, cmp1); 49 std::sort(cnty + 1, cnty + 1 + n, cmp2); 50 if(y[cnty[1]] == y[cnty[n]]) ans = n; 51 else 52 { 53 x[0] = y[0] = -2,000,000,001; 54 for(register long long i = 1;i <= n;++ i) 55 { 56 if(y[cnty[i]] == y[cnty[i - 1]]) continue; 57 for(register long long j = i + 1;j <= n;++ j) 58 { 59 if(y[cnty[i]] == y[cnty[j]]) continue; 60 if(y[cnty[j]] == y[cnty[j - 1]]) continue; 61 tot = 0; 62 for(register long long k = 1;k <= n;++ k) 63 { 64 if(k == 1 || x[cntx[k]] != x[cntx[k - 1]]) 65 { 66 ++ tot; 67 on1[tot] = on2[tot] = 0; 68 left[tot] = tot == 1 ? 0 : left[tot - 1] + on2[tot - 1] - on1[tot - 1]; 69 } 70 if(y[cntx[k]] > y[cnty[i]] && y[cntx[k]] < y[cnty[j]]) ++ on1[tot]; 71 if(y[cntx[k]] >= y[cnty[i]] && y[cntx[k]] <= y[cnty[j]]) ++ on2[tot]; 72 } 73 if(tot <= 2) 74 { 75 ans = n; 76 break; 77 } 78 long long mi = 0; 79 for(long long j = 1;j <= tot;++ j) 80 { 81 ans = max(ans, mi + left[j] + on2[j]); 82 mi = max(mi, on1[j] - left[j]); 83 } 84 } 85 } 86 } 87 printf("Case %lld: %lld\n", t, ans); 88 } 89 return 0; 90 }