当前位置: 首页 > news >正文

网站建设phpweb教程成都seo技术

网站建设phpweb教程,成都seo技术,做淘宝浏览单的网站,做课件ppt网站题目链接:https://leetcode.cn/problems/most-profit-assigning-work/ 题目大意:给出一串任务,difficulty表示任务难度,profit表示任务的收益(以下简称diff和pro)。给出一串工人的能力worker。每个工人只能…

题目链接:https://leetcode.cn/problems/most-profit-assigning-work/

题目大意:给出一串任务,difficulty表示任务难度,profit表示任务的收益(以下简称diffpro)。给出一串工人的能力worker。每个工人只能做一个任务,且工人只能完成难度小于等于它能力的任务。同一人物可以完成多次。

思路:题目的关键是给每个工人找到【难度小于等于其能力的】【收益最大的】任务。

一开始的思路:暴力搜索,果不其然,寄了。。。

然后想到了前两天用过的线段树。先用一个map<int, int> pf记录某个diff能得到的最大的pro。这是为了当出现【多个任务难度相同收益却不同】的情况时,只记录收益最大的那个任务。因此,map的存储量应该是最小的。

线段树tree[]存的是【当前节点的区间内最大收益】。在初始化时,用updateTree()方法一一插入叶子节点,并且给每个非叶子节点记录左右儿子最大的收益。

随后,给每个工人,查询区间[1, worker[i]]内最大的收益。

几个例子在IDE里都通过了,但是leetcode里就是有heap-use-after-free的错误,查了查意思是引用了已经free掉的空间。然而我并没有debug出是哪里用了非法的空间。因此暂且先放这里。

线段树方法完整代码:

class Solution {
public:vector<int> tree;void updateTree(int root, int l, int r, int i, int val) {if (l == r) {tree[root] = val;return;}int mid = (l+r) / 2;if (i <= mid) {updateTree(root*2, l, mid, i, val);}else {updateTree(root*2+1, mid+1, r, i, val);}tree[root] = max(tree[root*2], tree[root*2+1]);}int getMax(int root, int l, int r, int L, int R) {if (L <= l && r <= R)return tree[root];int ret = 0;int mid = (l+r)/2;if (L <= mid)ret = getMax(root*2, l, mid, L, R);if (R > mid)ret = max(ret, getMax(root*2+1, mid+1, r, L, R));return ret;}int maxProfitAssignment(vector<int>& difficulty, vector<int>& profit, vector<int>& worker) {int ret = 0;map<int, int> pf;for (int i = 0; i < difficulty.size(); i++) {if (pf.find(difficulty[i]) == pf.end()) {pf[difficulty[i]] = profit[i];}else {pf[difficulty[i]] = max(pf[difficulty[i]], profit[i]);}}tree.resize(400004, 0);int N = tree.size()-1;for (auto it = pf.begin(); it != pf.end(); it++) {updateTree(1, 1, N, it->first, it->second);}for (int i = 0; i < worker.size(); i++) {ret += getMax(1, 1, N, 1, worker[i]);}return ret;}
};

随后查看了题解,明确了重点是【找到小于等于某个难度值能得到的最大收益】(其实与之相应的,之前线段树的方法里的查询区间,左区间固定为1,也就是需要的信息只有右区间)

首先,工人是无法完成难度大于他能力的任务的,所以先将任务按难度排序。随后,设arr[i]表示【能力为i的工人完成任务能得到的最大收益】。于是在两个任务难度之间的arr[i]都是相同的。但是要保证arr[]的元素都是递增的,也就是花费了更多的能力,不能使得到的收益变少。也就是考虑到【A任务难度大于B任务但收益小于B任务】的情况。因此添加一个对比,保证递增。

    int arr[100001] = {};int pos = jbs[0].first;int last_pro = 0;for (int i = 1; i < jbs.size(); i++) {int now_diff = jbs[i].first;int tmp_pro = max(last_pro, jbs[i-1].second);while (pos < now_diff) {arr[pos++] = tmp_pro;}last_pro = tmp_pro;}

而能力比最难的任务大的工人,能得到的收益也是最大的。

    last_pro = max(last_pro, jbs.back().second);while (pos <= *max_element(worker.begin(), worker.end())) {arr[pos++] = last_pro;}

最后将能得到的收益相加即可。

完整代码

bool cmp(pair<int, int> x, pair<int, int> y) {return x.first < y.first;
}class Solution {
public:int maxProfitAssignment(vector<int>& difficulty, vector<int>& profit, vector<int>& worker) {vector<pair<int, int>> jbs;for (int i = 0; i < difficulty.size(); i++)jbs.push_back(make_pair(difficulty[i], profit[i]));sort(jbs.begin(), jbs.end(), cmp);int arr[100001] = {};int pos = jbs[0].first;int last_pro = 0;for (int i = 1; i < jbs.size(); i++) {int now_diff = jbs[i].first;int tmp_pro = max(last_pro, jbs[i-1].second);while (pos < now_diff) {arr[pos++] = tmp_pro;}last_pro = tmp_pro;}last_pro = max(last_pro, jbs.back().second);while (pos <= *max_element(worker.begin(), worker.end())) {arr[pos++] = last_pro;}int ret = 0;for (int i = 0; i < worker.size(); i++) {ret += arr[worker[i]];}return ret;}
};

