小红书笔试题

第二道题是 凑齐x个粉丝数,给了一个粉丝数组,然后选一个粉丝全要,剩下的粉丝只能要一半。我看到 范围是100,那么我三层for也肯定能过,所以我首先第一层for就选一个 粉丝全要的,在这个基础上,问题变为了 凑够粉丝数y,但是要最小推广。这个就是lc原题了,只不过需要求的是 最小推广次数, 我对此只能 加一个判断。 但是不知道为什么 最后每有过,之有9%,我很疑惑我哪里写错了,我感觉我思路没问题。我试验了 直接输出 -1,也就是无法凑 x,结果也是9%。 那位大佬能说一下 思路啥的,看看我的对不对。
全部评论
这个是背包问题。
点赞 回复 分享
发布于 2024-04-27 12:21 上海

相关推荐

投递美团等公司10个岗位
点赞 评论 收藏
分享
0816 京东笔试答案第一题, 减少逆序对数量对于每个 ai, 找到 所有 aj = a[i] + 1 且 j < i 的 j, 则对于选择 j < L <= i 的 L 贡献 +1, 然后做一个前缀和取最大值即可代码```#include <iostream>#include <cstring>#include <algorithm>#include <vector>#include <unordered_map>using namespace std;void solve(){int n; cin >> n;vector<int> a(n + 10, 0), b(n + 10, 0), c(n + 10, 0);unordered_map<int, vector<int>> pos;for (int i = 1; i <= n; i ++){cin >> a[i];}for (int i = 1; i <= n; i ++){int v =a[i];int t = v + 1;if (pos.find(t) != pos.end()){for (int j : pos[t]){if (j < i){b[j + 1] ++;b[i + 1] --;}}}pos[v].push_back(i);}int ans = 0;int tmp = 0;for (int i = 1; i <= n; i ++){tmp += b[i];ans = max(ans, tmp);}cout << ans << endl;}int main(){int _ = 1;cin >> _;while (_ --){solve();}}```第二题, 跳水打分解法类似求区间最值, 维护每个长度为 m 的区间的最大值和最小值, 然后遍历过去即可算出答案。代码```#include <bits/stdc++.h>#define PII pair<long, long>#define x first#define y secondusing namespace std;int main(){int n, m;cin >> n >> m;priority_queue<PII> maxPq;priority_queue<PII, vector<PII>, greater<PII>> minPq;vector<long long> a(n + 10);double ans = 0;long long tmp = 0;for (int i = 1; i <= n; i ++) cin >> a[i];for (int i = 1; i <= m; i ++){maxPq.push({a[i], i});minPq.push({a[i], i});tmp += a[i];}long long minV = minPq.top().x;long long maxV = maxPq.top().x;ans = double(tmp - minV - maxV) / (m - 2);int ans2 = 1;int l = 1, r = m;while (r < n){// cout << ans << endl;tmp -= a[l]; l ++;r ++; tmp += a[r];minPq.push({a[l], l}); minPq.push({a[r], r});maxPq.push({a[l], l}); maxPq.push({a[r], r});while (minPq.top().y < l) minPq.pop();while (maxPq.top().y < l) maxPq.pop();if ((double(tmp - minPq.top().x - maxPq.top().x) / (m - 2)) > ans){ans = double(tmp - minPq.top().x - maxPq.top().x) / (m - 2);ans2 = l;}}cout << ans2 << endl;}```
投递京东等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
4
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务