[real problem of autumn recruitment machine test] hw- calculation of task execution time executed in sequence

Keywords: Algorithm

/*
*      Title Description:
*          Given N tasks (1 < = N < = 100), the task numbers are accumulated sequentially from 0. These N tasks are queued in the system and executed in sequence. The execution time of each task
*      It is a non negative number, followed by t1,t2,...tn. if there are dependencies between some tasks, if the task on which a task depends is not executed, the task needs to return to the end of the queue
*      Re queue. Only task execution and task queuing will consume time, and the remaining operation consumption time is ignored. Please calculate the actual execution time (actual) of each task
     Execution time = execution time of the task itself + time waiting for other tasks to execute in the queue)

     Enter Description:
         The first line indicates the execution time of N tasks in the order of increasing task number. It is a comma separated string. The value range of execution time is [1, 999],
     For example, 1,3,4 (there is no space before and after the comma) indicates that there are three tasks in total, and the execution time of each task is 1,3,4 respectively.
         The input in the second line represents the dependency between tasks, which is a comma separated string. Each dependency indicates the dependency between two task numbers,
     For example: 0 - > 2 (there is no space before and after the comma), indicating that there is a dependency. The execution of task No. 0 depends on the execution of task No. 2. Pay attention to one
     A task can depend on the execution of multiple tasks. In this case, all the dependent tasks must be completed before the task can be executed
         The input is guaranteed to be legal, and there is no need to consider abnormal input scenarios such as spaces in the middle, and cyclic dependency between tasks

     Output Description:
         Output the actual execution time of each task in the order of increasing the task number, separated by commas, without spaces before and after commas, for example: 8,3,7

     Example 1:
         Input:
            1,3,4
            0->2
         Output:
            8,3,7
     Example 2:
         Input:
            1,3,4,5,8,5,3,6
            0->2,2->4,2->6
         Output:
            35,3,24,8,16,21,24,30
*/

explain:

        Huawei 0407 the second true question,

Problem solving ideas:

        1. Input processing: record the time required for each task according to the task number, and record the task list that each task depends on;

        2. Then execute each task in a circular order, simulate the process of task execution, and use two key variables to record, last_time records the end time of the previous task, and count records the number of completed tasks of the current task

      

 

The code is as follows:

#include<iostream>
#include<vector>
#include<string>
using namespace std;
vector<int> solution(vector<vector<int>>& depends, vector<int>& time) {
	// Number of tasks
	int n = time.size();
	// Task count completed
	int count = 0;
	// Record the time when the last task was completed
	int last_time = 0;
	// Record whether the task is completed
	vector<bool> isComplete(n, false);
	// Record the completion time of each task
	vector<int> res(n);
	// 
	while (count < n) {
		for (int i = 0; i < n; ++i) {
			// Initialize whether the task can be performed
			//If all tasks have been completed, return in advance
			if (count == n) break;
			//If the task has been completed, skip
			if (isComplete[i]) continue;
			bool toExcute = true;
			for (int dependTask:depends[i]) {
				// Judge whether the tasks that the task depends on have been completed. If they have been completed, the task can be executed
				// If a dependent task is not completed, the task cannot be executed
				if (!isComplete[dependTask]) toExcute = false;
			}
			if (toExcute) {
				last_time += time[i];
				res[i] = last_time;
				isComplete[i] = true;
				++count;
			}
		}
	}
	return res;
}
vector<string> splitToString(string& s, string c) {
	vector<string> res;
	int left = 0;
	int idx;
	int size = c.size();
	string tmp;
	while (s.find(c, left) != s.npos) {
		idx = s.find(c, left);
		tmp = s.substr(left, idx - left);
		left = idx + size;
		res.push_back(tmp);
	}
	tmp = s.substr(left, s.size() - left);
	res.push_back(tmp);
	return res;
}
vector<int> splitToNum(string& s, string c) {
	vector<string> stringVector = splitToString(s, c);
	vector<int> res(stringVector.size());
	for (int i = 0; i < res.size(); ++i) {
		res[i] = atoi(stringVector[i].c_str());
	}
	return res;
}
/*
vector<int> calculateTaskCost(vector<int>& task_time, vector<vector<int>>& graph) {
	vector<int> res(task_time.size());

}
*/
string printString(vector<int>& time) {
	string res;
	for (int i = 0; i < time.size(); ++i) {
		res += to_string(time[i]);
		res += ',';
	}
	res.erase(res.size() - 1);
	return res;
}
int main() {
	string time;
	cin >> time;
	vector<int> task_time = splitToNum(time, ",");
	string s;
	cin >> s;
	vector<string> pairs = splitToString(s, ",");
	vector<vector<int>> depends(task_time.size());
	for (string pair : pairs) {
		vector<int> tasks = splitToNum(pair, "->");
		depends[tasks[0]].push_back(tasks[1]);
	}
	vector<int> res = solution(depends, task_time);
	string prints = printString(res);
	cout << prints << endl;
}

The execution results are shown in the figure below:

Example 1:

  Example 2:

 

Posted by defunct on Fri, 10 Sep 2021 00:36:46 -0700