UVa 11134 Fabled Rooks(贪心+优先队列)

来源:互联网 发布:小米域名多少钱买的 编辑:程序博客网 时间:2024/06/09 09:02

题目链接


题目大意:

    在一个棋盘上,放置N个国际象棋的城堡,每个城堡给出可以放置的范围,要求这些城堡不能相互攻击到,求这些城堡的摆放方案。


解题思路:

    这题非常关键的一点就是横纵坐标可以分开独立考虑。对于每一个方向,我们维护一个下限小(其次上线小)的区间优先出堆的堆,以及当前要填充的坐标。每次去堆顶区间。如果上限小于要填充坐标说明无法填充。如果下限小于当前要填充坐标,更新下限,否则取下限和要填充坐标进行填充,并且更新要填充坐标。就这样从小到大不断填充,直至所有区间填充完毕。


AC代码:

#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <queue>using namespace std;const int maxn=5000+3;struct Node{    int l,r,id;//下限、上限、编号    bool operator<(const Node &other)const//下限小的优先出堆,其次考虑上限小的    {        if(l!=other.l)            return l>other.l;        else return r>other.r;    }}node[2][maxn];int N,ans[2][maxn];bool solve(int x){    priority_queue<Node> que;    for(int i=0;i<N;++i)        que.push(node[x][i]);    int now=0;    while(!que.empty())    {        Node tmp=que.top(); que.pop();        if(tmp.r<now)//区域内所有坐标都已使用,无法放置            return false;        if(tmp.l<now)//更新下限        {            tmp.l=now;            que.push(tmp);        }        else//放置棋子        {            int put=max(now,tmp.l);            ans[x][tmp.id]=put;            now=put+1;        }    }    return true;}int main(){    while(~scanf("%d",&N)&&N)    {        for(int i=0;i<N;++i)        {            scanf("%d%d%d%d",&node[0][i].l,&node[1][i].l,&node[0][i].r,&node[1][i].r);            node[0][i].id=i;            node[1][i].id=i;        }        if(solve(0)&&solve(1))//横纵坐标都有解            for(int i=0;i<N;++i)                printf("%d %d\n",ans[0][i],ans[1][i]);        else puts("IMPOSSIBLE");    }        return 0;}


0 0
原创粉丝点击