C++ 基于向量属性的智能分配

C++ 基于向量属性的智能分配,c++,vector,C++,Vector,Asana正在探索一种智能工作负载功能,旨在简化同事之间的任务分配。新创建的任务将自动分配给工作量最轻的团队成员。对于第i个人,已知以下信息: namesi - their name, a string containing only uppercase and lowercase letters; statusesi - their vacation indicator status, which is true if the person is on a vacation, or false

Asana正在探索一种智能工作负载功能,旨在简化同事之间的任务分配。新创建的任务将自动分配给工作量最轻的团队成员。对于第i个人,已知以下信息:

namesi - their name, a string containing only uppercase and lowercase letters;
statusesi - their vacation indicator status, which is true if the person is on a vacation, or false otherwise;
projectsi - the number of projects they are currently involved in;
tasksi - the number of tasks assigned to the report.
如果一个人的休假指标值设置为true,这意味着他们正在休假,无法分配新任务。相反,休假指标值为false意味着他们可以接受任务分配

Asana根据团队成员的可用性对其进行排序。如果A的任务比B少,或者如果这些数字相等,但A的分配项目比B少,那么A的可用性比B高。换句话说,如果A的(任务、项目)对少于B的同一对,我们可以说A的可用性比B的高

您的任务是查找可用性最高的人员的姓名。我们保证只有一个这样的人

范例

For names = ["John", "Martin"], statuses = [false, false],
projects = [2, 1] and tasks = [16, 5],
the output should be
smartAssigning(names, statuses, projects, tasks) = "Martin".
这些参数表示有关两个团队成员的信息:

“John”,状态=false,项目=2,任务=16; “Martin”,状态为false,项目为1,任务为5。 这里约翰和马丁的假期指标都是正确的,所以他们都愿意接受新的任务。Martin只分配了5项任务,而John分配了6项,因此Martin的可用性最高

For names = ["John", "Martin"], statuses = [false, true],
projects = [2, 1] and tasks = [6, 5],
the output should be
smartAssigning(names, statuses, projects, tasks) = "John".
For names = ["John", "Martin"], statuses = [false, false],
projects = [1, 2] and tasks = [6, 6],
the output should be
smartAssigning(names, statuses, projects, tasks) = "John".
这些论点代表以下团队成员:

“John”,状态=false,项目=2,任务=1; “Martin”,状态为true,项目为1,任务为5。 在本例中,无法为Martin分配任何新任务,因为他的假期指标为true。因此,“John”的可用性最高

For names = ["John", "Martin"], statuses = [false, true],
projects = [2, 1] and tasks = [6, 5],
the output should be
smartAssigning(names, statuses, projects, tasks) = "John".
For names = ["John", "Martin"], statuses = [false, false],
projects = [1, 2] and tasks = [6, 6],
the output should be
smartAssigning(names, statuses, projects, tasks) = "John".
提供以下信息:

“John”,状态=false,项目=1,任务=6; “Martin”,状态为false,项目为2,任务为6。 John和Martin的假期指标都是假的,每个人分配的任务数是6。然而,John只参与了1个项目,而Martin参与了2个项目,因此John的可用性最高

For names = ["John", "Martin"], statuses = [false, true],
projects = [2, 1] and tasks = [6, 5],
the output should be
smartAssigning(names, statuses, projects, tasks) = "John".
For names = ["John", "Martin"], statuses = [false, false],
projects = [1, 2] and tasks = [6, 6],
the output should be
smartAssigning(names, statuses, projects, tasks) = "John".
这是我的代码,但没有通过考试。我有什么问题

std::string smartAssigning(std::vector<std::string> names, std::vector<bool> statuses, std::vector<int> projects, std::vector<int> tasks) {
    int indicator=0;
    int num_member=names.size();
    int count=0;
    for (int i=0;i<num_member;i++) {   
       if(statuses[i]==false){
           count++;
           indicator=i;
       }
        else {
            names.erase(names.begin() + i);
            projects.erase(projects.begin() + i);
            tasks.erase(tasks.begin() + i);
        }
     }

    if(count==1)
         return names[indicator] ;   
    num_member=names.size();
    indicator=0;
    int count_min=1;
    int min=tasks[0];
    for (int i=1;i<num_member;i++) {  
       if(tasks[i]==min)  count_min++;
       else if(tasks[i]<min){
           min=tasks[i];
           indicator=i;

       } 

       else {
            names.erase(names.begin() + i);
            projects.erase(projects.begin() + i);
            tasks.erase(tasks.begin() + i);
        }
     }

     if(count_min==1)
         return names[indicator];

    num_member=names.size();
    indicator=0;
    count_min=1;
    min=projects[0];
    for (int i=1;i<num_member;i++) {  
       if(projects[i]==min)  count_min++;
       else if(projects[i]<min){
           min=projects[i];
           indicator=i;

       }        

     }

    if (count_min==1) return names[indicator];    
    else return "";
}
std::string智能分配(std::vector名称、std::vector状态、std::vector项目、std::vector任务){
int指标=0;
int num_member=names.size();
整数计数=0;

对于(int i=0;i您正在从向量中删除元素,但会迭代它们的整个原始大小。
此外,如果删除
begin()+i
处的元素,则递增
i
将使您跳过下一个候选元素(现在是
begin()+i
,而不是删除的元素)

这最终将导致未定义的行为

您的筛选逻辑也有缺陷。
例如,任务列表[3,2,2,1]给出的计数最小值为2,而不是1

由于这最终是一个排序问题,我将通过排序而不是过滤来解决它:

std::string smartAssigning(std::vector<std::string> names,
                           std::vector<bool> statuses,
                           std::vector<int> projects,
                           std::vector<int> tasks)
{
    struct Worker
    {
        Worker(std::string nm, int tasks, int projects) 
            : name(nm), workload(tasks, projects) 
            {}
        std::string name;
        std::pair<int, int> workload; // (tasks, projects)
        // As defined in the description.
        bool operator<(const Worker& other) const { return workload < other.workload; }
    };

    // Gather the people that are available.
    std::vector<Worker> staff;
    for (int i = 0; i < names.size(); ++i)
    {
        // Ignore people on vacation.
        if (!statuses[i])
        {
            staff.emplace_back(names[i], tasks[i], projects[i]);
        }
    }
    // Sort by availability
    std::sort(staff.begin(), staff.end());
    // And we're done.
    return staff[0].name;
}
std::字符串智能赋值(std::向量名称,
std::病媒状态,
std::矢量项目,
std::向量任务)
{
结构工人
{
工作者(标准::字符串nm、int任务、int项目)
:名称(nm)、工作负载(任务、项目)
{}
std::字符串名;
std::配对工作负载;//(任务、项目)
//如描述中所定义。

bool运算符运行代码时代码有什么问题?与应该的行为相比,您的行为是什么?事实上。我不知道哪一个是错误。我通过了示例测试,但隐藏测试失败。因此,我不知道我失败的输入思考
begin()+i
随着向量的缩小和增大而减小……谢谢。你说得对。