Fabled Rooks(UVA 11134)

网址如下:

Fabled Rooks - UVA 11134 - Virtual Judge

(第三方网址)

思路就是将二维拆成两个一维+贪心:

二维拆成两个一维:

他让我们计算x和y,我们分开计算这两个玩意就行了,先生成x轴上满足条件的答案,然后生成y轴的,最后拼在一起就是(xi, yi),也就是答案

贪心:

因为拆成了一维了,本来的矩形就变成了区间,这里我构造了struct:

struct Node{
    int l, r, id;
    Node(){}
    Node(int l, int r, int id):l(l), r(r), id(id){}
    bool operator<(const Node &t)const;
};

也就是表示第id个车应该在[ l, r ]区间之内,我们先按照 l 的大小从小到大排序,若 l 相同,则 r 小的在前面

如果第一个节点的区间包含1,则第id个车的 x/y 就取1了,然后把后面的节点的区间删去1,再排序,然后再取第一个节点,看看是否包含2。。。。。。以此类推

按 l 从小到大排序是为了判断当前是否有满足要取的地点的区间

而在 l 相同的情况下,若 r 更大,则有更多机会在其他位置取到,而 r 更小的需要更早被取,否则可能会造成取不到的情况

代码如下:

#include<cstdio>
#include<queue>
#include<vector>
using namespace std;

const int maxn = 5010;

struct Node{
    int l, r, id;
    Node(){}
    Node(int l, int r, int id):l(l), r(r), id(id){}
    bool operator<(const Node &t)const;
};
typedef priority_queue<Node, vector<Node>> pq;

pq qx, qy;
int ansx[maxn], ansy[maxn];
int n;

bool Node::operator<(const Node &t)const{
    if(l > t.l) return true;
    else if(l == t.l) return r > t.r;
    else return false;
}
bool getans(pq &q, int ans[]){
    int cur = 1;
    while(!q.empty()){
        Node u = q.top(); q.pop();
        if(cur < u.l || u.r < u.l) return false;
        ans[u.id] = cur;
        if(!q.empty()){for(Node v = q.top(); v.l == cur; v = q.top()){q.pop();v.l += 1;q.push(v);}}
        cur++;
    }
    return true;
}
inline void print_ans(void){
    for(int i = 1; i <= n; i++) printf("%d %d\n", ansx[i], ansy[i]);
}

int main(void)
{
    while(scanf("%d", &n) && n){
        while(!qx.empty()) qx.pop();
        while(!qy.empty()) qy.pop();
        for(int i = 1; i <= n; i++){
            int xl, yl, xr, yr; scanf("%d%d%d%d", &xl, &yl, &xr, &yr);
            qx.push(Node(xl, xr, i)); qy.push(Node(yl, yr, i));
        }
        if(getans(qx, ansx) && getans(qy, ansy)) print_ans();
        else printf("IMPOSSIBLE\n");
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值