uva11134 fabled rooks 贪心+问题独立分解

来源:互联网 发布:java 继承多个类 编辑:程序博客网 时间:2024/06/04 20:14

有点不太明白为什么问题是独立的。。

这里这样贪心是因为,越往小的地方走,灵活性越好。。

 

 

#include<iostream>#include<bits/stdc++.h>using namespace std;#define sf scanf#define pf printf#define mem(a,b) memset(a,b,sizeof(a));#define inf 0x3f3f3f3f#define INF 1e9;#define LL long long#define bug1 printf("bug1\n");#define bug2 printf("bug2\n");const int maxn=5010;struct Node{    int r,l;    int id;    bool friend operator <(Node a,Node b){        if(a.l==b.l)return a.r>b.r;        return a.l>b.l;    }}xx[maxn],yy[maxn];int n;int ans[maxn][2];bool solve(Node *a,int pos){    priority_queue<Node>q;    for(int i=1;i<=n;++i){        q.push(a[i]);    }    int col=1;    while(!q.empty()){        Node tmp=q.top();q.pop();        if(tmp.r<col)return false;        if(tmp.l<col){            tmp.l=col;            q.push(tmp);continue;        }        ans[tmp.id][pos]=max(tmp.l,col);        col=max(tmp.l,col)+1;    }    return true;}int main(){    while(~sf("%d",&n)){        for(int i=1;i<=n;++i){            sf("%d%d%d%d",&xx[i].l,&yy[i].l,&xx[i].r,&yy[i].r);            xx[i].id=yy[i].id=i;        }        if(solve(xx,0)&&solve(yy,1)){            for(int i=1;i<=n;++i){                pf("%d %d\n",ans[i][0],ans[i][1]);            }        }        else{            pf("IMPOSSIBLE\n");        }    }}


 

0 0
原创粉丝点击