[模拟]Assignment Algorithm

题目描述

A low-budget airline is designing a sophisticated algorithm that will assign more desirable seats to passengers who buy tickets earlier. Their airplane has r rows of seats, where r is an even integer. There are also 3 exit rows in the airplane; those rows do not contain any seats but only provide access to the emergency exits. One exit row is in the very front of the airplane (before the first row of seats), one in the very back (behind the last row of seats) and one right in the middle. The rows are numbered with integers 1 through r + 3 with row numbers increasing from the front to the back of the airplane. Rows numbered 1, r/2 + 2 and r + 3 are exit rows while all the other rows are seat rows.
The seating configuration is “3–3–3” — each seat row contains three groups of three seats with the passenger aisles between the groups. Seats in the same row are denoted with consecutive letters left to right corresponding to the pattern “ABC.DEF.GHI”.
When a passenger purchases a ticket, she is assigned a seat according to the following rules:
1. If there is an empty seat in a row directly after an exit row, all other rows are ignored in the following step (but they are not ignored when balancing the airplane in the last step).
2. first, we select a seat row with the largest number of empty seats. If there are multiple such rows,we select the one closest to an exit row (distance between rows a and b is simply |a − b|). If there are still multiple such rows, we select the one with the lowest number.
3. Now, we consider empty seats in the selected row and select one with the highest priority. Seat priorities, from highest to lowest are as follows:
(a) Aisle seats in the middle group (D or F).
(b) Aisle seats in the first and third group (C or G).
(c) Window seats (A or I).
(d) Middle seat in the middle group (E).
(e) Other middle seats (B or H).
If there are two empty seats with the same highest priority, we consider the balance of the entire airplane. The airplane’s left side contains all seats with letters A, B, C or D, while the right side contains all seats with letters F, G, H or I. We select an empty seat in the side with more empty seats. If both sides have the same number of empty seats, we select the seat in the left side of the airplane.
Some of the airplane’s seats are already reserved (possibly using a completely different procedure from the one described above). Determine the seats assigned to the next n passengers purchasing a ticket.

输入

The first line contains two integers r and n (2 ≤ r ≤ 50, 1 ≤ n ≤ 26) — the number of seat rows in the airplane (always an even integer) and the number of new passengers purchasing tickets. The following r + 3 lines contain the current layout of the airplane. The j-th line contains exactly 11 characters — the layout of the row j of the airplane. Exit rows and aisles are denoted with the “.” characters. The “#” character denotes a reserved seat, while the “-” character denotes a seat that is currently empty. You may assume there will be at least n empty seats in the airplane.

输出

Output r + 3 lines containing the final layout of the irplane. The layout should be exactly the same as in input with the following exception: the seat assigned to the j-th passenger purchasing a ticket should be denoted with the j-th lowercase letter of the English alphabet.

样例输入

2 17
...........
---.#--.---
...........
---.---.---
...........

样例输出

...........
hnd.#lb.fpj
...........
kqg.cma.eoi
...........

模拟。
AC代码:
#include <iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

char Map[55][55];
int seat[55];
int d[55];
int r,n;
int row;
int lef;
int righ;

void getrow(){
   if(seat[2]&&seat[r/2+3]){
     if(seat[2]==seat[r/2+3]||seat[2]>seat[r/2+3]) row=2;
     else row=r/2+3;
   }
   else if(seat[2]) row=2;
   else if(seat[r/2+3]) row=r/2+3;
   else{
      int maxseat=0;
      for(int i=1;i<=r+3;i++) maxseat=max(maxseat,seat[i]);
      int mind=100;
      for(int i=1;i<=r+3;i++) if(seat[i]==maxseat) mind=min(mind,d[i]);
      for(int i=1;i<=r+3;i++) if(seat[i]==maxseat&&d[i]==mind) {row=i; break;}
   }
}

void getseat(char c){
   seat[row]--;
   if(Map[row][5]=='-'&&Map[row][7]=='-'){
      if(lef>righ) {Map[row][7]=c; righ++; return;}
      else {Map[row][5]=c; lef++; return;}
   }
   else if(Map[row][5]=='-') {Map[row][5]=c; lef++; return;}
   else if(Map[row][7]=='-') {Map[row][7]=c; righ++; return;}
   if(Map[row][3]=='-'&&Map[row][9]=='-'){
      if(lef>righ) {Map[row][9]=c; righ++; return;}
      else {Map[row][3]=c; lef++; return;}
   }
   else if(Map[row][3]=='-') {Map[row][3]=c; lef++; return;}
   else if(Map[row][9]=='-') {Map[row][9]=c; righ++; return;}
   if(Map[row][1]=='-'&&Map[row][11]=='-'){
      if(lef>righ) {Map[row][11]=c; righ++; return;}
      else {Map[row][1]=c; lef++; return;}
   }
   else if(Map[row][1]=='-') {Map[row][1]=c; lef++; return;}
   else if(Map[row][11]=='-') {Map[row][11]=c; righ++; return;}
   if(Map[row][6]=='-') {Map[row][6]=c; return;}
   if(Map[row][2]=='-'&&Map[row][10]=='-'){
      if(lef>righ) {Map[row][10]=c; righ++; return;}
      else {Map[row][2]=c; lef++; return;}
   }
   else if(Map[row][2]=='-') {Map[row][2]=c; lef++; return;}
   else if(Map[row][10]=='-') {Map[row][10]=c; righ++; return;}
}

int main()
{
    scanf("%d%d",&r,&n);
    for(int i=1;i<=r+3;i++){
        for(int j=1;j<=11;j++){
            scanf(" %c",&Map[i][j]);
            if(Map[i][j]=='-') seat[i]++;
            if(Map[i][j]=='#'){
                if(j<6) lef++;
                if(j>6) righ++;
            }
        }
        d[i]=min(abs(i-1),min(abs(i-r/2-2),abs(i-r-3)));//i分别到1,r/2+2,r+3的距离。到r+3的距离我竟然写成了abs(i-r)...
    }
    for(int i=0;i<n;i++){
        char c='a'+i;
        getrow();
        getseat(c);

    }
    for(int i=1;i<=r+3;i++){
        for(int j=1;j<=11;j++){
            cout<<Map[i][j];
        }
        printf("\n");
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/lllxq/p/8849379.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值