coderforces #387 Servers(模拟)

本文介绍了一个基于时间循环的任务调度模拟算法,该算法用于确定在给定的服务器数量和任务需求下,哪些任务能够被执行,哪些将被忽略。通过跟踪每个服务器的状态及占用情况,实现了对任务的有效管理和调度。

Servers

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There are n servers in a laboratory, each of them can perform tasks. Each server has a unique id — integer from 1 to n.

It is known that during the day q tasks will come, the i-th of them is characterized with three integers: ti — the moment in seconds in which the task will come, ki — the number of servers needed to perform it, and di — the time needed to perform this task in seconds. All ti are distinct.

To perform the i-th task you need ki servers which are unoccupied in the second ti. After the servers begin to perform the task, each of them will be busy over the next di seconds. Thus, they will be busy in seconds ti, ti + 1, ..., ti + di - 1. For performing the task, ki servers with the smallest ids will be chosen from all the unoccupied servers. If in the second ti there are not enough unoccupied servers, the task is ignored.

Write the program that determines which tasks will be performed and which will be ignored.

Input

The first line contains two positive integers n and q (1 ≤ n ≤ 100, 1 ≤ q ≤ 105) — the number of servers and the number of tasks.

Next q lines contains three integers each, the i-th line contains integers ti, ki and di (1 ≤ ti ≤ 106, 1 ≤ ki ≤ n, 1 ≤ di ≤ 1000) — the moment in seconds in which the i-th task will come, the number of servers needed to perform it, and the time needed to perform this task in seconds. The tasks are given in a chronological order and they will come in distinct seconds.

Output

Print q lines. If the i-th task will be performed by the servers, print in the i-th line the sum of servers' ids on which this task will be performed. Otherwise, print -1.

Examples
Input
4 3
1 3 2
2 2 1
3 4 3
Output
6
-1
10
Input
3 2
3 2 3
5 1 2
Output
3
3
Input
8 6
1 3 20
4 2 1
6 5 5
10 1 1
15 3 6
21 8 8
Output
6
9
30
-1
15
36
Note

In the first example in the second 1 the first task will come, it will be performed on the servers with ids 1, 2 and 3 (the sum of the ids equals 6) during two seconds. In the second 2 the second task will come, it will be ignored, because only the server 4 will be unoccupied at that second. In the second 3 the third task will come. By this time, servers with the ids 1, 2 and 3 will be unoccupied again, so the third task will be done on all the servers with the ids 1, 2, 3 and 4 (the sum of the ids is 10).

In the second example in the second 3 the first task will come, it will be performed on the servers with ids 1 and 2 (the sum of the ids is 3) during three seconds. In the second 5 the second task will come, it will be performed on the server 3, because the first two servers will be busy performing the first task.

 【分析】可能有更简单的方法吧,我这个方法比较笨,直接循环模拟时间(天数)。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 10000000000000
