题目:
在给定区域内放车,使得所有车在不同行且不同列(无法互相攻击)
思路:
这个和processor很像,都是在给定范围内安排事件,所以都用到了优先队列,按照前端点优先.
这里要提到由于水平和垂直的位置确定不影响,所以分开确定,即一个车的位置确定分两次.
同时用m来控制下个元素的最小放置位置,如果某元素的最远范围小于m显然是不行的。
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 5005;
int n;
int ans[maxn][2];
struct pos{
int l, r, id;
friend bool operator<(const pos& a, const pos&b){
if(a.l != b.l) return a.l > b.l;
return a.r > b.r;
}
}arr1[maxn], arr2[maxn];
bool check(pos* arr, int p) {
priority_queue<pos> q;
for(int i=0; i<n; i++) q.push(arr[i]);//所有元素入队。
int m = 0;
while(!q.empty()) {
pos tmp = q.top();
q.pop();
if(tmp.r < m) return false;
if(tmp.l < m) {
tmp.l = m;
q.push(tmp);
continue;
}
int cnt = max(m, tmp.l);
ans[tmp.id][p] = cnt;
m = cnt+1;
}
return true;
}
int main() {
while(scanf("%d", &n) != EOF && n) {
for(int i=0; i<n; i++) {
scanf("%d%d%d%d", &arr1[i].l, &arr2[i].l, &arr1[i].r, &arr2[i].r);
arr1[i].id = arr2[i].id = i;
}
if(check(arr1, 0) && check(arr2, 1)) {
for(int i=0; i<n; i++) printf("%d %d\n", ans[i][0], ans[i][1]);
}
else printf("IMPOSSIBLE\n");
}
return 0;
}