zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    图片预加载的JS代码
    JavaScript实现漫天飞花及文字滚动特效的代码
    判断远程图片是否存在的JavaScript代码
    dos批处理命令详解
    内存虚拟盘软件XMSDSK的使用
    医学论坛(收集)
    velocity
    java开源项目 源代码
    开放源代码的全文检索引擎Lucene
    批处理命令大全
  • 原文地址:https://www.cnblogs.com/gfc-g/p/3873325.html
Copyright © 2011-2022 走看看