首页 > 代码库 > UVA 11134 Fabled Rooks

UVA 11134 Fabled Rooks

贪心+优先队列+问题分解

对x,y 分开处理

当 xl<cnt(当前处理行)时,不能简单的选择cnt,而是应该让xl=cnt 并重新加入优先队列。(y的处理同上)

 

 

 1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 #include <queue> 5 using namespace std; 6  7 struct node { 8     int l,r; 9     int id;10     friend bool operator < (const node &dis,const node &res){11         if (dis.l!=res.l) return dis.l>res.l;12         else return dis.r>res.r;13     }14 }rookx[5005],rooky[5005];15 16 int ans[2][5005];17     int n;18 19 int solved (node* x,int p){20     priority_queue<node> q;21     while (!q.empty())22         q.pop();23     for (int i=1;i<=n;i++)24         q.push(x[i]); 25     int cnt=1;26     while (!q.empty() ){27         node a;28         a=q.top();29         q.pop() ;30         if (a.r<cnt)31             return 0;32         if (a.l<cnt){33             a.l=cnt;34             q.push(a);35             continue ;36         } 37         if (a.l>cnt){38             return 0;39         }40         ans[p][a.id]=cnt++;41     }42     return 1;43 }44 45 int main (){46     while (cin>>n&&n){47         for (int i=1;i<=n;i++){48             cin>>rookx[i].l>>rooky[i].l>>rookx[i].r>>rooky[i].r;49             rookx[i].id=rooky[i].id=i;50         }51         if (solved (rookx,0)&&solved (rooky,1)){52             for (int i=1;i<=n;i++)53                 cout<<ans[0][i]<<" "<<ans[1][i]<<endl;54         }55         else cout<<"IMPOSSIBLE"<<endl;56     }57     return 0;58 }