#include <iostream>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <numeric>
#include <chrono>
#include <ctime>
#include <cmath>
#include <cctype>
#include <string>
#include <cstdio>
#include <iomanip>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <iterator>
using namespace std;
const int maxn = 5007;
struct Node {
int first;
int last;
int id;
}nodex[maxn],nodey[maxn];
int n;
bool CmpCheck(const Node& a, const Node& b) {
if (a.first != b.first) return a.first < b.first;
return a.last < b.last;
}
bool CmpOut(const Node& a, const Node& b) {
return a.id < b.id;
}
bool Check(Node * in) {
sort(in + 1, in + n + 1, CmpCheck);
for (int i = 1; i <= n; i++) {
if (in[i].first > in[i].last || in[i].first > i || in[i].last < i) return false;
in[i].first = i;
in[i].last = i;
int j = i + 1;
for ( ;j<=n;j++)
{
if (in[j].first <= i) {
in[j].first = i + 1;
}else if(in[j].first > i + 1) break;
}
sort(in + i + 1, in + j, CmpCheck);
}
return true;
}
int main()
{
while (cin >> n && n) {
bool isok = true;
for (int i = 1; i <= n; i++) {
cin >> nodex[i].first >> nodey[i].first >> nodex[i].last >> nodey[i].last;
nodex[i].id = i;
nodey[i].id = i;
}
if (Check(nodex) && Check(nodey)) {
sort(nodex + 1, nodex + n + 1, CmpOut);
sort(nodey + 1, nodey + n + 1, CmpOut);
for (int i = 1;i<=n;i++)
cout << nodex[i].first << " " << nodey[i].first << endl;
}
else cout << "IMPOSSIBLE" << endl;
}
return 0;
}
例题8-4(uva-11134)
最新推荐文章于 2021-05-18 14:52:46 发布
这篇博客探讨了一种高效算法,用于处理线性区间的覆盖问题。通过排序和迭代,算法确保了所有区间有效覆盖且无重叠。在输入n个区间后,程序检查并更新每个区间的起始和结束点,最终输出可行的解决方案或返回'IMPOSSIBLE'。
459

被折叠的 条评论
为什么被折叠?



