注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号
x
[b]
[/b][b]
[/b]LintCode上的这道题目题意是什么,大家有看懂吗?
1465. Order Of Tasks
[color=rgba(0, 0, 0, 0.65098)]
[b]Description[/b]
[color=rgba(0, 0, 0, 0.65098)]There are n different tasks, the execution time of tasks are t[], and the probability of success are p[]. When a task is completed or all tasks fail, the operation ends. Tasks are performed in a different order, and it is expected that the time to stop the action is generally different. Please answer in what order to perform the task in order to make the end of the expected action the shortest time? If the expected end time of the two task sequences is the same, the lexicographic minimum order of tasks is returned.
[color=rgba(0, 0, 0, 0.65098)]- 1≤n≤50,1≤ti≤10,0≤pi≤11≤n≤50,1≤ti≤10,0≤pi≤1
- nn is a positive integer, titi is a positive integer, pipi is a floating-point number
[color=rgba(0, 0, 0, 0.65098)]Have you met this question in a real interview? Yes
[color=rgba(0, 0, 0, 0.65098)][b]Example[/b]
Given n=1, t=[1], p=[1.0], return [1]. Explanation:The shortest expected action end time is 1.0*1+(1.0-1.0)*1=1.0Given n=2, t=[1,2], p=[0.3, 0.7], return [2,1]. Explanation:The shortest expected action end time is 0.7*2+(1.0-0.7)*0.3*(2+1)+(1.0-0.7)*(1.0-0.3)*(2+1)=2.3
贴一个提交的代码:
class Solution {public: /** * @param n: The number of tasks * @param t: The time array t * @param p: The probability array p * @return: Return the order */ struct node { int id, t; double p; node() {} node(int id, int t, double p) : id(id), t(t), p(p) {} }; static bool comp(node &l, node &r) { return l.p * r.t > r.p * l.t || abs(l.p * r.t - r.p * l.t) < 1e-4 && l.id < r.id; } vector<int> getOrder(int n, vector<int> &t, vector<double> &p) { // Write your code here vector<node> a(n); for(int i = 0; i < n; i++) a[i] = node(i + 1, t[i], p[i]); sort(a.begin(), a.end(), comp); vector<int> res; for(auto &it: a) res.push_back(it.id); return res; }};
|