#define met(a,b) memset(a,b,sizeof a)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef long long ll;
using namespace std;
const int N = 1e6+5;
const int M = 4e5+5;
int n,m,k,tot=0;
struct man{
    int k,d;
}a[N];
int main()
{
    //int vis[1000000];
    int mark[105];
    met(mark,0);
    int s,t,t1,t2,p,dir;
    scanf("%d%d",&n,&m);
    while(m--){
        scanf("%d%d%d",&s,&t,&t1);

        a[s].k=t;a[s].d=t1;
    }
    tot=n;
    for(int i=1;i<=1000000;i++){
        //if(i==15)printf("!!!%d\n",a[15].k);
        //printf("i=%d\n",i);
        if(!a[i].k){
            tot=0;
            for(int j=1;j<=n;j++){
                if(mark[j])mark[j]--;
                if(mark[j]<1)tot++;
                //printf("#%d\n",j,mark[j]);
            }
            continue;
        }
        //printf("!!!%d %d\n",i,tot);
        if(tot<a[i].k){
            printf("-1\n");
            tot=0;
            for(int j=1;j<=n;j++){
                if(mark[j])mark[j]--;
                if(mark[j]<1)tot++;
                //printf("#%d\n",j,mark[j]);
            }
            continue;
        }
        else {
            tot=0;
            p=a[i].k;
            int sum=0;
            for(int j=1;j<=n;j++){

                if(mark[j]==0&&p!=0){
                    mark[j]=a[i].d;
                    sum+=j;
                    p--;
                }
                if(mark[j])mark[j]--;
                if(mark[j]<1)tot++;
            }
            printf("%d\n",sum);
        }
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/jianrenfang/p/6197369.html

<think>好的,我现在需要帮助用户解决在Ubuntu系统中使用Ansible时,执行`ansible servers -m ping`命令出现“No hosts matched”的问题。用户提到的问题可能涉及Ansible的主机清单配置,我需要根据提供的引用和之前的对话来寻找解决方案。 首先,回顾用户的上一个问题,他们是在三台Ubuntu虚拟机上安装和配置Ansible,可能涉及主机清单的设置。用户现在遇到了主机未匹配的问题,说明Ansible无法识别“servers”组中的主机。 接下来,查看用户提供的引用内容: 引用[1]和[2]提到了主机清单的配置,其中使用了别名和IP地址,并指定了`ansible_python_interpreter`。引用[3]讨论了分组变量和主机变量,可能涉及文件结构。引用[4]和[5]提到了Ansible的配置文件,如`ansible.cfg`中的设置,包括禁用主机密钥检查和指定清单路径。 在之前的回答中,用户被指导编辑`/etc/ansible/hosts`文件,添加服务器IP,并设置全局变量。测试连接时使用`ansible servers -m ping`。现在出现的问题可能是由于主机清单配置错误、路径问题或SSH配置问题。 接下来,我需要考虑可能的解决方案: 1. **检查主机清单路径**:用户可能修改了默认的清单路径,或者在配置文件中指定了不同的路径。根据引用[5],用户可能在`ansible.cfg`中设置了`inventory = ~/ansible/config/hosts`,导致Ansible没有读取默认的`/etc/ansible/hosts`文件。需要确认当前使用的清单文件位置。 2. **验证主机清单内容**:用户是否正确填写了IP地址,是否有拼写错误,组名是否一致。引用[1]和[2]中的示例使用了别名和`ansible_host`参数,用户可能需要在清单中明确指定IP,尤其是在使用别名的情况下。 3. **检查组名是否正确**:在之前的回答中,组名是`[servers]`,用户执行命令时指定了`servers`组,需要确认组名是否完全一致,包括大小写。 4. **全局变量设置**:引用[2]和之前的回答中都提到了`all:vars`中的`ansible_user`和`ansible_python_interpreter`是否正确,特别是用户名是否正确,可能导致SSH连接失败。 5. **SSH配置问题**:虽然用户之前配置了免密登录,但可能仍有问题,比如密钥未正确分发,或`authorized_keys`权限问题。需要检查是否可以手动SSH连接到目标主机。 6. **Ansible配置覆盖**:用户可能在`ansible.cfg`中设置了`host_key_checking = False`(引用[4]和[5]),但若未设置,可能导致主机密钥验证失败,尤其是在测试环境中,用户可能没有接受主机密钥。 7. **使用`-i`参数指定清单文件**:可能用户没有使用默认清单文件,执行命令时需要显式指定路径,例如`ansible -i /etc/ansible/hosts servers -m ping`。 8. **检查Ansible版本和兼容性**:不同版本的Ansible可能有不同的配置要求,用户是否按照正确的版本进行配置,比如Ubuntu 22.04中的Ansible版本可能与其他版本不同。 接下来,需要将这些可能性整理成步骤,帮助用户逐步排查问题。例如,首先确认清单文件的位置和内容,然后检查SSH连接,接着验证Ansible配置,最后测试连接。 另外,用户可能忽略了一些细节,比如在主机清单中使用别名但没有指定`ansible_host`,或者组名拼写错误。例如,如果清单中的组名是`[servers]`,而用户执行时写成了`[server]`,也会导致不匹配。 还要考虑到用户是否有权限读取清单文件,或者文件权限设置是否正确。例如,`/etc/ansible/hosts`文件是否对当前用户可读。 最后,可能需要建议用户使用`ansible-inventory --list`命令来查看当前Ansible识别的清单结构,确认servers组是否存在,并且包含正确的主机。</think>### 解决Ansible执行`ansible servers -m ping`时出现"No hosts matched"的排查指南 #### 一、核心问题定位 当执行 `ansible servers -m ping` 返回 "No hosts matched" 时,说明Ansible未找到名为 `servers` 的主机组或主机组内未定义有效主机。以下是系统化排查步骤: --- #### 二、分步解决方案 ##### 1. **确认主机清单文件路径** - **默认路径检查** Ansible默认读取 `/etc/ansible/hosts`。若自定义了配置文件(如 `~/.ansible.cfg`),需检查是否覆盖了默认路径[^4][^5]: ```bash # 查看当前生效的清单文件路径 ansible --version | grep "inventory" ``` - **显式指定清单文件** 临时测试时可通过 `-i` 参数强制指定: ```bash ansible -i /etc/ansible/hosts servers -m ping ``` ##### 2. **验证主机清单内容** - **检查组名与IP地址** 确保 `/etc/ansible/hosts` 中已正确定义 `[servers]` 组,且IP地址无拼写错误: ```ini [servers] 10.0.0.4 # 直接使用IP 10.0.0.14 10.0.0.24 # 或使用别名+IP(需明确指定ansible_host) server1 ansible_host=10.0.0.4 server2 ansible_host=10.0.0.14 server3 ansible_host=10.0.0.24 ``` - **检查全局变量** 若使用别名,需在 `[all:vars]` 中指定连接用户和Python解释器: ```ini [all:vars] ansible_user=ubuntu # 替换为实际的SSH用户名 ansible_python_interpreter=/usr/bin/python3 ``` ##### 3. **检查清单语法与权限** - **文件权限问题** 确保Ansible有权限读取清单文件: ```bash sudo chmod 644 /etc/ansible/hosts ``` - **语法验证工具** 使用 `ansible-inventory` 验证清单解析结果: ```bash ansible-inventory --list # 查看所有主机组信息 ansible-inventory --graph # 以树状图显示主机关系 ``` ##### 4. **SSH连接验证** - **手动测试SSH连接** 从控制节点手动连接目标主机,确认免密登录是否生效: ```bash ssh ubuntu@10.0.0.4 # 替换为实际用户名和IP ``` - **检查公钥分发** 若手动连接失败,重新分发公钥: ```bash ssh-copy-id ubuntu@10.0.0.4 ``` ##### 5. **Ansible配置检查** - **禁用主机密钥验证(测试环境适用)** 在 `~/.ansible.cfg` 或 `/etc/ansible/ansible.cfg` 中添加: ```ini [defaults] host_key_checking = False # 跳过SSH主机密钥确认[^4][^5] ``` - **日志调试** 启用详细日志输出以定位问题: ```bash ansible servers -m ping -vvv # 显示详细调试信息 ``` --- #### 三、常见错误场景与修复 | **场景** | **解决方案** | |-----------------------------------|-----------------------------------------------------------------------------| | 主机清单中组名拼写错误(如`[server]`) | 确保组名与命令中完全一致(如 `[servers]`) | | SSH用户无权限或用户名错误 | 在 `[all:vars]` 中设置正确的 `ansible_user`,并确认用户有sudo权限[^4] | | Python解释器路径不匹配 | 检查目标主机是否安装Python3,并在清单中指定 `ansible_python_interpreter`[^2] | | 防火墙或端口限制 | 确保目标主机的SSH端口(默认22)开放,且无网络隔离 | --- #### 四、验证修复效果 完成上述调整后,重新执行命令: ```bash ansible servers -m ping ``` 成功时会返回每个主机的 `"ping": "pong"` 响应[^4]。 --- ### 相关问题 1. 如何通过Ansible动态管理不同环境(开发/生产)的主机清单? 2. Ansible Playbook中如何为不同主机组定义差异化变量? 3. 如何通过Ansible Vault加密敏感的主机连接信息?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值