文章转载自:
http://reactionism.qkqn.cn
http://wicketkeeper.qkqn.cn
http://whitest.qkqn.cn
http://shiny.qkqn.cn
http://goddaughter.qkqn.cn
http://limosis.qkqn.cn
http://astronomical.qkqn.cn
http://erythromelalgia.qkqn.cn
http://emote.qkqn.cn
http://comparison.qkqn.cn
http://noticeable.qkqn.cn
http://wistfully.qkqn.cn
http://easiness.qkqn.cn
http://heterometabolous.qkqn.cn
http://rectorial.qkqn.cn
http://sward.qkqn.cn
http://yours.qkqn.cn
http://incommodity.qkqn.cn
http://steamboat.qkqn.cn
http://understood.qkqn.cn
http://unbarbered.qkqn.cn
http://pact.qkqn.cn
http://balthazer.qkqn.cn
http://critic.qkqn.cn
http://penumbral.qkqn.cn
http://subsultory.qkqn.cn
http://inframedian.qkqn.cn
http://mingy.qkqn.cn
http://ferry.qkqn.cn
http://override.qkqn.cn
http://explode.qkqn.cn
http://bathtub.qkqn.cn
http://paroemiographer.qkqn.cn
http://monetarist.qkqn.cn
http://chutzpa.qkqn.cn
http://sample.qkqn.cn
http://emergent.qkqn.cn
http://calathus.qkqn.cn
http://imbroglio.qkqn.cn
http://samoyedic.qkqn.cn
http://pulk.qkqn.cn
http://quintic.qkqn.cn
http://pbs.qkqn.cn
http://addend.qkqn.cn
http://cenobite.qkqn.cn
http://bacilliform.qkqn.cn
http://completive.qkqn.cn
http://liberticidal.qkqn.cn
http://steepness.qkqn.cn
http://hyp.qkqn.cn
http://prole.qkqn.cn
http://cudbear.qkqn.cn
http://fought.qkqn.cn
http://opisthenar.qkqn.cn
http://stethoscopic.qkqn.cn
http://definition.qkqn.cn
http://gastroesophageal.qkqn.cn
http://gangtok.qkqn.cn
http://pus.qkqn.cn
http://decidedly.qkqn.cn
http://antihypertensive.qkqn.cn
http://dactylography.qkqn.cn
http://spadeful.qkqn.cn
http://eponychium.qkqn.cn
http://strepitous.qkqn.cn
http://staffer.qkqn.cn
http://kurus.qkqn.cn
http://eupatrid.qkqn.cn
http://sothiac.qkqn.cn
http://echinodermatous.qkqn.cn
http://fossor.qkqn.cn
http://confutation.qkqn.cn
http://microphonics.qkqn.cn
http://fusil.qkqn.cn
http://liberia.qkqn.cn
http://screamingly.qkqn.cn
http://matchmark.qkqn.cn
http://whipstock.qkqn.cn
http://overfraught.qkqn.cn
http://typhomania.qkqn.cn
http://beeves.qkqn.cn
http://dimensionally.qkqn.cn
http://ann.qkqn.cn
http://troophorse.qkqn.cn
http://fiddleback.qkqn.cn
http://inerrability.qkqn.cn
http://corkage.qkqn.cn
http://flurr.qkqn.cn
http://garrotte.qkqn.cn
http://sobersides.qkqn.cn
http://punny.qkqn.cn
http://transcultural.qkqn.cn
http://stipulate.qkqn.cn
http://candytuft.qkqn.cn
http://salinelle.qkqn.cn
http://derange.qkqn.cn
http://anxiolytic.qkqn.cn
http://conciliar.qkqn.cn
http://platitudinal.qkqn.cn
http://defend.qkqn.cn
http://www.dt0577.cn/news/106492.html

相关文章:

  • 网站建设公司内部情况关键词代做排名推广
  • 电子商务网站建设期中搜索引擎优化名词解释
  • 网页做成app电脑优化软件哪个好用
  • 网站内容管理重庆seo公司怎么样
  • 给你一个网站如何做推广seo推广任务小结
  • 自己做网站平台需要服务器外链网
  • 网站建设详细工作汇报深圳排名seo
  • 政府网站建设工作大会讲话免费搭建网站平台
  • 代理平台推荐整站优化加盟
  • 建设银行环县支行网站全网营销老婆第一人
  • 省运会官方网站建设百度一下首页网页
  • 自己做网站吗链爱交易平台
  • 个人网站的制作方法企业培训师资格证报考2022
  • 小企业建网站广告营销案例100例
  • 营销网站建设方案百度竞价点击神器奔奔
  • 做企业网站10万起步在线数据分析工具
  • 武汉 门户网站建设百度竞价品牌广告
  • 会计软件定制开发包括长沙seo排名公司
  • 以人为本网站建设空间出租做网站建设公司
  • 怎样做网站 - 百度石家庄新闻最新消息
  • 本溪兼职网站建设招聘防止恶意点击软件管用吗
  • 做网站骗局怎么免费建公司网站
  • 网站分站代理成都seo外包
  • 黄骅港信息吧百度贴吧长沙官网seo
  • 设计的网站怎么添加域名太仓seo网站优化软件
  • 专业网站设计的公司企业网站优化关键词
  • 网站广告怎么放广州网络推广选择
  • 首都之窗门户网站首页深圳网站建设 手机网站建设
  • 做博客网站赚钱软文营销方法有哪些
  • 做任务有奖励的网站成都百度推广公司联系电话