This question is so annoying!
I am so confused.
Question:
Place n cars in the n × n board so that any two cars have different rows and columns, and the I-th car must be placed within the specified I-th rectangle. An output scheme is the coordinates of each vehicle. If there is no solution, an "impossible" is output"
The rows and columns are independent, so they can be processed separately to convert two-dimensional data into point fetch problems on one-dimensional intervals:
There is an interval with a length of N and there are n communities. Find a scheme to get a point in the range between each community, yes, in the big interval, each unit is in the range of 1.
It is wrong to start writing greedy:
Sort the left endpoint of a range from small to large, and then sort the right endpoint from small to large.
Here is an inverse example:
For example, the intervals after sorting in this way are: [1, 3] [1, 3] [2, 2]
Then the first and second points will be placed in the first two [1, 3], and the third point will not be able to be placed.
However, it is clear that there is a legal solution for this situation.
The correct greedy method:
First, the right endpoint of the range is sorted from small to large, and then the left endpoint is sorted from large to small (if the range is short, first select ).
From the perspective of interval:
Then, for each interval, it traverses from left to right in the covered range. If there is no point, it is put in. If no vertices can be placed in the entire traversal interval, it indicates that there is no valid scheme.
From the point of consideration, I lost again .. ===| |
After sorting the intervals, start from the first vertex and find the first interval that can be put in.
The following is the AC code:
1 //#define LOCAL 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <algorithm> 6 using namespace std; 7 8 const int maxn = 5000 + 10; 9 struct Node 10 { 11 int x1, x2, y1, y2; 12 int x, y; 13 int order; 14 }a[maxn]; 15 int n; 16 bool vis[maxn]; 17 18 bool cmp1(Node a, Node b) 19 { 20 return a.x2 < b.x2 || (a.x2 == b.x2 && a.x1 > b.x1); 21 } 22 23 bool cmp2(Node a, Node b) 24 { 25 return a.y2 < b.y2 || (a.y2 == b.y2 && a.y1 > b.y1); 26 } 27 28 bool cmp3(Node a, Node b) 29 { 30 return a.order < b.order; 31 } 32 33 int main(void) 34 { 35 #ifdef LOCAL 36 freopen("11134in.txt", "r", stdin); 37 #endif 38 39 while(scanf("%d", &n) == 1 && n) 40 { 41 for(int i = 0; i < n; ++i) 42 { 43 scanf("%d%d%d%d", &a[i].x1, &a[i].y1, &a[i].x2, &a[i].y2); 44 a[i].order = i; 45 } 46 47 memset(vis, false, sizeof(vis)); 48 flag = true; 49 sort(a, a + n, cmp1); 50 for(int i = 0; i < n; ++i) 51 { 52 for(j = a[i].x1; j <= a[i].x2; ++j) 53 { 54 if(!vis[j]) 55 { 56 vis[j] = true; 57 a[i].x = j; 58 break; 59 } 60 } 61 if(j > a[i].x2) 62 { 63 flag = false; 64 break; 65 } 66 } 67 68 if(flag) 69 { 70 memset(vis, false, sizeof(vis)); 71 sort(a, a + n, cmp2); 72 for(int i = 0; i < n; ++i) 73 { 74 for(j = a[i].y1; j <= a[i].y2; ++j) 75 { 76 if(!vis[j]) 77 { 78 vis[j] = true; 79 a[i].y = j; 80 break; 81 } 82 if(j > a[i].y2) 83 { 84 flag = false; 85 break; 86 } 87 } 88 } 89 } 90 91 if(flag) 92 { 93 sort(a, a + n, cmp3); 94 for(int i = 0; i < n; ++i) printf("%d %d\n", a[i].x, a[i].y); 95 } 96 else 97 puts("IMPOSSIBLE"); 98 } 99 100 return 0;101 }Code Jun
Ultraviolet A 11134 (greedy in the interval) fabled Rooks