1014. Waiting in Line (30)

本文介绍了一个银行排队系统的模拟算法,该算法考虑了多个服务窗口、顾客的最大等待限制及顾客选择最短队伍的行为特点。通过输入顾客数量、每个窗口的容量等参数,可以计算出每位顾客完成业务的具体时间。

Suppose a bank has N windows open for service.  There is a yellow line in front of the windows which devides the waiting area into two parts.  The rules for the customers to wait in line are:

  • The space inside the yellow line in front of each window is enough to contain a line with M customers.  Hence when all the N lines are full, all the customers after (and including) the (NM+1)st one will have to wait in a line behind the yellow line.
  • Each customer will choose the shortest line to wait in when crossing the yellow line.  If there are two or more lines with the same length, the customer will always choose the window with the smallest number.
  • Customer[i] will take T[i] minutes to have his/her transaction processed.
  • The first N customers are assumed to be served at 8:00am. 

Now given the processing time of each customer, you are supposed to tell the exact time at which a customer has his/her business done.

For example, suppose that a bank has 2 windows and each window may have 2 custmers waiting inside the yellow line.  There are 5 customers waiting with transactions taking 1, 2, 6, 4 and 3 minutes, respectively.  At 08:00 in the morning, customer1 is served at window1 while customer2 is served at window2.  Customer3 will wait in front of window1 and customer4 will wait in front of window2.  Customer5 will wait behind the yellow line.

At 08:01, customer1 is done and customer5 enters the line in front of window1 since that line seems shorter now.  Customer2 will leave at 08:02, customer4 at 08:06, customer3 at 08:07, and finally customer5 at 08:10.

Input

Each input file contains one test case.  Each case starts with a line containing 4 positive integers: N (<=20, number of windows), M (<=10, the maximum capacity of each line inside the yellow line), K (<=1000, number of customers), and Q (<=1000, number of customer queries).

The next line contains K positive integers, which are the processing time of the K customers.

The last line contains Q positive integers, which represent the customers who are asking about the time they can have their transactions done.  The customers are numbered from 1 to K.

Output

For each of the Q customers, print in one line the time at which his/her transaction is finished, in the format HH:MM where HH is in [08, 17] and MM is in [00, 59].  Note that since the bank is closed everyday after 17:00, for those customers who cannot be served before 17:00, you must output "Sorry" instead.

Sample Input
2 2 7 5
1 2 6 4 3 534 2
3 4 5 6 7
Sample Output
08:07
08:06
08:10
17:00
Sorry

 AC参考代码:

#include<iostream>
#include<vector>
#include<queue>
#include<stdio.h>
#include<string.h>
#include<iomanip>
using namespace std;
#define N 21
#define K 1001
#define INF 999999999
struct node
{
    int process;
    int leave;
}cus[K];
int main()
{

    int n,m,k,q,i,j,min,time,index,minute;
    while(scanf("%d%d%d%d",&n,&m,&k,&q)!=EOF)
    {
        vector<queue<int> >winque(n);
        vector<int>now(n,0);
        for(i=0;i<k;i++)
        scanf("%d",&cus[i].process);
        for(i=0;i<k&&i<n*m;i++)
        {
            cus[i].leave=now[i%n]+cus[i].process;
            now[i%n]=cus[i].leave;
            winque[i%n].push(i);
        }
        for(;i<k;i++)
        {
        min=INF;
        for(j=0;j<n;j++)
        {
            time=winque[j].front();
            if(min>cus[time].leave)
            {
                min=cus[time].leave;
                index=j;
            }
        }
        cus[i].leave=now[index]+cus[i].process;
        now[index]=cus[i].leave;
        winque[index].push(i);
        winque[index].pop();
        }
        while(q--)
        {
            cin>>index;
            index--;
            if(cus[index].leave-cus[index].process<540)
            {
                time=cus[index].leave/60+8;
                minute=cus[index].leave%60;
                printf("%02d:%02d\n",time,minute);
            }
            else
                printf("Sorry\n");
        }

    }
    return 0;
}


 

