zoukankan      html  css  js  c++  java
  • UVa 11134

    We would like to place n rooks, 1 ≤ n ≤ 5000, on a n × n
    board subject to the following restrictions
    • The i-th rook can only be placed within the rectangle
    given by its left-upper corner (xli, yli) and its rightlower
    corner (xri, yri), where 1 ≤ i ≤ n, 1 ≤ xli ≤xri ≤ n, 1 ≤ yli ≤ yri ≤ n.
    • No two rooks can attack each other, that is no two rooks
    can occupy the same column or the same row.
    Input
    The input consists of several test cases. The first line of each
    of them contains one integer number, n, the side of the board. n lines follow giving the rectangles
    where the rooks can be placed as described above. The i-th line among them gives xli, yli, xri, andyri. 
    The input file
    is terminated with the integer ‘0’ on a line by itself. Output Your task is to find such a placing of rooks that the above conditions are satisfied and then output n lines each giving the position of a rook in order in which their rectangles appeared in the input. If there are multiple solutions, any one will do. Output ‘IMPOSSIBLE’ if there is no such placing of the rooks. Sample Input 8 1 1 2 2 5 7 8 8 2 2 5 5 2 2 5 5 6 3 8 6 6 3 8 5 6 3 8 8 3 6 7 8 8 1 1 2 2 5 7 8 8 2 2 5 5 2 2 5 5 6 3 8 6 6 3 8 5 6 3 8 8 3 6 7 8 0 Sample Output 1 1 5 8 2 4 4 2 7 3 8 5 6 6 3 7 1 1 5 8 2 4 4 2 7 3 8 5 6 6 3 7

    解题思路:

      经过分析可以发现,每个车横纵坐标的选取是相互独立的,因此可以分别考虑。那么问题可以简化成:将1~n 分别放入[xli,xri]n个区间中。选择的方法采用贪心法——以行为例,先将各个矩形区间按 xr 从小到大排序 [xl1,xr1][xl2,xr2]...[xln,xrn];依次选择每个区间中尽量靠xl的未放置车的位置,比如针对[xl1,xr1],应将车放入xl1位置,此时对于区间[xl2,xr2],xl1位置不再考虑。

    代码如下:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <ctime>
     5 #include <algorithm>
     6 using namespace std;
     7 #define time__ printf("time: %f
    ",double(clock())/CLOCKS_PER_SEC)
     8 const int maxn=5000;
     9 struct Rec{
    10     int xl,yl,xr,yr;
    11 };
    12 Rec rect[maxn+5];
    13 int id[maxn+5];
    14 int id_row_num[maxn+5];
    15 int id_col_num[maxn+5];
    16 int row[maxn+5];
    17 int col[maxn+5];
    18 int n;
    19 void init(){
    20     memset(id_row_num, 0, sizeof id_row_num);
    21     memset(id_col_num, 0, sizeof id_col_num);
    22     memset(row, 0, sizeof row);
    23     memset(col,0,sizeof col);
    24 }
    25 bool cmp_row(int &a,int &b){
    26     return rect[a].xr<rect[b].xr;
    27 }
    28 bool cmp_col(int &a,int &b){
    29     return rect[a].yr<rect[b].yr;
    30 }
    31 bool solve_row(){
    32     for(int i=1;i<=n;i++)
    33         id[i]=i;
    34     sort(id+1, id+1+n, cmp_row);
    35     //bool ok=true;
    36     for(int i=1;i<=n;i++){
    37         Rec &t=rect[id[i]];
    38         bool flag=false;
    39         for(int j=t.xl;j<=t.xr;j++){
    40             if(row[j]==0){
    41                 row[j]=1;
    42                 id_row_num[id[i]]=j;
    43                 flag=true;
    44                 break;
    45             }
    46         }
    47         if(!flag){
    48             return false;
    49         }
    50     }
    51     return true;
    52 }
    53 bool solve_col(){
    54     for(int i=1;i<=n;i++)
    55         id[i]=i;
    56     sort(id+1, id+1+n, cmp_col);
    57     //bool ok=true;
    58     for(int i=1;i<=n;i++){
    59         Rec &t=rect[id[i]];
    60         bool flag=false;
    61         for(int j=t.yl;j<=t.yr;j++){
    62             if(col[j]==0){
    63                 col[j]=1;
    64                 id_col_num[id[i]]=j;
    65                 flag=true;
    66                 break;
    67             }
    68         }
    69         if(!flag){
    70             return false;
    71         }
    72     }
    73     return true;
    74 }
    75 
    76 int main(int argc, const char * argv[]) {
    77     while(scanf("%d",&n)==1&&n){
    78         init();
    79         for(int i=1;i<=n;i++)
    80             scanf("%d%d%d%d",&rect[i].xl,&rect[i].yl,&rect[i].xr,&rect[i].yr);
    81         if(solve_row()&&solve_col()){
    82             for(int i=1;i<=n;i++)
    83                 printf("%d %d
    ",id_row_num[i],id_col_num[i]);
    84         }
    85         else printf("IMPOSSIBLE
    ");
    86         //time__;
    87     }
    88     //time__;
    89     return 0;
    90 }
  • 相关阅读:
    Luogu P1396 营救
    Luogu P1339 热浪Heat Wave
    哈夫曼树学习笔记
    题解 CF1372C
    题解 CF 1372 B
    题解 CF 1372A
    题解 UVA1193 Radar Installation
    题解 洛谷 P2287 [USACO07NOV]Sunscreen G
    洛谷 P1080 国王游戏 题解
    牛客练习赛 66C公因子 题解
  • 原文地址:https://www.cnblogs.com/Kiraa/p/5385244.html
Copyright © 2011-2022 走看看