High priority queue of the server that receives the request from the server that receives the request.

Source: Internet
Author: User

High priority queue of the server that receives the request from the server that receives the request.

There is an NxN board. You need to put N cars on it. They cannot attack each other, and each car can only be placed in a specified rectangle.

 

Idea: first, cars cannot attack each other, so each row has only one vehicle in each column. We represent each vehicle in coordinates (x, y, the final requirement is that the x coordinates of any two vehicles are different, and the y coordinates of any two vehicles are different. Then, the x and y of each vehicle have their own range .....!!! X and y are independent of each other and will not affect each other !! So we only need to first find their own abscissa, and then find their own ordinate, right? At this time, the problem becomes quite simple. Our question becomes that there are n line segments on the X axis. You must take a point for each line segment, and the points for each line segment must be different from each other. Have we done this? Simply use the priority queue to get greedy.

 

#include 
 
  #include 
  
   #include 
   
    #include
    #include 
     
      #include 
      
       #include 
       
        #include 
        
         #include 
         
          #include using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define rep(i,a,b) for(int i=(a);i<(int)(b);++i)#define rrep(i,b,a) for(int i=(b);i>=(int)(a);--i)#define clr(a,x) memset(a,x,sizeof(a))#define mp make_pair#define eps 1e-10#define LL long long#define zero(x) (-eps < (x) && (x) < eps)const int maxn = 5000 + 5;int lx[maxn],rx[maxn],ly[maxn],ry[maxn];int X[maxn],Y[maxn];vector
          
            v[maxn];int n;bool Try(int *l, int * r,int * ret){ rep(i,0,n) v[i].clear(); rep(i,0,n) v[l[i]].push_back(i); priority_queue
           
             > q; rep(i,0,n){ rep(j, 0, v[i].size()){ int x = v[i][j]; q.push(mp(-r[x],x) ); } if(q.empty() || -q.top().first < i) return false; int c = q.top().second; q.pop(); ret[c]=i; } return true;}bool solve(){ if(!Try(lx,rx,X) || !Try(ly,ry,Y)) return false; rep(i,0,n) printf("%d %d\n",X[i]+1, Y[i]+1); return true;}int main(){ while(scanf("%d",&n),n){ rep(i,0,n){ scanf("%d%d%d%d",lx+i,ly+i,rx+i,ry+i); --lx[i];--ly[i];--rx[i];--ry[i]; } if(!solve()) puts("IMPOSSIBLE"); } return 0;}
           
          
         
        
       
      
     
   
  
 


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.