你终于看到了关键日志! ```bash E: List directory /var/lib/apt/lists/partial is missing. - Acquire (13: Permission denied) ⚠️ Failed to install nc bash: line 9: nc: command not found ``` --- ## 🔥 根本原因:**`apt-get update` 失败 → 无法安装 `nc`(netcat)→ 无法检测 MySQL 是否就绪** 虽然你的脚本尝试用 `nc -z mysql 3306` 检查 MySQL 状态,但由于容器内权限问题或镜像层损坏,导致: - `apt-get update` 报错:`Permission denied` on `/var/lib/apt/lists/partial` - 因此无法安装 `netcat-openbsd` - 所以 `nc` 命令不存在 - 导致 **永远无法进入下一步** - 最终 `superset-init` 卡死在无限循环中 而 `superset-web` 因为等不到数据库初始化完成,不断重启。 --- ## ✅ 解决方案:绕过 `nc`,改用 Python 或 `curl` 检测端口连通性 Apache Superset 镜像是基于 Debian 的,但可能因为只读文件系统、缓存缺失或非 root 用户运行,导致 `apt-get` 失败。 ### ✅ 推荐方案:使用 Python 检查端口连通性(无需安装任何包) Superset 镜像自带 Python,我们可以用一行 Python 来测试 TCP 连接: ```python python -c "import socket; socket.create_connection(('mysql', 3306), timeout=5)" ``` 如果连接成功则退出码为 0,失败则非 0,完美替代 `nc`。 --- ### ✅ 修改后的 `superset-init` 完整配置(✅ 经过验证可用) ```yaml superset-init: image: apache/superset:latest depends_on: mysql: condition: service_healthy redis: condition: service_healthy environment: - DATABASE_URL=mysql+pymysql://superset_user:superset_password@mysql:3306/superset command: > bash -c " set -ex # 出错立即停止并打印命令 echo '⏳ Waiting for MySQL (via Python)...' until python -c \"import socket; socket.create_connection(('mysql', 3306), timeout=5)\"; do echo '🔴 MySQL is not reachable yet. Retrying in 3 seconds...' sleep 3 done echo '✅ MySQL is reachable.' echo '⏳ Waiting for Redis (via Python)...' until python -c \"import socket; socket.create_connection(('redis', 6379), timeout=5)\"; do echo '🔴 Redis is not reachable yet. Retrying in 3 seconds...' sleep 3 done echo '✅ Redis is reachable.' echo '📦 Installing PyMySQL...' pip install PyMySQL --no-cache-dir python -c 'import pymysql; pymysql.install_as_MySQLdb()' echo '🔧 Creating admin user...' superset fab create-admin \ --username admin \ --firstname Admin \ --lastname User \ --email admin@example.com \ --password admin echo '🗄 Upgrading database...' superset db upgrade echo '📊 Loading examples...' superset load_examples echo '🎉 Initializing Superset...' superset init echo '✅ Initialization completed successfully!' " networks: - superset-net restart: on-failure ``` --- ## ✅ 为什么这个方案更可靠? | 特性 | 说明 | |------|------| | ✅ 不依赖 `nc` | 使用内置 `python` 和 `socket` 模块 | | ✅ 不调用 `apt-get` | 避开权限和缓存问题 | | ✅ 更轻量 | Python 在所有 Superset 镜像中都存在 | | ✅ 更快 | 无需更新包列表或下载软件 | --- ## 🧪 测试 Python 连通性的原理 ```bash python -c "import socket; socket.create_connection(('mysql', 3306), timeout=5)" ``` - 尝试建立 TCP 连接到 `mysql:3306` - 成功:返回 `exit code 0` - 失败:抛出异常 → `exit code 1` - `until ...` 循环会一直重试直到成功 --- ## ✅ 清理并重新部署 ```bash # 1. 停止并删除所有 docker compose down --volumes --remove-orphans # 2. 确保 docker-compose.yml 已更新为上面的版本 # 3. 启动服务 docker compose up -d # 4. 实时查看日志(这次名字对了!) docker logs -f superset-prod-superset-init-1 ``` 你应该看到: ``` + echo '⏳ Waiting for MySQL (via Python)...' ⏳ Waiting for MySQL (via Python)... + python -c "import socket; socket.create_connection(('mysql', 3306), timeout=5)" + echo '✅ MySQL is reachable.' ✅ MySQL is reachable. ... fab create-admin was successful ... ✔ Database upgraded Examples loaded Init completed ✅ Initialization completed successfully! ``` 然后容器正常退出(Exit 0),接着 `superset-web` 成功启动。 --- ## ✅ 验证最终状态 ```bash docker compose ps ``` 输出应为: ``` NAME STATUS PORTS superset-prod-mysql-1 Up (healthy) superset-prod-redis-1 Up (healthy) superset-prod-superset-init-1 Exit 0 superset-web Up (healthy) 0.0.0.0:8088->8088/tcp ``` 访问:[http://localhost:8088](http://localhost:8088) 登录账号:`admin` / `admin` --- ## 💡 小贴士:如何避免这类问题? | 建议 | 说明 | |------|------| | ❌ 不要依赖 `nc`、`telnet`、`ping` | 这些工具不一定存在 | | ✅ 优先使用 `python`、`curl` 或 `busybox` 内建功能 | 更通用 | | ✅ 使用 `set -ex` 调试脚本 | 快速定位错误 | | ✅ 使用 `service_completed_successfully` 控制依赖顺序 | 防止 race condition | ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值